如何在 Box2D 中设置形状/夹具/主体的中心

发布于 2024-11-18 19:37:52 字数 340 浏览 4 评论 0原文

嘿,我正在尝试集成 SFML 和 Box2D,SFML 使精灵、形状等的设置中心变得非常容易。另一方面,Box2D 我遇到了麻烦,因为我不知道如何设置甚至找到形状或夹具的中心。

在我看来,当手动添加顶点到 b2_PolygonShape 时,中心被设置为顶点数组中的第一个顶点,但是当使用快捷函数 SetAsBox() 或任何其他 SetAs__()。中心是形状的中间,或框/形状的一半范围。

我需要通用 box2D 和 SFML 的居中系统,但我无法弄清楚局部坐标系如何适用于对象。

如何在 Box2D 中设置/获取许多对象(例如形状、固定装置、主体等)的中心?

Hey i'm trying to integrate SFML and Box2D, and SFML has made setting centers for sprites, shapes, etc. very easy. Box2D, on the other hand, I'm having trouble with as i can't figure out how to set or even find the center of a shape or fixture.

It seems to me that when adding vertices manually to a b2_PolygonShape the center is set to the first vertex in the vertex array, but the results are very different when using the shortcut functions SetAsBox() or any other SetAs__(). The Center is the middle of the shape, or half extent(s) of the box/shape.

I need to commonize box2D's and SFML's centering system, but i can't figure out how the local coordinate systems work for objects.

How do I set/get the centers of the many objects such as shapes, fixtures, bodies, etc. in Box2D?

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

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

发布评论

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

评论(5

狠疯拽 2024-11-25 19:37:53

好吧,我意识到 SFML 和 Box2D 确实没有那么不同,但我只是没有考虑如何在世界坐标中正确制作/渲染形状。

除非另有说明,当移动/绘制对象的位置时,始终使用 (0,0) 作为对象的参考点/中心,并且每个顶点都是相对于该点绘制的。

SFML 教程有点令人困惑,因为它说设置形状/精灵的中心是相对于对象左上角的偏移量,不是 (0,0)。

Ok I realized that SFML and Box2D really weren't all that different, but i just wasn't thinking about how shapes are made/ rendered correctly in world coordinates.

Unless otherwise specified, (0,0) is always used as the refrence point/ center of the object when its position is being moved around/ drawn, and each vertex is drawn in relation to that point.

the SFML tutorial was a bit confusing as it said setting the center of a shape/sprite was the offset from the top left corner of the object, not (0,0).

如果没结果 2024-11-25 19:37:53

我认为使用 box2d 你有责任使多边形的“质心”与你身体的位置重合。
基本上你要做的就是这样的。

  • 计算顶点列表的质心 (b2vec)

  • 将顶点移动 -centroid。

Box2d 提供了必要的函数来帮助您完成此任务。
SetAs__ 的情况是,质心为 (0,0),并且围绕身体中心对称地创建多边形。

I think that with box2d you are responsible of making the 'centroid' of the polygon coincide with the position of your body.
Basically what you have to do is something like this.

  • Compute the centroid of your list of vertices (b2vec)

  • Shift the vertices by -centroid.

Box2d provides the necessary functions to help you with this task.
What happens with SetAs__ is that the centroid is (0,0) and the polygon is created symmetrically around the center of the body.

梦晓ヶ微光ヅ倾城 2024-11-25 19:37:53

我会再试一次。我很确定我可以帮助你,但我不完全理解你的问题。

物体在世界上有一个位置 (b2Vec2)。
物体具有b2Fixture(密度、摩擦、恢复、形状等...)
b2PolygonShape 具有顶点和海量数据。这些顶点是相对于身体位置的。

b2PolgygonShape 的示例顶点(矩形,大小:1 米 * 1 米)

(-0.5f, -0.5f)  // left  upper corner
( 0.5f, -0.5f)  // right upper corner
( 0.5f,  0.5f)  // right lower corner
(-0.5f,  0.5f)  // left  lower corner

请记住,您无法自己设置顶点!您必须将它们传递给

b2PolgygonShape::Set(b2Vec2 *vertices, int count);

此方法将根据顶点计算大量数据。

I'll give it another try. I'm pretty sure I can help you but I don't understand your question completely.

A body has a Position (b2Vec2) in the world.
A body has b2Fixtures (density, friction, restitution, shape, etc...)
The b2PolygonShape has vertices and mass data. Those vertices are relative to the body position.

Example vertices for a b2PolgygonShape (rectangle, size: 1 meter * 1 meter)

(-0.5f, -0.5f)  // left  upper corner
( 0.5f, -0.5f)  // right upper corner
( 0.5f,  0.5f)  // right lower corner
(-0.5f,  0.5f)  // left  lower corner

Keep in mind you can't set the vertices yourself! You have to pass them to

b2PolgygonShape::Set(b2Vec2 *vertices, int count);

This method will compute the mass data, depending on the vertices.

与往事干杯 2024-11-25 19:37:53

这可能是相关的,尽管我使用的是 Box2DWeb 和 Easel.js 而不是 Box2D 和 SFML。
我遇到的问题是 Easel.js regX/regY(精灵的图形中心)需要匹配物理质心。

对于具有一个 b2FixtureDef 的简单 b2Body,主体的质心与 b2FixtureDef.shape 的 m_centroid 重合(无论是 SetAsBox、SetAsArray、新的 b2CircleShape 等)。

对于多夹具主体和不规则多边形,对我有用的是添加所有夹具(即调用 b2Body.CreateFixture),然后将图形 regX/regY 设置为等于 b2Body.m_sweep.localCenter.x/y(乘以像素) /米比例因子)。

