Prismatic Joint -> Spring (how to)

Just figured out how to hack the Prismatic Joint into a spring:

Code:
var pj:b2PrismaticJoint = joint as b2PrismaticJoint;
var pjt:Number = pj.GetJointTranslation();
var pjs:Number = pj.GetJointSpeed();
pj.SetMaxMotorForce(Math.abs((pjt * 100) + (pjs * 10))); // 100 is the spring constant, 10 is the damping constant.
pj.SetMotorSpeed(pjt > 0 ? -10000 : +10000); // Arbitrary humongous number.


You'll have to do that for each time step. I see a lot of people asking about Springs here so I thought I'd post it.
Works for revolute joints to:

Code:
var rj:b2RevoluteJoint = joint as b2RevoluteJoint;
var rja:Number = rj.GetJointAngle();
rj.SetMaxMotorTorque(Math.abs((rja * 5000) + (rj.GetJointSpeed() * 100)));
rj.SetMotorSpeed(rja > 0 ? -10000 : +10000);
... and if you want the bodies to rotate freely like a distance joint, just stick another "dummy" body in there with a revolute joint:

Code:
...
var emptyDef:b2BodyDef = new b2BodyDef();
emptyDef.position = new b2Vec2(x, y);
var empty:b2Body = world.b2world.CreateDynamicBody(emptyDef);
var emptySD:b2CircleDef = new b2CircleDef();
emptySD.radius = 1.0;
emptySD.density = 1.0;
emptySD.maskBits = 0x0000;
empty.CreateShape(emptySD);
empty.SetMassFromShapes();
var emptyJD:b2RevoluteJointDef = new b2RevoluteJointDef();
emptyJD.Initialize(empty, b2body, new b2Vec2(x, y));
world.b2world.CreateJoint(emptyJD);
pjd.Initialize(empty, otherb2body, new b2Vec2(x, y), axis);
...
joint = b2world.CreateJoint(pjd);


Ta da!
Cool, but is that really considered hacking? It looks like that's how springs were really intended to be done, at least in the current iteration of the library, since there are no spring entities defined by box2d. The joints, especially with the inclusion of motors and limits, makes custom forces (of which spring forces are a subset) very nice to implement.
mayobutter: I really don't think springs (as in distance joint type ones) are meant to be implemented this way. Creating a dummy body and two joints seems somewhat arcane.
I think the philosophy is that true springs should be implemented entirely yourself, as unlike joints they don't require complicated solver techniques. C'mon, it's like 10 lines of code (baring sign errors)

Code:
world1 = body1.localtoworld(spring.local1)
world2 = body2.localtoworld(spring.local2)
d = world2 - world1
l = d.normalise()

world1v = body1.velocityatpoint(spring.local1)
world2v = body2.velocityatpoint(spring.local2)
dv = world2v - world1v
lv = dv.dot(d)

d *= spring.k * (spring.l - l) - spring.kv * lv
body1.applyforce(world1,d)
body2.applyforce(world1,-d)
BorisTheBrave wrote:
mayobutter: I really don't think springs (as in distance joint type ones) are meant to be implemented this way. Creating a dummy body and two joints seems somewhat arcane.
I think the philosophy is that true springs should be implemented entirely yourself, as unlike joints they don't require complicated solver techniques. C'mon, it's like 10 lines of code (baring sign errors)


Yes, I'll admit the "dummy" joint code was a bit silly, but in cases where you do want the typical constraints of the Prismatic or Revolute joint, I think this is pretty handy.

I actually set out to implement springs exactly like you suggested, but stopped after considering these two cases, where both A and B are the bodies connected by the spring :

1. A and B are both dynamic and can move freely.
2. A is dynamic and can move freely, but B is static or in some other way constrained from moving.

It seems like in both cases the spring should condense / expand at the same rate, BUT if you apply equal forces to each body, case (2) will expand / condense at half the speed (because the force applied to B will be wasted - it's stuck). So, in case (2), A should receive double the force of A in case (1).

Maybe I'm completely wrong and case (2) SHOULD expand / condense at half the speed. All spring physics tutorials I find tend to deal with one side of the spring being stuck to something static like a wall though.
mayobutter wrote:
1. A and B are both dynamic and can move freely.
2. A is dynamic and can move freely, but B is static or in some other way constrained from moving.

It seems like in both cases the spring should condense / expand at the same rate, BUT if you apply equal forces to each body, case (2) will expand / condense at half the speed (because the force applied to B will be wasted - it's stuck). So, in case (2), A should receive double the force of A in case (1).

Maybe I'm completely wrong and case (2) SHOULD expand / condense at half the speed. All spring physics tutorials I find tend to deal with one side of the spring being stuck to something static like a wall though.


Well, as long as you apply newtons second law (equal and opposite forces) the masses will take care of that effect.

If you had a 1kg mass attached to a 50kg mass it wont be very much different spring behavior than if the 1kg one was attached to a solid wall. Think about it.

I think joints plus springs in order to use the constraints is totally legitimate and should work very well.
I just use b2PrismaticJoint and Hooke's law in Update function:
Code:
var kx:Number = - 75 * joint.GetJointTranslation() / (joint.GetUpperLimit() - joint.GetLowerLimit());
joint.SetMaxMotorForce(kx);
And it works perfectly!
Hi,

I have seen another implementation

in each timestep

spring1->SetMaxMotorForce( baseSpringForce + (40 * baseSpringForce * powf(spring1->GetJointTranslation(), 2) ) );
spring1->SetMotorSpeed( -20 * spring1->GetJointTranslation() );

spring2->SetMaxMotorForce( baseSpringForce + (40 * baseSpringForce * powf(spring2->GetJointTranslation(), 2) ) );
spring2->SetMotorSpeed( -20 * spring2->GetJointTranslation());


Where baseSpringForce is 5 times the mass being supported by the spring (i.e. the car in this example).

I am not sure what the physics behind this is, but it seems to involve the distance of the joint translation squared as a factor, and then seems to set the motor speed in the opposite direction to the prismatic joint translation.

WHat is the physics this is based on, what are the constants? Which is the correct implementation of a prismatic spring for, for example, wheel suspension??

So many questions, so little knowledge!!

Sorry for cross posting this in 2 threads, I put it in the wrong thread (albeit a related spring / distance joint discussion).

Thanks

i

Original thread: https://box2d.org/forum/viewtopic.php?t=1007