Ammo - Springs: Change rest length of a spring


Recently I’m working on spring constraint with Ammojs to create a muscle-like behaviour.
With Ammo, I can change the stiffness, damping and linear limits.
(doc: Bullet Collision Detection & Physics Library: btGeneric6DofSpringConstraint Class Reference)

I think the better thing to change to have a good behaviour is the rest length of the spring. If a muscle contract, its length get smaller. It’s also easier to control the length than tweaking the stiffnes which also allow to change the length but with less control.

I created a small playground where I attached a spring between 2 boxes. The spring part start at line 70. I read a post where someone was using setLinearLimits to change the rest length (c++ - What are "frame A" and "frame B" in btGeneric6DofConstraint's constructor for? - Game Development Stack Exchange) . I tried to do the same but I get some strange behaviour. Changing linear limits to get a minimal restlength imply that it’s behave like a rod. I made a rotation to the box to display this. Just uncomment line 108-109.

If someone has an idea on how to change the rest length and have a rubber band behaviour :).

Ammo is definitively not easy to grasp, I hope cannon will support compound body.

Thanks for help.

Adding @trevordev to have a look :slight_smile:

1 Like

Adding @Cedric who is also a physic simulation mastermind :smiley:


Hi Spyrou

A call to setEquilibriumPoint is needed to achieve what you want. This method has been exposed in ammo.js a couple days ago. I’m going to update BabylonJS and will let you know when you can try it again.


Hi Cedric,

Ok :).
Thanks ^^. I saw the function in ammojs but I didn’t understand why I couldn’t use it.

Thanks @Cedric for the implementation of the EquilibriumPoint. I just noticed ^^.

The problem still remain. Maybe I’m not doing it right.

In this PG13, I added the equilibrium point on the y axis to -10 (line 131). The distance between the 2 boxes is now correct.
But it behave still like a rod and not a rubber band. The spring is going straight to the right.

Someone has a suggestion ?

Thanks for help :slight_smile:

Maybe you can chain 2 btGeneric6DofSpringConstraint. 1 for each box with a transform between.

I’m looking for this kind of behaviour =>

It is done with CannonJS wich I cannot use because it doesn’t handle the compound bodies at the time I’m writing this reply ;s.
But I hope this is can be done in Ammojs.

I’m not sure for the 2 springs. I think it will add another behaviour and it the problem will be the same no ?

If Ammojs is not suited for this, I think I’m gonna try to create a compound body with cannonjs.

@Cedric, you were thinking about something like that ?
line 177: Creation of the spring
line 1: Creation of the spring class
it’s funny how it goes crazy ^^

Ok so I think I found something. I attached two ConeTwist constraints on each insertion.

It seems to behave well. I think I’ll have to tweak some of the limits of the cone.
One question remain, is’nt this fix too heavy for like 200 springs with each 2 joints. There will be like 400 cone contraints and 200 spring constraints.

It behaves well!

I don’t know how fast it will be on your target. Is there a way you can enable/disable joints based on some distance (camera, player,…). The idea is to only activate the joints you subject to interact with.