Box2DFlashAS3 (or Box2DFlash) is a free 2D physics engine ported from Box2D C++ physics engine. It is mainly designed around rigid bodies and joints, but supports a host of other features, like raycasting, or continuous collision detection.
Sample
package { import flash.display.Sprite; import flash.events.Event; // Bring In Box2D import Box2D.Dynamics.* import Box2D.Collision.* import Box2D.Collision.Shapes.* import Box2D.Dynamics.Joints.* import Box2D.Dynamics.Contacts.* import Box2D.Common.Math.* /** * ... * @author Zach */ public class Main extends Sprite { private var world:b2World; private var timestep:Number; private var iterations:uint; private var pixelsPerMeter:Number = 30; public function Main():void { makeWorld(); makeWalls(); makeDebugDraw(); if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function makeWorld():void { // Define the gravity vector var gravity:b2Vec2 = new b2Vec2(0.0, 10.0); // Allow bodies to sleep var doSleep:Boolean = true; // Construct a world object world = new b2World(gravity, doSleep); world.SetWarmStarting(true); timestep = 1.0 / 30.0; iterations = 10; } private function makeWalls():void { // Note: I am assuming a stage size of 640x400; // These placements will put a row of boxes around an area that size. // We reuse the shape and Body Definitions. // Box2D creates a different body each time we call world.CreateBody(wallBd); var wall:b2PolygonShape= new b2PolygonShape(); var wallBd:b2BodyDef = new b2BodyDef(); var wallB:b2Body; // Left wallBd.position.Set( -95 / pixelsPerMeter, 400 / pixelsPerMeter / 2); wall.SetAsBox(100/pixelsPerMeter, 400/pixelsPerMeter/2); wallB = world.CreateBody(wallBd); // Box2D handles the creation of a new b2Body for us. wallB.CreateFixture2(wall); // Right wallBd.position.Set((640 + 95) / pixelsPerMeter, 400 / pixelsPerMeter / 2); wallB = world.CreateBody(wallBd); wallB.CreateFixture2(wall); // Top wallBd.position.Set(640 / pixelsPerMeter / 2, -95 / pixelsPerMeter); wall.SetAsBox(680/pixelsPerMeter/2, 100/pixelsPerMeter); wallB = world.CreateBody(wallBd); wallB.CreateFixture2(wall); // Bottom wallBd.position.Set(640 / pixelsPerMeter / 2, (400 + 95) / pixelsPerMeter); wallB = world.CreateBody(wallBd); wallB.CreateFixture2(wall); } private function makeDebugDraw():void { // set debug draw var debugDraw:b2DebugDraw = new b2DebugDraw(); var debugSprite:Sprite = new Sprite(); addChild(debugSprite); debugDraw.SetSprite(debugSprite); debugDraw.SetDrawScale(30.0); debugDraw.SetFillAlpha(0.3); debugDraw.SetLineThickness(1.0); debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit); world.SetDebugDraw(debugDraw); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point update() } private function update(e:Event = null):void { world.Step(timestep, iterations, iterations); world.ClearForces(); // Render world.DrawDebugData(); } } }
[…] TweetQuickBox2D is a mini-library created to work with Box2DFlashAS3, which is an AS3 port of the famous Box2D C++ physics engine. The main purpose of this library is […]