Tuesday, 9 August 2011

Add Box2D bodies in Actionscript 3

I have written some quick and dirty Actionscript 3 code which will quickly get you started adding Box2D bodies on the stage. The two functions accept parameters where you can set the x, y, bodyDef type, width, height, density, restitution and friction of an object.

package
{
 import flash.display.Sprite;
 import flash.display.StageAlign;
 import flash.display.StageScaleMode;
 import flash.events.Event;
 import Box2D.Dynamics.*;
 import Box2D.Collision.*;
 import Box2D.Collision.Shapes.*;
 import Box2D.Common.Math.*;
 
 public class AddBodiesExample extends Sprite
 {
  private const  GRAVITY_X:int = 0;
  private const  GRAVITY_Y:int = 10;
  private const  PIXEL_TO_METRE:int = 30;

  private var world:b2World;
  
  public function AddBodiesExample()
  {
   stage.scaleMode = StageScaleMode.NO_SCALE;
   stage.align = StageAlign.TOP_LEFT;

   world = new b2World(new b2Vec2(GRAVITY_X, GRAVITY_Y), true);

   debugDraw();

   addEventListener(Event.ENTER_FRAME, update);
  }
  
  private function createBox(px:int, py:int, w:Number, h:Number, bType:int, d:Number = 1, f:Number = 0.4, r:Number = 0.3):b2Body
  {
   var bodyDef:b2BodyDef = new b2BodyDef();
   bodyDef.position.Set((px + w / 2) / PIXEL_TO_METRE, (py + h / 2) / PIXEL_TO_METRE);
   bodyDef.type = bType;
   var polygonShape:b2PolygonShape = new b2PolygonShape();
   polygonShape.SetAsBox(w / 2 / PIXEL_TO_METRE, h / 2 / PIXEL_TO_METRE);
   var fixtureDef:b2FixtureDef = new b2FixtureDef();
   fixtureDef.shape = polygonShape;
   fixtureDef.restitution = r;
   fixtureDef.density = d;
   fixtureDef.friction = f;
   var body:b2Body = world.CreateBody(bodyDef);
   body.CreateFixture(fixtureDef);
   return body;
  }
  
  private function createSphere(px:int, py:int, radius:int, bType:int, d:Number = 1, f:Number = 0.4, r:Number = 0.3):b2Body
  {
   var bodyDef:b2BodyDef = new b2BodyDef();
   bodyDef.position.Set((px + radius) / PIXEL_TO_METRE, (py + radius) / PIXEL_TO_METRE);
   bodyDef.type = bType;
   var bCircle:b2CircleShape = new b2CircleShape(radius / PIXEL_TO_METRE);
   var fixtureDef:b2FixtureDef = new b2FixtureDef();
   fixtureDef.shape = bCircle;
   fixtureDef.density = d;
   fixtureDef.friction = f;
   fixtureDef.restitution = r;
   var body:b2Body = world.CreateBody(bodyDef);
   body.CreateFixture(fixtureDef);
   return body;
  }
  
  private function debugDraw():void
  {
   var debugDraw:b2DebugDraw = new b2DebugDraw();
   var debugSprite:Sprite = new Sprite();
   addChild(debugSprite);
   debugDraw.SetSprite(debugSprite);
   debugDraw.SetDrawScale(PIXEL_TO_METRE);
   debugDraw.SetFlags(b2DebugDraw.e_shapeBit);
   debugDraw.SetFillAlpha(0.5);
   world.SetDebugDraw(debugDraw);
  }
  
  private function update(e:Event):void
  {
   world.Step(1 / 30, 10, 10);
   world.ClearForces();
   world.DrawDebugData();
  }
 }
}

Here is a quick example I have created a new AS3 Flash file with the stage dimension 550 x 400 pixels. In the constructor I have added the following code.

createBox(0, 370, 550, 30, b2Body.b2_staticBody); 
createSphere(150, 130, 30, b2Body.b2_dynamicBody, 10, 0.1, 0.7); 
createSphere(240, 130, 30, b2Body.b2_dynamicBody, 3, 0.4, 0.3);
createSphere(330, 130, 30, b2Body.b2_dynamicBody, 5, 0.4, 0.4);  

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP