Box2D Joints #1 – Distance Joints

Joints are one of the most interesting parts of Box2D. Over the next several weeks I will attempt to explain all of the Box2D joint types and possibly add a new one.

Before You Start
I have switched over to pixels as my primary unit for working with Box2D. This was done by changing the scale factor and it impacts defining joints because they use N•m as their units. So now the values used in joints will need to be 30x bigger. Nothing major but if you use the same values as the test bed examples your joint won’t work properly. This will be more important with other joint types.

Distance Joints
A distance joint is the simplest of the joints to use. As the name implies it links two bodies together with a rigid connection that maintains the distance between the bodies. Each body is free to rotate and the collideConnected property of the inherited b2JointDef controls whether the bodies collide. The joint itself will not collide with other objects. As with most things in Box2D the vectors that specify the connection points are relative to the world and not the bodies. Example:

//Create a new distance joint definition
var djd:b2DistanceJointDef = new b2DistanceJointDef();
//Initialize the joint between two bodies. The b2Vec2() objects are used to define the location of the joint ends. Values are sample only.
djd.Initialize(body1, body2, new b2Vec2(0,0),new b2Vec2(0,30));
//Define whether the two objects will be allowed to collide with each other.
djd.collideConnected = true;
var distJoint:b2DistanceJoint = m_world.CreateJoint(djd);


Don’t have flash? Get it

One Response to “Box2D Joints #1 – Distance Joints”

  1. luke Says:

    More, more! This is one of the most helpful tutorials on Box 2d out there right now.

    What would be ideal is if you could provide the source files for this, and update it so it works in the 2.0 version of Box2d.

    I’m still struggling with a basic Mouse Joint, so if you could do a tutorial on this with source files that’d be fantastic.

    Also, I guess if you posted your source code that would answer it, but how do you make the box clickable/draggable. I assume it’s AS3 code plus some update function to let Box2d know what’s going on….