Box2D 2.1中的多边形/形状定义发生了变化

我在这里经历了一个关于Box2D的相当不错的教程,但是当他做了一个b2PolygonDef时,我遇到了一个问题,因为这个教程是2.0版本的,而且我使用的是2.1。 我可以下载2.0我想,但我真的想知道如何更新如何做到这一点的最新版本。

如果你们不想看整个video,基本上他是这样做的:

  private function createWallsAndFloor():void { // Create the shape (polygon) definition var bigLongShapeDef:b2PolygonDef = new b2Polygondef(); bigLongShapeDef.vertexCount = 4; b2Vec2(bigLongShapeDef.vertices[0]).Set(0 / RATIO, 0 / RATIO); b2Vec2(bigLongShapeDef.vertices[1]).Set(550 / RATIO, 0 / RATIO); b2Vec2(bigLongShapeDef.vertices[2]).Set(550 / RATIO, 10 / RATIO); b2Vec2(bigLongShapeDef.vertices[3]).Set(0 / RATIO, 10 / RATIO); bigLongShapeDef.friction = 0.5; bigLongShapeDef.restitution = 0.3; bigLongShapeDef.density = 0.0; // Create the body definition // Create the body // Create the shape } 

我可以看到他们删除了b2PolygonDefb2ShapeDef因为我使用的是Flash Develop,那么我该怎么做呢?

(如果你不介意看video,他会解释他在做这个之前在做什么。)

错误是由于Box2D引擎正在更新。 FlashyTodd的优秀教程不幸已经过时(22/10/10)。 尽管他在video的顶部提到了这一点,但是对于新手来说,链接改变的东西并不多(包括我自己在内)。

DrDeth的Box2D手册上面的链接是C ++手册,而不是AS3,这是FlashyTodd的工作内容。

我还发现这个有用的例子 ,它logging了从2.0到2.1代码的变化。

对于其他人来说,这是FlashyTodd示例HelloBoxWorld更新为符合Box2D 2.1。 (只是一个地板和dynamic框)

  package{ import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import Box2D.Dynamics.b2World; import Box2D.Collision.Shapes.b2PolygonShape; import Box2D.Dynamics.b2Body; import Box2D.Dynamics.b2Fixture; import Box2D.Dynamics.b2FixtureDef; import Box2D.Dynamics.b2BodyDef; import Box2D.Common.Math.b2Vec2; import Box2D.Collision.b2AABB; import Box2D.Dynamics.b2DebugDraw; public class HelloBoxWorld extends MovieClip{ protected var _world:b2World; //ratio of pixels to meters. public static const RATIO:Number = 30; private var _fallingCrate:b2Body; private var timeStep:Number = 1.0 / 30.0; private var velocityIterations:int = 6; private var positionIterations:int = 2; public function HelloBoxWorld() { //1. set up the world setupWorld(); //2. create walls and floor createWallsAndFloor(); //3. make create addACrate(); setupDebugDraw(); addEventListener(Event.ENTER_FRAME, newFrameEvent); } private function setupDebugDraw():void { var spriteToDrawOn:Sprite = new Sprite(); addChild(spriteToDrawOn); var artistForHire:b2DebugDraw = new b2DebugDraw(); artistForHire.SetSprite(spriteToDrawOn); artistForHire.SetDrawScale(RATIO); artistForHire.SetFlags(b2DebugDraw.e_shapeBit); artistForHire.SetLineThickness(2.0); artistForHire.SetFillAlpha(0.6); _world.SetDebugDraw(artistForHire); } private function newFrameEvent(e:Event):void { //variables assigned at the top _world.Step(timeStep, velocityIterations, positionIterations); _world.ClearForces(); _world.DrawDebugData(); trace("Falling Craete is at x:"+_fallingCrate.GetPosition().x +",y:"+_fallingCrate.GetPosition().y); } private function setupWorld():void { //1. set up 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, 9.8); //3. Ignore sleeping objects. var ignoreSleeping:Boolean = true; _world = new b2World(gravity, ignoreSleeping); trace ("My world has "+_world.GetBodyCount()+" bodies"); } private function createWallsAndFloor():void { //1. Define a body with a position var groundBodyDef:b2BodyDef = new b2BodyDef(); //x,y of centre. set as integers, not floats groundBodyDef.position.Set(250 / RATIO, 400 / RATIO); //2. Use the world object to create the body using the body definition above var groundBody = _world.CreateBody(groundBodyDef); //3. Define fixtures with a shape, friction, density, etc. var groundBox:b2PolygonShape = new b2PolygonShape(); //width ,height. set as integers, not floats groundBox.SetAsBox(200 / RATIO, 20 / RATIO); var fixtureDef:b2FixtureDef = new b2FixtureDef(); fixtureDef.shape = groundBox; fixtureDef.density = 0.0;//density is set to 0.0 by default so this isnt really needed here fixtureDef.friction = 0.5; groundBody.CreateFixture(fixtureDef); trace ("My world has "+_world.GetBodyCount()+" bodies"); } private function addACrate():void { //moving object var movingBodyDef:b2BodyDef = new b2BodyDef(); //set the type of body here movingBodyDef.type = b2Body.b2_dynamicBody;//b2_dynamicBody is a property of b2Body. !manual is confusing here. movingBodyDef.position.Set(100.0 / RATIO, 100.0 / RATIO); movingBodyDef.angle = 30 * Math.PI / 180; _fallingCrate = _world.CreateBody(movingBodyDef); var dynamicBox:b2PolygonShape = new b2PolygonShape(); dynamicBox.SetAsBox(50.0 / RATIO, 50.0 / RATIO); var dynamicfixtureDef:b2FixtureDef = new b2FixtureDef(); dynamicfixtureDef.shape = dynamicBox; dynamicfixtureDef.density = 1.0; dynamicfixtureDef.friction = 0.3; _fallingCrate.CreateFixture(dynamicfixtureDef); trace ("My world has "+_world.GetBodyCount()+" bodies"); } } } 

你应该看看Box2D手册页 。 它写得很好,很容易理解。

为了更直接地解决你的问题,看起来你不再使用形状定义来定义形状。 您只需简单地创建形状,然后使用夹具将形状附加到一个物体上。

 b2PolygonShape dynamicBox; dynamicBox.SetAsBox(1.0f, 1.0f); b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); 

希望这可以帮助。

以下是我正在处理的游戏中的一段代码:

  // Create the shape b2PolygonShape shape; // Create a "capsule" shaped object b2Vec2 vertices[8]; vertices[0].Set( (-mWidth/2 + 5.0f) / PhysicsManager::mPhysicsScale, (mHeight/2) / PhysicsManager::mPhysicsScale); vertices[1].Set( (-mWidth/2) / PhysicsManager::mPhysicsScale, (mHeight/2 - 5.0f) / PhysicsManager::mPhysicsScale); vertices[2].Set( (-mWidth/2) / PhysicsManager::mPhysicsScale, (-mHeight/2 + 5.0f) / PhysicsManager::mPhysicsScale); vertices[3].Set( (-mWidth/2 + 5.0f) / PhysicsManager::mPhysicsScale, (-mHeight/2) / PhysicsManager::mPhysicsScale); vertices[4].Set( (mWidth/2 - 5.0f) / PhysicsManager::mPhysicsScale, (-mHeight/2) / PhysicsManager::mPhysicsScale); vertices[5].Set( (mWidth/2) / PhysicsManager::mPhysicsScale, (-mHeight/2 + 5.0f) / PhysicsManager::mPhysicsScale); vertices[6].Set( (mWidth/2) / PhysicsManager::mPhysicsScale, (mHeight/2 - 5.0f) / PhysicsManager::mPhysicsScale); vertices[7].Set( (mWidth/2 - 5.0f) / PhysicsManager::mPhysicsScale, (mHeight/2) / PhysicsManager::mPhysicsScale); shape.Set( vertices, 8 ); // Create the fixture def b2FixtureDef fixtureDef; fixtureDef.shape = &shape; fixtureDef.density = mDensity; fixtureDef.friction = mFriction; fixtureDef.restitution = mRestitution; fixtureDef.isSensor = mIsSensor; if (contactGroup) { fixtureDef.filter.categoryBits = contactGroup->mCategoryBits; fixtureDef.filter.maskBits = contactGroup->mMaskBits; } // Create the fixture b2Fixture *fixture = mBody->CreateFixture(&fixtureDef);