We should be used to the static body by now. It is time to add more types of obstacles to the physics world.
In this task, we add a cross obstacle with motor spinning.
Let's execute the following steps to create a spinning cross in the world:
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; revoluteJointDef.bodyB = circle; revoluteJointDef.collideConnected = false; revoluteJointDef.maxMotorTorque = obstacle.maxTorque; revoluteJointDef.motorSpeed = obstacle.motorSpeed; revoluteJointDef.enableMotor = obstacle.enableMotor; this.world.CreateJoint(revoluteJointDef); };
if (o.type === 'rect') { // existing rect obstacle code goes here. } else if (o.type === 'cross') { this.createCross(o); }
game.levels = [ { hoopPosition: {x:50, y:160}, ballName: 'slow ball', ballPosition: {x:350, y:250}, ballRandomRange: {x:80, y:80}, obstacles: [ { type: 'cross', graphicName: 'Cross', position: {x: 165, y: 140}, length: 60, width: 10, enableMotor: true, maxTorque: 25, motorSpeed: 3.0 }, ] }, // existing levels definition goes here. ];
The following screenshot shows the creation of the new cross obstruction:
There are several steps to create the spinning cross. First, we create the cross body with two fixtures attached to it. One object can be composed of several parts. This is the same as the fact that we can have multiple fixtures attached to one body. We have two rectangular-shaped fixtures to composite the cross body.
The cross is a dynamic body, so it can be affected by the balls motion. But it will also fall because of the gravity. So, we create another static body by using a joint and a static body. We can attach the dynamic cross to the static one. Different kinds of joints represent different types of relationships. For example, here we use a revolute joint to limit the cross to rotation. We also make it spin automatically by enabling the motor on the joint.
Then, we extract the variables into the level definition.
There are different types of joints. The following are some common joints when developing games:
You can find some joints examples in a web article by Allan Bishop at http://blog.allanbishop.com/box2d-2-1a-tutorial-part-2-joints/.
3.145.103.16