In the little demo earlier you saw a few rectangles and circles. Rectangles were not moving (static) and the circles were moving (dynamic). Those object in box2D are of type b2Body. A b2Body is created by the b2World not by you directly but by using the b2World method CreateBody(). A b2Body is made up of different type of b2Shape and b2Fixture. Shapes create the shape of the body while fixture are made from the shape and used in collision detection. A body can be made of one or many shapes. Creating a body can require quite a few lines of code so that’s where you want to reduce this process by creating static method or functions. Anyway let’s see how to create a simple rectangle body first:

We start as often in Box2D with a definition class which simply provides a bunch of default values:

var my_body:b2BodyDef = new b2BodyDef();

my_body.position.Set(100 / SCALE, 200 / SCALE);

my_body.angle = 15 * math.PI / 180 ;

my_body.type = b2Body.b2_dynamicBody;

we set the position of our body with the Box2D equivalent of 100 pixel on the x axis and 200 pixel on the y axis. We then give an angle in radians of course. Finally we set the type of our body. This can be static (not moving), dynamic (respond to forces) and kinematic (special type of body with unique velocity and no response to forces). Note that you can change a b2Body type at any time. You could create a static b2Body and change it to dynamic later on by calling mybody.SetType()

An important concept to not miss here is the body coordinates that we set explicitly. You will later be able to get the coordinates of that body by using the method mybody.GetPosition(). This will give you the coordinate of that body regardless of its shape and mass while calling mybody.GetWorldCenter() will give you the coordinate of the center of mass which might not be the same.

var my_box:b2PolygonShape = new b2PolygonShape();

my_box.SetAsBox(100 / 2 / SCALE, 100 / 2 / SCALE);

There are 3 type of shape you can use to build your bodies. The b2PolygonShape allows you to create easily rectangles and complex polygon using an array of vertices, the b2CircleShape creates circles, and the b2EdgeShape creates lines type of object. This last shape type is used less often and has some limitation concerning its interaction with other objects. Use it partially.

var my_fixture:b2FixtureDef = new b2FixtureDef();

my_fixture.density = 1;

my_fixture.friction = 1;

my_fixture.restitution = 0.5;


We create the fixture and pass the shape we defined and applied a few values for mass, friction and restitution. The mass value is used for computing the mass of the shape (a value of 0 creates a static body!). The friction value is used in calculating the friction between two objects, and the restitution value is used for computing the bounciness of objects.


var world_body:b2Body=world.CreateBody(my_body);


We create a b2body by passing the body definition in the CreateBody method of the b2World class. Then we create the fixture by using the b2body method CreateFixture and passing our fixture in it.

Quite a complex operation you would say to create a little rectangle. Well to be honest this is actually the short version. We could have also passed to our fixture a b2FilterData object (used to define groups of collision).

Now we only created a body with one fixture which we could call a simple body. We could as well create a body with multiple fixture which we could then call a complex body. Let’s see this in action (click anywhere):

You can at any time add new fixtures to a body or remove them.

Now to display real graphics you simply need to keep a reference to the body you create and use their values to update your graphic position. For this simple tutorial I’m just going to store my references in a Dictionary. Note that b2Body instances have a SetUserData() method where you can store any type of objects. You can use that to store a reference to your graphic. There are hundreds of way to link a b2Body reference to the graphic it represents so I’ll let you find your preferred way. I personally use specific set of classes for this task. Click anywhere.

now it’s time to see how Box2D handles collision and more important how we can find out when two bodies collide.