Game Development Reference
In-Depth Information
var dJoint:b2DistanceJointDef=new b2DistanceJointDef();
dJoint.bodyA=frontCart;
dJoint.bodyB=rearCart;
dJoint.localAnchorA=new b2Vec2(0,0);
dJoint.localAnchorB=new b2Vec2(0,0);
dJoint.length=100/worldScale;
var distanceJoint:b2DistanceJoint;
distanceJoint=world.CreateJoint(dJoint) as
b2DistanceJoint;
addEventListener(Event.ENTER_FRAME,updateWorld);
}
2.
Consequently, the addCart function declaration changes too, but this is
not interesting. What I would like you to notice are the lines added if motor
is true :
private function
addCart(pX:Number,pY:Number,motor:Boolean):b2Body {
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(pX/worldScale,pY/worldScale);
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);
if (motor) {
rJoint.enableMotor=true;
rJoint.maxMotorTorque=1000;
rJoint.motorSpeed=5;
}
var revoluteJoint:b2RevoluteJoint;
revoluteJoint=world.CreateJoint(rJoint) as
b2RevoluteJoint;
rJoint.bodyB=rearWheel;
 
Search WWH ::




Custom Search