Adding a cross obstacle

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.

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 fixtures in one body and then a static 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;
      revoluteJointDef.bodyB = circle;
      revoluteJointDef.collideConnected = false;
    
      revoluteJointDef.maxMotorTorque = obstacle.maxTorque;
      revoluteJointDef.motorSpeed = obstacle.motorSpeed;
      revoluteJointDef.enableMotor = obstacle.enableMotor;
    
      this.world.CreateJoint(revoluteJointDef);
    };
  2. Then, we update the level creation code to take care of the new cross obstacle:
    if (o.type === 'rect') {
      // existing rect obstacle code goes here.
    } else if (o.type === 'cross') {
      this.createCross(o);
    }
  3. We define a new level with our new cross obstacle. Let's add the following level definition into the levels array:
    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:

Engage thrusters

Objective complete – mini debriefing

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.

Classified intel

There are different types of joints. The following are some common joints when developing games:

  • The revolute joint
  • The distance joint
  • The prismatic joint
  • The pulley joint

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/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.103.16