So far I kept all type of joint into its own section but here the b2LineJoint (new to 2.1) and b2PrismaticJoint are so similar that I thought it would be better to keep them together. They are so similar in fact that you create them exactly the same way and with the same type of values. So what’s the difference? Well with the b2LineJoint the b2Body target of the joint can still move freely on its own while with the b2PrismaticJoint the b2Body target is tied to its joint and the joint axis. We’ll take a look shortly so we can see the difference but first let me tell you what they do.
Those two joint allow a b2Body to translate along an angle. You can also set a translation range (minimum, maximum) and finally you can power them with a motor. Think about the moving platform in the old 2D Super Mario games. You can set them either vertical or horizontal or at any angle you want. Perfect for creating elevators, carriers, suspension system, pumps and so on.

In this next demo I set a b2lineJoint on the left and a b2PrismaticJoint on the right. The goal being to reach the center top platform with the buggy. Click on the green arrows to make the two joint go up or down. You can control the buggy with the arrow keys on your keyboard:


If you tried to make both joint carry the buggy I’m guessing you experienced a little problem. With the b2Prismaticjoint the platform stay still while going up just like an elevator but with the b2LineJoint the platform starts rotating and it’s nearly impossible to keep the buggy on it.
Both joint translate a b2Body along an axis but with the b2PrismaticJoint the b2Body is tied to the joint and so cannot rotate or interact with the Box2D world like a free b2Body would. With the b2LineJoint the b2Body is still free to rotate and interact with the Box2D world.

Relevant code (remember b2LineJoint and b2PrismaticJoint are created the same way):

var linedef:b2LineJointDef = new b2LineJointDef();

linedef.Initialize(axis, lineplatform, lineplatform.GetPosition(), new b2Vec2(0, 1));

linedef.enableLimit = true;

linedef.enableMotor = true;

linedef.lowerTranslation = 0;

linedef.maxMotorForce = 100;

linedef.motorSpeed = 0;

linedef.upperTranslation = 270 / SCALE;

linejoint = world.CreateJoint(linedef as b2JointDef) as b2LineJoint;

The initialize method is particular here. I created a special axis b2Body and passed it as the reference b2Body (I could have used world.getGroundBody()). Next I pass the platform b2Body which is the one the buggy is supposed to stand on. I then set the anchor to the coordinate of that b2Body. Finally last parameter is a vector (notice we don’t use / SCALE here) and will set the angle at which the translation occurs. You have seen most other values before. We enable range limit, enable motor, set the lower range to 0 (starting point), the upper range to 270 (reaching the ground). A torque of 100 so we can carry some weight. We simply set the joint motor speed when clicking on the green arrows. An operation as simple as that :

linejoint.SetMotorSpeed( -1);

prismjoint.SetMotorSpeed(-1);

Negative value to go up and positive to go down.

Now to make a platform go horizontal you would pass a vector like this:
new b2Vec2(1, 0)

Also to make a platform go back and forth (there are about dozens of ways to do it) you could set a number variable to keep track of the platform position and once the platform stops moving it is time to reverse the speed:

linedef.motorSpeed = 1;//start with some speed

jointposition = 0;//keep track of platform position

Then in the update method after the world.step():

if (jointposition == linejoint.GetJointTranslation())//not moving?

{

    linejoint.SetMotorSpeed(linejoint.GetMotorSpeed() * -1);//reverse the speed

}

else

{

    jointposition = linejoint.GetJointTranslation();//it's moving so store the position

}

That’s it about those two joints now let’s play with the b2PulleyJoint joint.