Mixing Box2D 加速度计 +重力?
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
答案 1)
您还可以编写
答案 2)
将 UIAccelerometer didAccelerate 方法所需的任何值存储在实例变量中。例如accelX:
在您的世界更新方法中,只需使用这些值以您需要的任何方式计算力。然后像您发布的那样施加力:
Answer to 1)
You can also write
Answer to 2)
Store whatever values you need from the UIAccelerometer didAccelerate method in an instance variable. For example accelX:
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:
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.