This project has moved and is read-only. For the latest updates, please go here.

F3 How to make a spring not push back?

Jul 31, 2010 at 1:19 PM
Edited Jul 31, 2010 at 1:28 PM

I'm making a game where I need to attach two bodies with eachother, using a spring.
I read the Box2D manual and use a DistanceJoint, where I set it to become 'springy'. Like this:

            objPhysicsJoint = new DistanceJoint(objBodyA, objBodyB, vecAnchorA, vecAnchorB);
            objPhysicsJoint.CollideConnected = true;
            objPhysicsJoint.Frequency = fSpringStrength;
            objPhysicsJoint.DampingRatio = fSpringDamping;
            objPhysicsJoint.Length /= 1.2f; // 1.5f;
(fSpringStrength = 2, fSpringDmaping = 0.1)
This works, but I need the spring to only pull and not 'push back'. So when the bodies move closer to eachother, and by that making the distance between them shorter, I don't want the spring to push the bodies back to the string's 'normal' length. All I want the spring to do is pull when it gets stretched out.
Anyone knows how? I figure it's much like the Mousejoint but then the MouseJoint is fixed and I need it to work between two (dynamic) bodies.
Aug 2, 2010 at 7:21 AM

Anyone? I thought this was possible with F2, but I might be mistaken.

Aug 2, 2010 at 9:52 AM
Springs don't exist in FP3 like they do in FP2.x. I don't know if this is on purpose or if it just hasn't been implemented yet. You'll have to get one of the devs to confirm that. You could probably make your own mouse-like joint to do what you want. It's a fairly simple thing to do.
Aug 2, 2010 at 10:04 AM
Edited Aug 2, 2010 at 10:08 AM

Use a MaxDistanceJoint, its basically the same as the DistanceJoint but it only "pulls" (stops the 2 bodies getting further than the "maxdistance").

That should do what you want!

@Jerky: In FP3 you can make the distance joints act like springs by modifying their DampingRatio and Frequency. It's a bit of a delicate process of tweaking and depends on the timestep. The Box2D Manual explains it more.

Aug 2, 2010 at 10:44 AM

Thanks for the replies guys.

Robertdodd, I can't believe I watched over that one. I'm sure it will do exactly what I need. Thanks!

Aug 3, 2010 at 8:20 AM
I quickly changed the joint to a maxDistance one yesterday when I got home, but the anchors where completely messed up. Is there any difference between the two except for the way they apply force on the bodies?
Aug 3, 2010 at 8:26 AM

It seems the MaxDistanceJoint takes two world anchors instead of local anchors, like the DistanceJoint.

I'm actually busy making the joints more uniform right now, so there should be a new commit up soon with the changes! Thanks for pointing it out!

Aug 3, 2010 at 9:08 AM
Thanks robertdodd. I've read the manual, but the only stuff that sticks are the things I've been working on. I haven't needed springs yet, so I haven't absorbed that yet.
Aug 4, 2010 at 11:02 AM

Hi robert, sorry I looked over this reply, but thanks for looking into it!

Sep 1, 2010 at 7:11 PM

Where did the MaxDistanceJoint go in the official FP3.0 release?

Sep 2, 2010 at 8:49 PM


How can achieve the behaviour described above?