我创建了一个 box2d 世界,我想限制世界的高度。 我确实搜索了谷歌,显然在以前版本的 box2d 中有一个选项,你必须定义你的世界的大小,但我不确定你是否能够设置世界的高度,但在当前版本中他们有完全取消该选项。
所以我只是在寻找一种限制高度的方法,因为我的玩家是一个上下跳跃的球,我想限制它能跳多高(跳跃是由物理和重力以及球所以在几次良好的跳跃之后,随着速度的增加,球跳得非常高,我不想限制速度)并在 y=900
上加上边框。
最佳答案
Box2D 世界的大小是无限的。你不能限制世界,但你可以创建一个形状来包围 Box2D 世界中的特定区域。
下面介绍了如何创建一个主体和形状,将形状放在屏幕周围,这样对象就不会离开屏幕。通过更改角坐标以适应您需要的任何内容,很容易调整此代码:
// for the screenBorder body we'll need these values
CGSize screenSize = [CCDirector sharedDirector].winSize;
float widthInMeters = screenSize.width / PTM_RATIO;
float heightInMeters = screenSize.height / PTM_RATIO;
b2Vec2 lowerLeftCorner = b2Vec2(0, 0);
b2Vec2 lowerRightCorner = b2Vec2(widthInMeters, 0);
b2Vec2 upperLeftCorner = b2Vec2(0, heightInMeters);
b2Vec2 upperRightCorner = b2Vec2(widthInMeters, heightInMeters);
// static container body, with the collisions at screen borders
b2BodyDef screenBorderDef;
screenBorderDef.position.Set(0, 0);
b2Body* screenBorderBody = world->CreateBody(&screenBorderDef);
b2EdgeShape screenBorderShape;
// Create fixtures for the four borders (the border shape is re-used)
screenBorderShape.Set(lowerLeftCorner, lowerRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(lowerRightCorner, upperRightCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperRightCorner, upperLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
screenBorderShape.Set(upperLeftCorner, lowerLeftCorner);
screenBorderBody->CreateFixture(&screenBorderShape, 0);
注意:此代码适用于 Box2D v2.2.1。我假设这就是您正在使用的,因为您说的是“以前的版本”,这要求以不同的方式编写此代码(使用 SetAsEdge 方法)。
关于objective-c - Box2D 世界大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020093/