box2d -- test1

box2d 资料真的好少啊。

 

demo:  http://www.sandy1219.com/box2d/test1/Hello2.html

 

 <textarea cols="79" rows="18" name="code" class="c-sharp">package { import Box2D.Collision.Shapes.b2CircleDef; import Box2D.Collision.Shapes.b2PolygonDef; import Box2D.Collision.b2AABB; import Box2D.Common.Math.b2Vec2; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2BodyDef; import Box2D.Dynamics.b2DebugDraw; import Box2D.Dynamics.b2World; import flash.display.Sprite; import flash.display.StageScaleMode; import flash.events.Event; [SWF(backgroundColor="0x333333", width="550", height="800", frameRate="30")] public class Hello2 extends Sprite { public function Hello2() { super(); init(); } private var worldAB:b2AABB; private var gravity:b2Vec2; private var world:b2World; private var m_phys_scale:Number = 30 ; private var rate:Number = 30 ; public var m_timestep:Number=1.0 / 30.0; public var m_iterations:Number=10.0; public var m_dbgSprite:Sprite; private function init():void { stage.scaleMode = StageScaleMode.NO_SCALE; worldAB = new b2AABB(); worldAB.lowerBound.Set(-100.0, -100.0); worldAB.upperBound.Set(100.0, 100.0); gravity = new b2Vec2(0.0,10.0); var sleep:Boolean = true ; world = new b2World(worldAB,gravity,sleep); createBox(300 / m_phys_scale , 100 / m_phys_scale, 0, 10 / m_phys_scale, 10 / m_phys_scale, true); AddStaticBox(250 / m_phys_scale, 480 / m_phys_scale, 250 / m_phys_scale, 10 / m_phys_scale); m_dbgSprite=new Sprite(); addChild(m_dbgSprite); SetDebugDraw(); addEventListener(Event.ENTER_FRAME, Update); } private function createBox(xPos:Number, yPos:Number, angle:Number, halfWidth:Number, halfHeight:Number, isStatic:Boolean):void { var boxShape:b2CircleDef = new b2CircleDef(); boxShape.density = .5 ; boxShape.friction = .3 ; boxShape.restitution = .3 ; //boxShape.SetAsBox(halfWidth, halfHeight); boxShape.radius = halfWidth; var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.angle = angle; //bodyDef.userData = new Box(halfWidth * 2, halfHeight * 2, 0xffffff * Math.random()); bodyDef.position.Set( xPos, yPos); var body:b2Body = world.CreateBody(bodyDef); body.CreateShape(boxShape); body.SetMassFromShapes(); //addChild(bodyDef.userData); } public function Update(e:Event):void { world.Step(m_timestep, m_iterations); /* for (var b:b2Body = world.m_bodyList; b; b=b.m_next) { if (b.m_userData is Sprite) { b.m_userData.x = b.GetPosition().x *2 ; b.m_userData.y = b.GetPosition().y *2; b.m_userData.rotation = b.GetAngle() * 180 / Math.PI; } } */ } public function AddStaticBox(_x:Number, _y:Number, _halfwidth:Number, _halfheight:Number):void { var bodyDef:b2BodyDef = new b2BodyDef(); bodyDef.position.Set(_x, _y); var boxDef:b2PolygonDef = new b2PolygonDef(); boxDef.SetAsBox(_halfwidth, _halfheight); boxDef.density=0.0; var body:b2Body=world.CreateBody(bodyDef); body.CreateShape(boxDef); body.SetMassFromShapes(); } public function SetDebugDraw():void { var dbgDraw:b2DebugDraw = new b2DebugDraw(); dbgDraw.m_sprite = m_dbgSprite; dbgDraw.m_drawScale = 30; dbgDraw.m_fillAlpha = 1; dbgDraw.m_lineThickness = 2.0; dbgDraw.m_drawFlags = b2DebugDraw.e_aabbBit | b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit | b2DebugDraw.e_centerOfMassBit | b2DebugDraw.e_coreShapeBit | b2DebugDraw.e_obbBit | b2DebugDraw.e_pairBit ; world.SetDebugDraw(dbgDraw); } } }</textarea>

你可能感兴趣的:(box2d -- test1)