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.
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);