物理模拟引擎box2D-2

根据第一例子重新自己做了box2D的例子,感觉写这个比前面的体会多点。

package  
{
	import Box2D.Collision.b2AABB;
	import Box2D.Collision.Shapes.b2PolygonDef;
	import Box2D.Collision.Shapes.b2ShapeDef;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2World;	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author ottoliu
	 */
	public class Hello extends MovieClip {
		
		protected var _world:b2World;
		
		// the ratio of pixels to meters
		public const RATIO:Number = 40;
		private var _fallingBody:b2Body;
		
		public function Hello() 
		{
			// 1. setup the world 
			 setupWorld();
			// 2. Creating the wall and floor
			createWallAndFloor();
			// 3 . adding the crate
			addCrate();
			
			setupDebugDraw();
			
			addEventListener(Event.ENTER_FRAME, newFrameEvent);
		}
		
		private function setupDebugDraw():void
		{
			
			var spriteToDraw:Sprite = new Sprite();
			addChild(spriteToDraw);
			
			var artistForHire:b2DebugDraw = new b2DebugDraw();
			artistForHire.m_sprite = spriteToDraw;
			artistForHire.m_drawScale = RATIO;
			artistForHire.SetFlags(b2DebugDraw.e_shapeBit);
			artistForHire.m_fillAlpha = 0.6;
			artistForHire.m_lineThickness = 2.0;
			
			_world.SetDebugDraw(artistForHire);
			
			
			
		}
		
		private function newFrameEvent(e:Event):void 
		{
			
			_world.Step(1.0 / 30.0, 10);
			
			trace("falling crate is "+_fallingBody.GetPosition().x+" , "+_fallingBody.GetPosition().y);
		}
		
		private function addCrate():void
		{
			//falling blocks to the world
			 var fallingCrateDef:b2PolygonDef = new b2PolygonDef();
			 fallingCrateDef.SetAsBox(25 / RATIO, 25 / RATIO);
			 fallingCrateDef.friction = 0.3;
			 fallingCrateDef.restitution = 0.1;
			 fallingCrateDef.density = 0.4;
			 
			//set it to 250,-30
			var fallingBodyDef:b2BodyDef = new b2BodyDef();
			fallingBodyDef.position.Set(250 / RATIO, -50 / RATIO);
			fallingBodyDef.angle = 30 * Math.PI / 180;
			
			_fallingBody = _world.CreateBody(fallingBodyDef);
			_fallingBody.CreateShape(fallingCrateDef);
			_fallingBody.SetMassFromShapes();
		}
		
		private function createWallAndFloor():void
		{
			//create the shape defination
			var bigLongPolyDef:b2PolygonDef = new b2PolygonDef();
			bigLongPolyDef.vertexCount = 4;
			b2Vec2(bigLongPolyDef.vertices[0].Set(0 / RATIO, 0 / RATIO));
			b2Vec2(bigLongPolyDef.vertices[1].Set(550 / RATIO, 0 / RATIO));
			b2Vec2(bigLongPolyDef.vertices[2].Set(550 / RATIO, 10 / RATIO));
			b2Vec2(bigLongPolyDef.vertices[3].Set(0 / RATIO, 10 / RATIO));
			
			bigLongPolyDef.friction = 0.5;
			bigLongPolyDef.restitution = 0.3;
			bigLongPolyDef.density = 0.0;
					
			
			// create the body defination
			var floorBodyDef:b2BodyDef = new b2BodyDef();
			floorBodyDef.position.Set(0 / RATIO, 390 / RATIO );
			
			// create body
			var floorBody:b2Body = _world.CreateBody(floorBodyDef);
			// create shape
			floorBody.CreateShape(bigLongPolyDef);
			floorBody.SetMassFromShapes();
			
			//wall
			var tallskinDef:b2PolygonDef = new b2PolygonDef();
			tallskinDef.SetAsBox(5 / RATIO, 195 / RATIO);
			tallskinDef.friction = 0.5;
			tallskinDef.restitution = 0.3;
			tallskinDef.density = 0.0;
						
			var wallBodyDef:b2BodyDef = new b2BodyDef();
			wallBodyDef.position.Set(5 / RATIO, 195 / RATIO);
			
			var leftWall:b2Body = _world.CreateBody(wallBodyDef);
			leftWall.CreateShape(tallskinDef);
			leftWall.SetMassFromShapes();
			
			wallBodyDef.position.Set(545 / RATIO, 195 / RATIO );
			var rightWall:b2Body = _world.CreateBody(wallBodyDef);
			rightWall.CreateShape(tallskinDef);
			rightWall.SetMassFromShapes();
			
			trace("the world has "+_world.GetBodyCount()+" bodies.");
		}
		
		private function setupWorld():void
		{
			// 1. setup the size of  the universe
			var universeSize:b2AABB = new b2AABB();
			universeSize.lowerBound.Set( -3000/RATIO, -3000/RATIO);
			universeSize.upperBound.Set( 3000/RATIO, 3000/RATIO);
			
			// 2. define gravity
			var gravity:b2Vec2= new b2Vec2(0.0, 10.0);
			// 3. ignore sleeping?
			var doSleep:Boolean = true;
			
			_world = new b2World(universeSize, gravity, doSleep);
			trace("my world has " + _world.GetBodyCount() + "bodies.");
		}
		
		
	}
	
}

你可能感兴趣的:(box2D)