Setting UP

21
Jun
2012

Unlike Box2djs in which we had to import a lot of javascript files, in Box2dweb we have to import only ONE javascript file. You can download the package by visiting this site. After the download is complete extract files in your root folder and the following line within the "html" tag.

or
and you are ready to go.

### Importing Classes

There is no way to import packages and classes in Javascript, so we have to use var assignments, if we don't want to use full addresses. Copy and paste the following code at the starting of the script tag.
```   var b2Vec2 = Box2D.Common.Math.b2Vec2;
var b2BodyDef = Box2D.Dynamics.b2BodyDef;
var b2Body = Box2D.Dynamics.b2Body;
var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
var b2Fixture = Box2D.Dynamics.b2Fixture;
var b2World = Box2D.Dynamics.b2World;
var b2MassData = Box2D.Collision.Shapes.b2MassData;
var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
```

### Now we Should Create a world:

`var world = new b2World(new b2Vec2(0,10), true);`
With the above code we have created a Box2D world with a Gravity of 10 m/s/s in downward direction. It is not necessary in Box2D world that the gravity is always downwards. We can change the gravity by changing the vector.

NOTE: The +ve X Axis is towards "RIGHT" and +ve Y Axis is "DOWNWARDS".

The "true" next to b2Vec2(0,10) means an object in this world will fall asleep if it's not moving physically. We usually allow objects to sleep because when an object is sleeping it won't be simulated by Box2d, so that energy of your device can be saved, meanwhile it can be woken up fast and easily.

By administrator at 11:59:59 AM 0 Comment(s)