You are Here:Home>>Old Posts>>Using Box2DFlashAS3 (AS3 physics engine) Part 3 – Learn more about body object

Using Box2DFlashAS3 (AS3 physics engine) Part 3 – Learn more about body object

Carlos Pinho
By | 2009-05-21T03:40:05+00:00 May 21, 2009|Old Posts|

In the previous tutorial, i have do an basic example. You should learn the basic step to start using Box2D in flash. In the following tutorials, i hope you will get more familiarize with using Box2D. First, i will talk about the basic element in Box2d – body.

What is body (rigid body)?

Yes, like you and me, every human is a body in the world. Also, a plant, an animal, a television, all is a body. For a simple definition, a body is a individual element or object which exist in the world with different shape, mass, or other properties. In the box2D world, it is the same. Every graphics, button, etc. can be a body.


To create a body in Box2DFlashAS3

Before start, you should know some basic class related to creating a body.

b2BodyDef –
body definition class. It is a data class use to hold the data which describe the mass and position of the body.

b2CircleDef, b2PolygonDef – body shape definition class. It is a data class use to hold the data to describe the shape and some physical properties of the body.

b2Body – body class. It is the class for creating the body instance actually use for simulation.

1. Create body definition object

Body definition object is use to hold the definition of a body object. It is use as a data object to define the properties of the body. When using the body definition to create a body object, the data will copy from the body definition to the body instance. So, the definition object can be reuse after creating the body object instance.

To create a body definition, you can use the code below.

var bodyDef:b2BodyDef = new b2BodyDef();

Afterward, you need to set the display object which this definition refer to. Here i have a ball shape movie clip called ball1_mc. Later, we can loop though body object inside the world and get back the displayb object reference by the m_userData property of body object. = ball1_mc;

Because the simluator need to know the init position of the body object, we need to set it by following code. I simply use the current position of the display object by refer to x and y property of the display object. So, we don’t need to change the code if we change the position of the display object.

bodyDef.position.Set(ball1_mc.x, ball1_mc.y);

Each body will have mass property, you can set the mass by following code. It is in kg.

bodyDef.massData.mass = 1.0;

Normally, we don’t set the mass directly. The mass will be calculate by the size of the display object. This help to maintain the mass to size relationship. For example, we have several ball object and they have different size. If we set the mass individually, the simulation may seem wired if we not set the mass properly. So, we should set the mass base of the size of each ball. You will learn how to set the mass base on the shape in later step.


2. Create Shape definition

Define Shape

The shape of the body object will affect the behavior in simulation. So, we need to define the shape of the body object. Box2d provide 2 type of shape – circle and polygon. We can use b2CircleDef and b2PolygonDef class to create the definition.

Circle Shape

var groundDefinition:b2PolygonDef = new b2PolygonDef();

Polygon Shape

var ballDefinition = new b2CircleDef();

Set Size

We need to define the size of the shape. For a box shape, we can use the following method of the b2PolygonDef. The agreement is the half width and height of the box.

groundDefinition.SetAsBox(230, 24);

For a circle shape, we can use the radius property to set the size of the circle shape.

ballDefinition.radius = 33;

Set Friction

Friction is used to make objects slide along each other realistically. Box2D supports static and dynamic friction, but uses the same parameter for both. Friction is simulated accurately in Box2D and the friction strength is proportional to the normal force (this is called Coulomb friction). The friction parameter is usually set between 0 and 1. A value of zero turns off friction and a value of one makes the friction strong. When the friction is computed between two shapes, Box2D must combine the friction parameters of the two shapes. This is done with the following formula:

groundDefinition.friction = .5;

Restitution is used to make objects bounce. The restitution value is usually set to be between 0 and 1. Consider dropping a ball on a table. A value of zero means the ball won’t bounce. This is called an inelastic collision. A value of one means the ball’s velocity will be exactly reflected. This is called a perfectly elastic collision. Restitution is combined using the following formula.

ballDefinition.restitution = 0.5;

Set Density

Higher density means the object is heavier (large mass value) if they are in same size. We can set the density by the following code. If density equal to zero, that means the object is unmovable.

ballDefinition.density = 1.0;

It is more simple to maintain same density to same type of object and calculate the mass by the size of the shape.

3. Create body instance

Last, we need to create the actual body object use for simulation.

We construct a body object from the body definition and add to the world object for simulation by following code.

var ball1Body:b2Body = world.CreateBody(ball1BodyDefinition);

Then attach the shape to the body object.


Now, we can calculate the mass by calling the method SetMassFromShapes(). This will set the mass property of the body object base on the size of the shape of defined in the shape definition.



You should now familiarize with the body object in Box2D. In next tutorial, i will talk about world object and adding force in the world.

About the Author:

Carlos Pinho
A father, a husband and a geek... Carlos was the founder of projects like The Tech Labs and Flash Enabled Blog. He is the founder of TekTuts He is passionate about technologies. Their main skills are in analytics, transport & logistics, business administration. He also writes about programming resources, trends, strategy and web development.