[SWF(backgroundColor = 0x333333)]
import com.actionsnippet.qbox.*;
// setting debug = true, will use the Box2D debug rendering
var sim:QuickBox2D=new QuickBox2D(this, {debug:false, gravityY:10});
// creates static boxes on all sides of the screen
sim.createStageWalls({lineAlpha:0, fillColor:0xFF9900});
// add 25 boxes
for (var i:int = 0; i<25; i++){
var xp:Number = 3 + (i % 5);
var yp:Number = 1 + int( i / 5);
sim.addBox({x:xp, y:yp, width:1, height:1, fillColor: i * 10 << 16, lineAlpha:0, angularDamping:5});
}
sim.addBox({x:7, y:10, width:3, height:.2, angle:-.3, density:0,lineAlpha:0, fillColor:0xFF9900});
sim.addCircle({x:3, y:14, radius:2, fillColor:0xCC0000, lineColor:0x333333});
sim.addPoly({x:13, y:5, verts:[[1,0,2,2,1,1.33],[1,0,1,1.33,0,2]], angle: .4, density:1});
// begins the simulation
sim.start();
// all non-static objects can be dragged
sim.mouseDrag();
I’ve been messing with Box2D for the past few weeks – I decided to create a library (QuickBox2D) for quick prototyping. This snippet makes use of this library, so in order to run it you’ll need to download the zip included at the end of this post…
If you don’t already know, Box2D is a great physics library created by Erin Catto. It was ported to AS3 by Matthew Bush and John Nesky. You can download the AS3 version of the library here.
Today’s snippet creates this swf (click any image to view demo):
QuickBox2D
QuickBox2D simplifies Box2D instantiation and makes it easy to skin rigid bodies with Library assets… normally you need to do something like this to create a box in Box2D:
var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.position.Set(3, 3); var boxDef:b2PolygonDef = new b2PolygonDef(); boxDef.SetAsBox(1, 1); boxDef.density = 1; var body:b2Body = w.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes();
and this doesn’t include setting up the physics world and the main loop to run the simulation… after writing a good deal of repetitive code I decided to make QuickBox2D. After writing lots of QuickBox2D style instantiation I decided to create an editor so that I could just draw circles, boxes, polygons and joints and then test and save them in real time (will be posting this editor in the near future). The next few posts will be about this mini-library and I’ll be writing up some real documentation for it… as of now, it doesn’t have any…
You can download Box2D here
You can download QuickBox2D here
and you can view some AS3 Box2D demos here






