HTML and CSS Reference
In-Depth Information
var fixDef = new b2FixtureDef;
fixDef.density = ball.density;
fixDef.friction = ball.friction;
fixDef.restitution = ball.restitution;
// existing code goes here.
}
5. Instead of hardcoding the obstacle, we iterate the obstacles' list from the level
deiniion and create the obstacle:
physics.createObstacles = function(level) {
var bodyDef = new b2BodyDef;
var fixDef = new b2FixtureDef;
// default fixture
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
// obstacles
var body;
for(var i=0, len=level.obstacles.length; i<len; i++) {
var o = level.obstacles[i];
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = o.position.x/pxPerMeter;
bodyDef.position.y = o.position.y/pxPerMeter;
bodyDef.angle = o.angle;
if (o.type === 'rect') {
fixDef.shape = new b2PolygonShape();
fixDef.shape.SetAsBox(o.dimension.width/pxPerMeter,
o.dimension.height/pxPerMeter);
body = this.world.CreateBody(bodyDef);
body.CreateFixture(fixDef);
}
}
};
 
Search WWH ::




Custom Search