Game Development Reference
In-Depth Information
bodyDef.type=b2Body.b2_dynamicBody;
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(40/worldScale,20/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.density=1;
fixtureDef.restitution=0.5;
fixtureDef.friction=0.5;
var body:b2Body=world.CreateBody(bodyDef);
body.CreateFixture(fixtureDef);
var frontWheel:b2Body=addWheel(pX+20,pY+15);
var rearWheel:b2Body=addWheel(pX-20,pY+15);
var rJoint:b2RevoluteJointDef=new b2RevoluteJointDef();
rJoint.bodyA=body;
rJoint.bodyB=frontWheel;
rJoint.localAnchorA.Set(20/worldScale,15/worldScale);
rJoint.localAnchorB.Set(0,0);
var revoluteJoint:b2RevoluteJoint;
revoluteJoint=world.CreateJoint(rJoint) as b2RevoluteJoint;
rJoint.bodyB=rearWheel;
rJoint.localAnchorA.Set(-20/worldScale,15/worldScale);
revoluteJoint=world.CreateJoint(rJoint) as b2RevoluteJoint;
return body;
}
But it's not over with the code to add in the addCart function. As you can
see, I added two revolute joints pinning the origin of the each wheel to the
cart. Don't worry about collisions as bodies pinned with a revolute joint do
not collide.
12. Finally we need a distance joint to manage the truck-trailer thing, to be added
in the Main function:
public function Main() {
world=new b2World(new b2Vec2(0,5),true);
debugDraw();
ground();
var frontCart:b2Body=addCart(200,430);
var rearCart:b2Body=addCart(100,430);
var dJoint:b2DistanceJointDef=new b2DistanceJointDef();
dJoint.bodyA=frontCart;
dJoint.bodyB=rearCart;
dJoint.localAnchorA=new b2Vec2(0,0);
 
Search WWH ::




Custom Search