HTML and CSS Reference
In-Depth Information
Engage thrusters
The following steps create the physics world as the foundaion of the game:
1. The Box2D classes are put in different modules. We will need to reference some
common classes as we go along. We use the following code to create an alias for
these Box2D classes:
// alias
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2AABB = Box2D.Collision.b2AABB
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2MassData = Box2D.Collision.Shapes.b2MassData
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2DebugDraw = Box2D.Dynamics.b2DebugDraw
, b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef
, b2RevoluteJointDef = Box2D.Dynamics.Joints.b2RevoluteJointDef
;
2. We prepare a variable that states how many pixels define 1 meter in the physics
world. We also define a Boolean to determine if we need to draw the debug draw:
var pxPerMeter = 30; // 30 pixels = 1 meter. Box3D uses meters and
we use pixels.
var shouldDrawDebug = false;
3. All the physics methods will be put into the game.physics object. We create this
literal object before we code our logics:
var physics = game.physics = {};
4. The first method in the physics object creates the world:
physics.createWorld = function() {
var gravity = new b2Vec2(0, 9.8);
this.world = new b2World(gravity, /*allow sleep= */ true);
// create two temoporary bodies
var bodyDef = new b2BodyDef;
var fixDef = new b2FixtureDef;
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = 100/pxPerMeter;
 
Search WWH ::




Custom Search