这对于简单的圆圈和方框不起作用,但它们一开始就不是问题。希望这有帮助。

This is maybe related, though I'm using Box2DWeb and Easel.js rather than Box2D and SFML.
The issue I encountered is that the Easel.js regX/regY (the graphical center of the sprite) needs to match the physical center of mass.

For a simple b2Body with one b2FixtureDef, the center of mass of the body coincides with the m_centroid of the b2FixtureDef.shape (whether SetAsBox, SetAsArray, new b2CircleShape, etc).

For multi-fixture bodies and irregular polygons, what worked for me was to add all fixtures (i.e. call b2Body.CreateFixture), then set the graphic regX/regY equal to the b2Body.m_sweep.localCenter.x/y (multiplied by the pixels/meter scale factor).

This didn't work for simple circles and boxes, but they weren't a problem in the first place. Hope this helps.

メ斷腸人バ 2024-11-25 19:37:53

您可以简单地执行以下操作:

b2Body *body = ...;
b2Vec2 pos = body->GetPosition();

这是身体在世界中的位置(pos.xpos.y,以米为单位定义)。
这意味着您必须使用比率,例如:1 米 = 20 像素。因此,您必须将坐标乘以比率才能获得以像素为单位的位置。

使用该位置来绘制图像。如果需要,您可以向图像添加坐标,并在相对于身体位置的这些坐标上绘制图像。

请记住,您也必须按照该比例绘制图像。当比例改变(放大或缩小)时,图像也应该放大。

对于我的游戏,我使用 SDL 和 OpenGL,这是一个非常好的组合,所以我不知道 SFML 到底是如何工作的。

You can simply do this:

b2Body *body = ...;
b2Vec2 pos = body->GetPosition();

This is the body position in the world (pos.x and pos.y, defined in meters).
This means you have to use a ratio, eg: 1 meter = 20 pixels. So you have to multiply the coordinates by your ratio to get the position in pixels.

Use that position to draw your images. If needed you can add coordinates to your image and draw the image on these coordinates relative to the body position.

Keep in mind that you have to draw your images as well with that ratio. When the ratio changes (zoom in or zoom out), the images should enlarge as well.

For my game, I'm using SDL with OpenGL, which is a very nice combination, so I don't know exactly how SFML works.

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