AGE - Box2D support  

  AGE  Haxe  NME   Nov 10, 2012

Since Box2D is one of the most used physics engine, I’ve started to implement it into the AGE game engine.

I’ve used the box2d version from haxelib, so don’t forget to add the following to your nmml file :

<haxelib name="box2d" />

I’ve implemented it with the behaviors system. So now we have a Box2dEntity that has a special Behavior (Box2dMovementBehavior) which initialize and update all the Box2D stuff.

For example, to define an entity :

import com.revolugame.age.display.Box2dEntity;

class Block extends Box2dEntity
{
    public function new (pX: Int, pY: Int)
    {
        super(pX, pY);
        makeGraphic(32, 32, 0xFF000000);
        initBox2dStuff(30, true, 0, 0, 0);
//                                     ^ friction
//                                  ^ restitution
//                               ^ density
//                          ^ if the entity is dynamic
//                     ^ conversion meters to pixels
    }
}
add(new Block(10, 50));

And that’s it !

The Box2D support works with Flash and C++ but needs some optimization for now.

Here is a quick example (click on the blocks to remove them) :

You can also apply a force or an impulse on your entity :

_b2dBehavior.applyImpulse(1, 2); // x, y
_b2dBehavior.applyForce(1, 2); // x, y

Another example (click on the oranges to apply a random impulse) :

For more information on Box2D, check out the flash documentation.

Share it