In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: Box2D test

Get Adobe Flash player
by hacker_3o7ysnb8 08 Mar 2010
    Embed
// forked from shaktool's forked from: Box2D test
package  {
  import Box2D.Collision.*;
  import Box2D.Collision.Shapes.*;
  import Box2D.Common.Math.*;
  import Box2D.Dynamics.*;
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;
  [SWF(backgroundColor=0x333333)]
  public class Box2DTutorial extends Sprite {
    private var world: b2World;
    private var boxBody: b2Body;
    
    public function Box2DTutorial(): void {
      stage.addEventListener(MouseEvent.CLICK, clickHandler);
     
    var tf:TextField = new TextField(); 
     addChild(tf);
    tf.text = ""+ stage.frameRate;
      // Set the world boundaries:
      var worldAABB: b2AABB = new b2AABB();
      worldAABB.lowerBound.Set(-100, -100);
      worldAABB.upperBound.Set(100, 100);
      
      // Set the rate of acceleration due to gravity:
      var gravity: b2Vec2 = new b2Vec2(0, 10);
      
      // Optionally stop simulating inactive bodies:
      var doSleep: Boolean = true;
      
      // Create a world to contain the bodies:
      world = new b2World(worldAABB, gravity, doSleep);
      
      
      // Create a body for the floor:
      var floorBodyDef: b2BodyDef = new b2BodyDef();
      floorBodyDef.position.Set(2.5, 3);
      var floorBody: b2Body = world.CreateBody(floorBodyDef);
      
      // Create a shape for the floor:
      var floorShapeDef: b2PolygonDef = new b2PolygonDef();
      floorShapeDef.SetAsBox(2, 0.1);
      floorBody.CreateShape(floorShapeDef);
      
      // Create a body for the box:
      var boxBodyDef: b2BodyDef = new b2BodyDef();
      boxBodyDef.position.Set(2.5, 1);
      boxBody = world.CreateBody(boxBodyDef);
      
      // Create a shape for the box:
      var boxShapeDef: b2PolygonDef= new b2PolygonDef();
      boxShapeDef.SetAsOrientedBox(0.3, 0.2, new b2Vec2(0, 0), 0.8);
     // boxShapeDef.restitution = 0.4;
      
      // Make sure that the box has mass, so that it will be dynamic:
      boxShapeDef.density = 2;
      boxBody.CreateShape(boxShapeDef);
      boxBody.SetMassFromShapes();
      
      // Use Box2D's internal rendering engine to display the simulation for now.
      var debugDraw: b2DebugDraw = new b2DebugDraw();
      debugDraw.m_sprite = this; // = an empty container on the display list
      debugDraw.m_drawScale = 100; // 100 pixels per meter
      debugDraw.m_drawFlags = b2DebugDraw.e_shapeBit; // draw shapes
      world.SetDebugDraw(debugDraw);

      
      addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    }
    
    private function clickHandler(event:Event):void {
      boxBody.ApplyImpulse(new b2Vec2(0,-3), boxBody.GetWorldCenter());
    }
    
    private function enterFrameHandler(event:Event):void {
      // The time passed, or the inverse of the framerate:
      var timeStep: Number = 1/30;
      // The quality of the simulation:
      var iterations: int = 10;
      world.Step(timeStep, iterations);
    }
  }
}