Game Development Reference
In-Depth Information
Keeping bodies at a given distance -
distance joints
A distance joint is probably the easiest joint to understand and handle. It just sets the
distance between two points of two objects, and it will remain constant, no matter
what happens.
So we are about to create a new box and link it with the distance joint to the one
already in the world. Time to change a bit in our Main function:
public function Main() {
world=new b2World(new b2Vec2(0,9.81),true);
debugDraw();
var bodyDef:b2BodyDef=new b2BodyDef();
bodyDef.position.Set(320/worldScale,470/worldScale);
var polygonShape:b2PolygonShape=new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale,10/worldScale);
var fixtureDef:b2FixtureDef=new b2FixtureDef();
fixtureDef.shape=polygonShape;
var groundBody:b2Body=world.CreateBody(bodyDef);
groundBody.CreateFixture(fixtureDef);
bodyDef.position.Set(320/worldScale,430/worldScale);
bodyDef.type=b2Body.b2_dynamicBody;
polygonShape.SetAsBox(30/worldScale,30/worldScale);
fixtureDef.density=1;
fixtureDef.friction=0.5;
fixtureDef.restitution=0.2;
var box2:b2Body=world.CreateBody(bodyDef);
box2.CreateFixture(fixtureDef);
bodyDef.position.Set(420/worldScale,430/worldScale);
var box3:b2Body=world.CreateBody(bodyDef);
box3.CreateFixture(fixtureDef);
var dJoint:b2DistanceJointDef=new b2DistanceJointDef();
dJoint.bodyA=box2;
dJoint.bodyB=box3;
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);
stage.addEventListener(MouseEvent.MOUSE_DOWN,createJoint);
}
 
Search WWH ::




Custom Search