Creating a b2Joint requires sometime quite a lot of repetitive code so just like when creating a b2Body you might want to wrap that up into classes or static methods in order to cut down the amount of code you write. You can of course combine b2Joint together to achieve a complex system of mechanic. Be aware that creating a non logical situation can cause a Flash Player crash so be sure you know what you are doing. B2Joint can also break if you apply to them the right amount of force.

Most joint creation use a common pattern:
. A b2Body reference.
. The b2Body target of the joint.
. the position where the joint will be attached.

The b2Body reference is the b2Body where you want to attach the joint.
The target b2Body is the body the joint will influence.
The position is an absolute position (x,y) that will indicate where you want to attach the joint in relation to the reference joint. Even though we use absolute position here it will end up being a relative positioning to the reference b2Body. As I said this is true for most joint creation so that also means not all of them use that pattern. Don’t worry, we will look at all of them.

So let’s start with a simple b2Joint, the b2WeldJoint. This joint simply glue two b2Body together. You could use this b2Joint to construct a complex object and upon the right condition you would destroy the b2Joint so the complex object fall apart. Be aware that that joint is quite sensitive to force constraint so don’t use it if you want two b2Body to be very strongly glued together. In this next example I built a little bridge and simply let objects fall upon it. As the weight increases on the bridge you will start to see it move and even bend. That is because a lot of constraint is imposed on the weld joint themselves. Finally when the last object falls I destroy all joints and let the poor bridge crash down gracefully. Click anywhere to start the demo:

The bridge was made up of 5 graphic/b2Body and 4 joints and created by calling a createWeldJoint() method. Check it out in the document class but here is the relevant part:

var jointdef:b2WeldJointDef = new b2WeldJointDef();

jointdef.Initialize(bridgebox, bridgeboxtwo, new b2Vec2(350 / SCALE, 350 / SCALE));

var joint:b2WeldJoint = world.CreateJoint(jointdef as b2JointDef) as b2WeldJoint;

First we created a b2WeldJointDef (every single joint type has a Def type of class we use for creating them) just like we do when creating a b2Body. Then we call Initialize() (here again we will do this on most joint type). We pass the two b2Body we want to weld together and an anchor point (b2Vec2). Most joint definition class have an Initialize() method we need to call. Finally we create our joint by calling world.CreateJoint() and our joint is up and working. As I said this one is quite simple but there’s really so many interesting thing that can be created with it. Experiment on your own and let’s move onto another simple one.