Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to implement something that behaves much like a distance joint / revolute joint in Box2D, however I do not want to throw a whole physics engine at this problem. I'm curious where I might start research to implementing such a thing.

Currently I only know them by their Box2D names, so googling that leads of course back to Box2D, so even that would be helpful.

share|improve this question
You might want to consider just using box2D for now an evaluate how much of an issue this really is at the end of your project. – OriginalDaemon Nov 16 '12 at 10:43
That is of course definitely an option. However like I said I as a coder would prefer to implement this on my own if only as a learning exercise. – onedayitwillmake Nov 16 '12 at 17:19
up vote 1 down vote accepted

The complexity of doing this in a physics solver comes from trying to do it in a way that is both numerically stable (doesn't freak out and send your rigid bodies flying) and that conserves energy.

Erin Catto (the author of Box2d) has several GDC presentations that show how to do this with impulses so you wouldn't need an entire physics solver to make it happen.

share|improve this answer

Implementing any constraint in an impulse/velocity solver comes down to finding the relative velocity between the two connected bodies that you want to remove.

In the case of the distance constraint, this relative velocity is always in the direction between the constraint anchor points.

Vector between the anchor points

If you get the relative velocity of the two bodies, project it onto the normalised vector between the anchor points, you'll have the velocity that you want to remove.

Velocity of each body projected onto the vector between anchor points

Then, all you'll need is an equation which lets you remove velocity by calculating an impulse. Plug in the velocity to remove and out pops an impulse which you can then apply equally and opposite to both rigid bodies.

If you'd like to read more about this process, I wrote an article a while back which covers designing a constraint in a impulse/velocity solver:

Hope it helps!

share|improve this answer
In your equation: I = (1+e)*N*(Vr • N) / (1/Ma + 1/Mb) (from the article you linked), what does each identifier represent? – theonlygusti Aug 26 '15 at 21:06

Why don't you take a look at the Box2D Source. In there you'll be able to see how the distance joint works. It might be a bit complex, but the part I would start with is "solveVelocityConstraints" in the b2DistanceJoint.

If all you are looking for is a very simplistic implementation then you could try this;

  • Find the distance between ObjectA and ObjectB
  • Find the difference the distance and the desired rest distance.
  • Apply a force, proportional to this difference value, to each body, in the direction of the other body.

It might look similar to the following for a basic implementation (disclaimer, I don't have the ability to test this right now).

Vec3 dir = ObjectB.position - ObjectA.position;
float mag = (dir.length - targetLength) * 0.5f;
ObjectA.velocity += dir * -diff;
ObjectB.velocity += dir * diff;

Keep in mind this is a very simplistic implementation and is potentially unstable. Also keep in mind that this would never let the body "rest", so the two objects would be constantly moving towards and then away from each other, although it might be too small to see. This is why you would be best either using box2D or working out how it works and then replicating it.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.