Game Development Reference
In-Depth Information
12. And finally, let's create the joint. We need to work on the queryCallback
function once we know the player is trying to pick a dynamic body.
private function queryCallback(fixture:b2Fixture):Boolean {
var touchedBody:b2Body=fixture.GetBody();
if (touchedBody.GetType()==b2Body.b2_dynamicBody) {
var jointDef:b2MouseJointDef=new b2MouseJointDef();
jointDef.bodyA=world.GetGroundBody();
jointDef.bodyB=touchedBody;
jointDef.target=mouseToWorld();
jointDef.maxForce=1000*touchedBody.GetMass();
mouseJoint=world.CreateJoint(jointDef) as b2MouseJoint;
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveJoint);
stage.addEventListener(MouseEvent.MOUSE_UP,killJoint);
}
return false;
}
This is some new code, so let's dissect it line by line:
var jointDef:b2MouseJointDef=new b2MouseJointDef();
We are creating a b2MouseJointDef object. In the end, creating joints is not
that different from creating bodies. In both cases we have a definition with
all tuning parameters, and the body/joint itself. So what we just did is the
creation of the joint definition.
Now we need to specify some joint parameters, such as the bodies linked by
the joint.
jointDef.bodyA=world.GetGroundBody();
jointDef.bodyB=touchedBody;
bodyA and bodyB properties specify the bodies linked by the joint. As we are
going to link a body with the mouse, bodyA will be the ground body . The
ground body is not the static box we are using as the floor, but an invisible,
untouchable body which represents the world itself. In the real world, the
ground body is the air which surrounds us. It's everywhere but we can't see
it and we can't touch it.
On the other hand, bodyB is the body we just clicked, so we are going to say
"the clicked body will be pinned to the world, in a given point". Which point?
Mouse pointer coordinates of course, thanks to the target property, which
allows us to specify such coordinates as a b2Vec2 object, and this is where
the mouseToWorld function comes into play:
jointDef.target=mouseToWorld();
 
Search WWH ::




Custom Search