Box2D 世界大小

发布于 2024-12-13 20:18:05 字数 251 浏览 4 评论 0原文

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

所以我只是在寻找一种限制高度的方法,因为我的球员是一个上下跳跃的球,我想限制它能跳多高(跳跃是由物理、重力和球的速度完成的,所以经过几次良好的跳跃后,随着速度的增加,球跳得非常高,我不想限制速度)并在假设y=900上放置边界。

I have created a box2d world, and I wanted to limit the height of the world.
I did search google and apparently there was an option in the previous version of box2d where you would have to define the size of your world, but I am not sure if you were able set the height of the world but in the current version they have taken that option completely off.

So I am just looking for a way to limit the height, as my player is a ball that jumps up and down and I want to limit how high it can jump (the jumps are done by physics and gravity and the speed of the ball so after a few good jumps, the ball jumps really high as its speed increases and I dont want to limit the speed) and put border on let say y=900.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的心爱 2024-12-20 20:18:05

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 方法)。

The Box2D world has an infinite size. You can't limit the world, but you can create a shape that encloses a certain area in the Box2D world.

Here's how to create a body and shape that puts a shape just around the screen, so that objects don't leave the screen. It is easy to adapt this code by changing the corner coordinates to fit whatever you need:

        // 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);

Note: this code is for Box2D v2.2.1. I assume that's what you're using because you said "previous version" which required this code to be written differently (with the SetAsEdge method).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文