Mixing Box2D 加速度计 +重力?

发布于 2024-12-13 19:09:46 字数 656 浏览 3 评论 0原文

我有 2 个问题:
1. 是否可以检索 b2Body 的当前 X 坐标?我知道 API GetPosition 但这还不够具体,我特别需要 X 坐标。
2. 是否可以在 UIAccelerometer 委托方法中向特定的 b2Body 添加重力?

我想做的是使用应用力将 b2Body 与当前 UIAccelerometer 输入和 Box2D 重力一起移动到一条线上。 这就是我到目前为止所拥有的,没有人知道如何将 #1+#2 合并到这一行中吗?:

body->ApplyForce(b2Vec2(accelX*100, -30.0f), body->GetPosition());

谢谢!

编辑1: 感谢您回答这些问题,这让我明白了! 这给我留下了另外 3 个问题:
1。现在我了解了如何获取 b2Body 的 x 坐标,我该如何设置它?
会是这样的吗?

body->GetPosition().x = 30;

2。使用这些 x 坐标,它与我使用 UIKit 时的 X 坐标相同吗?我知道 UIKit 框架不同,我只是想确保如果我在 UIKit 中做了类似的事情,它会输出相同的 X 坐标,这是正确的吗?

I have 2 questions:
1. Is it possible to retrieve the current X Coordinate of a b2Body? I know the API GetPosition but that is not specific enough, I need the X coordinate specifically.
2. Is it possible to add gravity to my specific b2Body in my UIAccelerometer delegate method?

What I am trying to do is move the b2Body with the current UIAccelerometer input with Box2D gravity in one line using Apply Force.
This is what I have so far, doesn't anyone know how I can incorporate #1+#2 into this line?:

body->ApplyForce(b2Vec2(accelX*100, -30.0f), body->GetPosition());

Thanks!

Edit1:
Thanks for answering those questions, that cleared it up for me!
This leaves me 3 more questions:
1. Now that I understand how to get the x coordinate of b2Body, how would I set it?
Would it be something like this?

body->GetPosition().x = 30;

2. With these x coordinates it is the same X coordinate as if I was using UIKit correct? I know UIKit frames things different I just want to make sure that if I did something similar in UIKit, it would output the same X coordinate, is that correct?

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

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

发布评论

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

评论(2

彡翼 2024-12-20 19:09:46

答案 1)

b2Vec pos = body->GetPosition();
float x = pos.x;
// do whatever you need to do with x

您还可以编写

float x = body->GetPosition().x;

答案 2)

将 UIAccelerometer didAccelerate 方法所需的任何值存储在实例变量中。例如accelX:

accelX = acceleration.x;

在您的世界更新方法中,只需使用这些值以您需要的任何方式计算力。然后像您发布的那样施加力:

body->ApplyForce(b2Vec2(accelX*100, -30.0f), body->GetPosition());

Answer to 1)

b2Vec pos = body->GetPosition();
float x = pos.x;
// do whatever you need to do with x

You can also write

float x = body->GetPosition().x;

Answer to 2)

Store whatever values you need from the UIAccelerometer didAccelerate method in an instance variable. For example accelX:

accelX = acceleration.x;

In your world update method simply use those values to calculate the force in whatever way you need it. Then apply the force like you posted:

body->ApplyForce(b2Vec2(accelX*100, -30.0f), body->GetPosition());
野の 2024-12-20 19:09:46

1) 1) body->GetPosition().x

1) 2) 施加力或改变世界重力。

2) 1) 您使用 SetPosition(b2Vec2)。

2) 2) Box2d 坐标除以 PTM_RATIO,0,0 位于 box2d 的左下角,但对于 UIKit 位于左上角。

1) 1) body->GetPosition().x

1) 2) Either apply a force or change the world's gravity.

2) 1) You use SetPosition(b2Vec2).

2) 2) Box2d coordinates are divided by PTM_RATIO and 0,0 is in the bottom left for box2d, but top left for UIKit.

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