Game Development Reference
In-Depth Information
There's no need to comment on the creation of the
box3
body as it's just another box,
but I am going to explain the creation of the distance joint line by line.
var dJoint:b2DistanceJointDef=new b2DistanceJointDef();
You should be used to the Box2D way to create definitions first, so here is
b2DistanceJointDef
, the definition of the distance joint.
Just like the mouse joint, distance joint has its properties to be defined, so we
are going to meet
bodyA
and
bodyB
again, this time assigning them the two
dynamic boxes:
dJoint.bodyA=box2;
dJoint.bodyB=box3;
Then we need to define the point in both bodies where the joint has to be pinned.
localAnchorA
and
localAnchorB
properties define the local points where you
apply the joints. Pay attention these points are
local
, so if we want to define the
joint at the origins of both bodies, their value will be as shown in the following
lines of code, irrespective of the position of the bodies:
dJoint.localAnchorA=new b2Vec2(0,0);
dJoint.localAnchorB=new b2Vec2(0,0);
And finally, the length of the joint, that is the fixed distance between
localAnchorA
and
localAnchorB
points. Boxes have been created at (320,430) and (420,430)
respectively, so there's already a distance of 100 pixels. We don't want to change
this value, so the
length
property will be:
dJoint.length=100/worldScale;
Now the joint definition is ready to be created in the world thanks to the
b2DistanceJoint
object—created and added to the world in the old usual way:
var distanceJoint:b2DistanceJoint;
distanceJoint=world.CreateJoint(dJoint) as b2DistanceJoint;