Hello again, this is the first lesson for teaching Box2D. Don’t worry is not so hard, and if you realy like physics, is hard not to like what Box2D can do. You need to know that Box2D is in origin a Physics Engine for C++ programing. So you will see that are a lot of differences betwen writing code in pure AS 3.0 for a real object move, and writing code for a real object move in Box2D. But, let’s stop talking, and see how this engine works.
If we want to do an object that moves, we first need to create a World, is exactly like in our lifes. This World need to have gravity and need to know if he will igone or not static bodies. We will see later how will this look in a code.
Like I said this engine works like the real word, so he don’t work in PIXEL(this is very important, for you to know), this engine works in METERS. Again this is very important, you will see later how to make an application on a monitor that works in pixels.
In Box2D of course we will work with objects. Let’s see how we will make an object:
Those are only the basic things that we will use, but are enough for making an object. In future lessons we will talk more about other interesting things that you will learn.
After you read this, you are ready to understand how this application works. For this first lesson I made a very very very simple application. We will make an object and put him to fall into space with a gravity of 9.8 meters per seccond.
You won’t be able to see anything graphic for this project, you will be only able to see Y coordinate for our block that will fall down. I know that this is not very fun for you, and I’m sorry for that, but I promise that in the next lesson , I will show you how to make a graphic for your object.
Code:
package
{
import Box2D.Collision.Shapes.b2PolygonShape;
import Box2D.Common.Math.b2Vec2;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2FixtureDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
private var _world:b2World;
private var _block:b2Body;
private static const RATIO:Number = 30;
public function Main()
{
//// Set up the world
createWorld();
//Create the Block
createB();
// Give this World some life
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function createWorld():void
{
// Set up the gravity
var gravity:b2Vec2 = new b2Vec2(0 , 9.8);
// Tell the world to ignore sleeping objects
var doSleep:Boolean = true;
_world = new b2World(gravity, doSleep);
}
private function createB():void
{
var blockDef:b2BodyDef;
var blockShape:b2PolygonShape;
blockDef = new b2BodyDef();
blockDef.position.Set(300 / RATIO, 0 / RATIO);
blockDef.type = b2Body.b2_dynamicBody;
blockShape = new b2PolygonShape();
blockShape.SetAsBox(20 / RATIO,25 / RATIO);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = blockShape;
fixtureDef.density = 1.0;
fixtureDef.friction = 0.8;
fixtureDef.restitution = 0.3;
_block = _world.CreateBody(blockDef);
_block.CreateFixture(fixtureDef);
}
private function onEnterFrame(e:Event):void
{
_world.Step(10,10,10);
_world.ClearForces();
trace(_block.GetPosition().y);
}
}
}
Let’s take a look again in this code, we have 3 private variables. The first one represents the World, the second one the body, and the last one help as to convert pixels to meters. As I said at the begining, Box2D works in meters.
After that I create the world function -createWorld- . I think this is clear I give the world a gravity of 9.8 on Y, and I set the world to ignore sleeping objects. On the -createB- function, I creat our body, I give him a position:
blockDef.position.Set(300 / RATIO, 0 / RATIO);
and a form (Width and Hight):
blockShape.SetAsBox(20 / RATIO,25 / RATIO);
Now, let’s see if your block is falling down or not. As you can see I have a -trace- statement at the end of the code, that will show as the Y coordinate for our block. Let’s test this and see what kind of result we will obtain.

As you can see our result is exactly as I expected. As you can see, my block is falling. And he will not stop because in this world that we created we don’t have any other objects that can interact with him. So, because of that he will fall continuously.
That was the first lesson about how to get started with Box2D. I hope you enjoy this, and see you soon at the next tutorials. THANKS.
Box2D 2.1a Lesson 2(Tutorial)