Box2D世界大小

Aho*_*tbi 3 xcode objective-c box2d cocos2d-iphone box2d-iphone

我创造了一个box2d世界,我想限制世界的高度.我确实搜索了谷歌,显然在之前版本的box2d中有一个选项,你必须定义你的世界的大小,但我不确定你是否能够设置世界的高度,但在当前版本中他们有完全关闭了该选项.

所以我只是想找到一种方法来限制高度,因为我的球员是一个上下跳跃的球,我想限制它能跳多高(跳跃是由物理和重力以及球的速度完成的经过几次良好的跳跃之后,随着速度的增加,球跳得非常高,我不想限制速度)并且让边界说出来y=900.

Lea*_*s2D 6

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);
Run Code Online (Sandbox Code Playgroud)

注意:此代码适用于Box2D v2.2.1.我假设你正在使用的是因为你说"以前的版本"需要以不同的方式编写代码(使用SetAsEdge方法).