HTML and CSS Reference
In-Depth Information
4. Next, we code the following method to create a large body as a boundary:
physics.createWorldBoundary = function() {
var bodyDef = new b2BodyDef;
var fixDef = new b2FixtureDef;
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = -800/pxPerMeter;
bodyDef.position.y = 300/pxPerMeter;
bodyDef.angle = 0;
fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(2000/pxPerMeter, 10/pxPerMeter);
body = this.world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
body.SetUserData({isBoundary:true}); // distinguish this object
from others.
};
5. It's ime to invoke our two new methods:
physics.createLevel = function() {
this.createWorldBoundary();
this.setupContactListener();
// existing code goes here
};
6. In the contact-handling method, we add checking between the balls and boundary.
We add those ball bodies to the list:
physics.setupContactListener = function() {
// contact
var contactListener = new Box2D.Dynamics.b2ContactListener;
contactListener.BeginContact = function(contact, manifold) {
if (contact.GetFixtureA().IsSensor() || contact.GetFixtureB().
IsSensor()) {
game.increaseScore();
}
// world boundary.
var userDataA = contact.GetFixtureA().GetBody().GetUserData();
 
Search WWH ::




Custom Search