HTML and CSS Reference
In-Depth Information
Engage thrusters
Let's add the sensor and handle the collision in the following steps:
1. First, we append the createHoop method to create a body between the two
litle hoop squares. We set it as a sensor so that the balls can pass through it:
physics.createHoop = function() {
// existing hoop code goes here.
// hoop sensor
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = (hoopX+20)/pxPerMeter;
bodyDef.position.y = hoopY/pxPerMeter;
bodyDef.angle = 0;
fixDef.isSensor = true;
fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(20/pxPerMeter, 3/pxPerMeter);
body = this.world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
};
2. Then, we set up the contact listener in the following method deiniion. It registers
out its own beginContact method to the Box2D engine:
physics.setupContactListener = function() {
// contact
var contactListener = new Box2D.Dynamics.b2ContactListener;
contactListener.BeginContact = function(contact, manifold) {
if (contact.GetFixtureA().IsSensor() || contact.GetFixtureB().
IsSensor()) {
game.increaseScore();
}
};
this.world.SetContactListener(contactListener);
};
3. Besides detecion collision between the hoop and ball, we will also detect the
collision between the balls and world boundary. We remove the balls that hit
the boundary. To remove bodies in the physics world, we first need to store the
reference of the bodies that we are going to remove. So, we define an array in
the physics object:
physics.bodiesToRemove = [];
 
Search WWH ::




Custom Search