HTML and CSS Reference
In-Depth Information
Adding a cross obstacle
We should be used to the staic body by now. It is ime to add more types of obstacles to the
physics world.
In this task, we add a cross obstacle with motor spinning.
Engage thrusters
Let's execute the following steps to create a spinning cross in the world:
1. First, we define a new method that creates a cross. It is a long method. A cross is
constructed with two ixtures in one body and then a staic body with a revolute
joint to spin the cross:
physics.createCross = function(obstacle) {
var bodyDef = new b2BodyDef;
var fixDef = new b2FixtureDef;
// default fixture
fixDef.density = 0.2;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.position.x = obstacle.position.x/pxPerMeter;
bodyDef.position.y = obstacle.position.y/pxPerMeter;
fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(obstacle.length/pxPerMeter, obstacle.
width/pxPerMeter);
var cross = this.world.CreateBody(bodyDef);
cross.CreateFixture(fixDef);
fixDef.shape.SetAsBox(obstacle.width/pxPerMeter, obstacle.
length/pxPerMeter);
cross.CreateFixture(fixDef);
// a circle as the spinning joint
bodyDef.type = b2Body.b2_staticBody;
fixDef.shape = new b2CircleShape(10/pxPerMeter);
var circle = this.world.CreateBody(bodyDef);
circle.CreateFixture(fixDef);
var revoluteJointDef = new b2RevoluteJointDef;
revoluteJointDef.bodyA = cross;
 
Search WWH ::




Custom Search