Box2D Test

by designquest
♥0 | Line 88 | Modified 2009-08-17 01:01:18 | MIT License
play

ActionScript3 source code

/**
 * Copyright designquest ( http://wonderfl.net/user/designquest )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1Xz5
 */

package{

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import Box2D.Dynamics.*;
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;

    [SWF(backgroundColor="#000000", frameRate="100")]

	
	public class test1 extends Sprite{
		
		public var m_world:b2World;
		public var m_iterations:int = 5;
		public var m_timeStep:Number = 1.0/30.0;
		public static const RATIO:int = 30;
		public var dbgSprite:Sprite;// Sprite of Debug Draw
		
		public function test1(){

			buildWorld();
			buildGround(3,10,14,0.5,0.3);
			buildGround(17,10,14,0.5,-0.3);
			debugDraw();
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, addObject);
			addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
		}
		
		/*	A physics world is a collection of bodies, shapes, and constraints 
			that interact together. Box2D supports the creation of multiple worlds, 
			but this is usually not necessary or desirable.
		*/
		private function buildWorld():void {
			// Creat world AABB
			var worldAABB:b2AABB = new b2AABB();
			worldAABB.lowerBound.Set(-1, -1);
			worldAABB.upperBound.Set(stage.stageWidth/RATIO+1, stage.stageHeight/RATIO+1);
			
			// Define the gravity vector
			var gravity:b2Vec2 = new b2Vec2(0, 5.0);
			
			// Allow bodies to sleep
			var doSleep:Boolean = true;
			
			// Construct a world object
			m_world = new b2World(worldAABB, gravity, doSleep);
		}
		
		/*	Bodies are built using the following steps:
			1) Define a body with a position, damping, etc.
			2) Use the world object to create the body.
			3) Define shapes with geometry, friction, density, etc.
			4) Create shapes on the body.
			5) Optionally adjust the body's mass to match the attached shapes.
		*/
		private function buildGround(Gx:Number,Gy:Number,Gw:Number,Gh:Number,Ga:Number):void {
			var ground:b2Body;
			var groundDef:b2BodyDef;
			var boxDef:b2PolygonDef;
						
			// Define a body with a position, damping, etc
			groundDef = new b2BodyDef();
			groundDef.position.Set(Gx, Gy);
			groundDef.angle = Ga;
			
			// make the body visible (PhyGround is a MovieClip in Library)
			//groundDef.userData = new PhysGround();
			//groundDef.userData.width = Gw*RATIO; 
			//groundDef.userData.height = Gh*RATIO; 
			//addChild(groundDef.userData);
			
			// Use the world object to create the body
			ground = m_world.CreateBody(groundDef);
			
			/* 	Define shapes,
			 	a 2D piece of collision geometry that is rigidly attached to a body. 
				Shapes have material properties of friction and restitution.
			*/
			boxDef = new b2PolygonDef();
			boxDef.SetAsBox(Gw/2, Gh/2);
			boxDef.friction = 0.3;
			boxDef.density = 0; // static bodies require zero density
			
			// Create shapes on the body
			ground.CreateShape(boxDef);
		}
		
		private function addObject(e:MouseEvent):void{
			var body:b2Body;
			var bodyDef:b2BodyDef;
			var boxDef:b2PolygonDef;
			var circleDef:b2CircleDef;
			
			bodyDef = new b2BodyDef();
			bodyDef.position.x = e.stageX /RATIO;
			bodyDef.position.y = e.stageY /RATIO;

			var rX:Number = Math.random() + 0.1;
			var rY:Number = Math.random() + 0.1;

			// Circle
			circleDef = new b2CircleDef();
			circleDef.radius = rX;
			circleDef.density = 1.0;
			circleDef.friction = 0.5;
			circleDef.restitution = 0.8;
				
			//bodyDef.userData = new PhysCircle();
			//bodyDef.userData.width = rX * 2 * RATIO; 
			//bodyDef.userData.height = rX * 2 * RATIO; 
			//addChild(bodyDef.userData);
			
			body = m_world.CreateBody(bodyDef);
			body.CreateShape(circleDef);
			body.SetMassFromShapes();
		}
				
		private function Update(e:Event):void{
			
			m_world.Step(m_timeStep, m_iterations);
			
			// Go through body list and update sprite positions/rotations
			for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
				if (bb.m_userData is Sprite){
					bb.m_userData.x = bb.GetPosition().x * RATIO;
					bb.m_userData.y = bb.GetPosition().y * RATIO;
					bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
				}
			}			
		}
		
		private function debugDraw():void
		{
			var dbgDraw:b2DebugDraw = new b2DebugDraw();
			dbgSprite = new Sprite();
			addChild(dbgSprite);
			dbgDraw.m_sprite = dbgSprite;
			dbgDraw.m_drawScale = RATIO;
			dbgDraw.m_fillAlpha = 0.0;
			dbgDraw.m_lineThickness = 1.0;
			dbgDraw.m_drawFlags = 0xFFFFFFFF;
			m_world.SetDebugDraw(dbgDraw);

		}
		
	}

}

Forked