Box2Dwrapper – Box2DFlashAS3 made simple!
Hi guys:
So, about a week ago, a friend of mine asked me if I could give him a quick explanation as to how to use/setup Box2DFlashAS3. Honestly, I didn’t have the time as I was swamped with work, so I directed him over to Emanuele’s site which is chock full on Box2DFlashAS3 tutorials. After going through his posts for a couple days, my buddy said that he was able to figure it out but that the syntax was ridiculous! I don’t blame him either; the syntax is very unorthodox for a developer that has only develops in AS3.
That got me to thinking and I decided to create a wrapper class to try and make it simpler to use. Now, keep in mind that I really never dug into this particular physics engine. I quickly saw that a simple wrapper class was not going to be enough, but I started anyway and ended up with the Box2Dwrapper library.
The Box2Dwrapper library is modeled after Papervision3D’s BasicView and is extremely easy to use. Please keep in mind that the library is still under development and probably will be for some time since I will be busy with JigLibFlash refactoring and other work, never the less, I will update it often so keep tuned.
Enough gibberish, on to the code! Remember, it’s still in its infant stage and undocumented, but I’m still gonna let you guys check it out.
This is how simple it is to use:
{
import com.box2dwrapper.object.Box;
import com.box2dwrapper.object.Circle;
import com.box2dwrapper.view.Box2DView;
public class Box2Dwrapper_Test extends Box2DView
{
public function Box2Dwrapper_Test()
{
super(2000, 2000, 0, 9.8, true);
showDebug = true;
allowDragging = true;
initialize();
}
private function initialize():void
{
createFloor();
createFace();
startPhysicsRender();
}
private function createFloor():void
{
var floor:Box = new Box();
floor.position(stage.stageWidth * 0.5, stage.stageHeight);
floor.scale(stage.stageWidth * 0.5, 10);
floor.type = "static";
addBody(floor);
}
private function createFace():void
{
var face:Circle = new Circle();
face.radius = 20;
face.position(stage.stageWidth * 0.5, 25);
face.type = "dynamic";
face.restitution = 0.5;
addBody(face);
}
}
}
IN THE CONSTRUCTOR
- The Box2DView class accepts 4 parameters passed in via the “super” method. They are:
- world width
- world height
- horizontal force
- vertical force
- allow sleep
- showDebug (internal) allows you to see the debug drawn physics objects.
- allowDragging (internal) makes the objects automatically draggable.
IN THE INITIALIZE METHOD
- startPhysicsRender() (internal) starts the render ticker.
IN THE createFloor and createFace METHODS
Here you can see how easy it is to create a box/circle with physics properties. Definitely a syntax we can relate to. Here are their properties and methods:
- radius (circle only) sets the radius
- density sets the mass
- restitution sets the elasticity
- friction sets the friction
- type can be either “static” or “dynamic”
- skin sets a display object from the library as a material, for example circle.skin = new Face();
- scale(width, height) sets the width and height
- position(x, y) sets the position
There are other properties and methods that are in the process of integration and that will be available in a future release. In the meantime, you guys can take the library as is, add, edit, remove stuff, dissect it as you wish. Just don’t claim it as yours
You can grab the code from the Google repository here :
http://box2dwrapper.googlecode.com/svn/trunk/

Recent Comments