import flash.display.Sprite; import flash.events.Event; // Classes used in this example import Box2D.Dynamics.*; import Box2D.Collision.*; import Box2D.Collision.Shapes.*; import Box2D.Common.Math.*; |
// create world AABB var worldAABB:b2AABB = new b2AABB(); worldAABB.minVertex.Set(-1000.0, -1000.0); worldAABB.maxVertex.Set(1000.0, 1000.0); // Define the gravity vector var gravity:b2Vec2 = new b2Vec2(0.0, 300.0); // Allow bodies to sleep var doSleep:Boolean = true; // Construct a world object m_world = new b2World(worldAABB, gravity, doSleep); |
// Add event for main loop addEventListener(Event.ENTER_FRAME, updating, false, 0, true); public function updating(e:Event):void{ m_world.Step(m_timeStep, m_iterations); } |
var bodyDef:b2BodyDef; var boxDef:b2BoxDef; var circleDef:b2CircleDef; // Add ground body bodyDef = new b2BodyDef(); boxDef = new b2BoxDef(); boxDef.extents.Set(1000, 100); boxDef.friction = 0.3; /*Notice that the ground object has no density like the later definitions. That's because it is static and we don't want it effected by any forces.*/ bodyDef.position.Set(320, 400); bodyDef.AddShape(boxDef); // Add sprite to body userData /*We have a Sprite object in the library called PhysGround. Here we are associating that with our body definition.*/ bodyDef.userData = new PhysGround(); bodyDef.userData.width = boxDef.extents.x * 2; bodyDef.userData.height = boxDef.extents.y * 2; addChild(bodyDef.userData); m_world.createBody(bodyDef); // Add some objects for (var i:int = 1; i < 20; i++){ /*We are going to re-use the same bodyDef from before. It doesn't matter now, because it's already been copied and stored in our world object.*/ bodyDef = new b2BodyDef(); // Box if (Math.random() < 0.5){ boxDef = new b2BoxDef(); boxDef.extents.Set(Math.random() * 15 + 10, Math.random() * 15 + 10); boxDef.density = 1.0; boxDef.friction = 0.5; boxDef.restitution = 0.2; bodyDef.AddShape(boxDef); /*We have a Sprite object in the library called PhysBox.*/ bodyDef.userData = new PhysBox(); bodyDef.userData.width = boxDef.extents.x * 2; bodyDef.userData.height = boxDef.extents.y * 2; } // Circle else { circleDef = new b2CircleDef(); circleDef.radius = Math.random() * 15 + 10; circleDef.density = 1.0; circleDef.friction = 0.5; circleDef.restitution = 0.2 bodyDef.AddShape(circleDef); /*We have a Sprite object in the library called PhysCircle.*/ bodyDef.userData = new PhysCircle(); bodyDef.userData.width = circleDef.radius * 2; bodyDef.userData.height = circleDef.radius * 2; } bodyDef.position.x = Math.random() * 400 + 120; bodyDef.position.y = Math.random() * 100 + 50; m_world.createBody(bodyDef); addChild(bodyDef.userData); } |
public function updating(e:Event):void{ m_world.Step(m_timeStep, m_iterations); // Go through body list and 更新 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.m_position.x; bb.m_userData.y = bb.m_position.y; bb.m_userData.rotation = bb.m_rotation * (180/Math.PI); } }/*This is an extreemly clever way to do this, and I suggest you copy it exactly in your own projects.*/ } |
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |