将旋转角度转换为 Box2D 脉冲

发布于 2024-10-06 07:12:44 字数 424 浏览 0 评论 0原文

如果之前已经回答过这个问题,我深表歉意,但我无法找到我想要的东西。

我有一个 Box2D 动态主体,我对其应用线性脉冲以将其变成射弹。因此,当我单击屏幕上的任意位置时,我希望身体投影到触摸位置。我还不关心力,只关心角度。

我已经让我的精灵旋转到指向触摸位置,这样我就可以检测到初始旋转角度,但是如何将这个角度(例如 45 度)转换为“b2Vec2”值,以便我可以精确地以 45 度发射主体?像这样的事情:

float rotationValue = 45.0f;
b2Vec2 vect = ??????
b2Vec2 PointVector = body->GetPosition();
body->ApplyLinearImpulse(vect, PointVector);

任何线索将不胜感激。

谢谢

Many apologies if this has been answered before but I wasn't able to quite find what I was looking for.

I have a Box2D dynamic body that I apply linear impulse to to turn it into a projectile. So when I click anywhere on the screen I want the body to be projected towards the touch location. I'm not concerned about the force yet, just the angle.

I already have my sprite rotating to point to the touch location so I can detect the initial rotation angle but how do I turn this angle, say 45 degrees into a "b2Vec2" value so that I can launch the body at exactly 45 degrees? something like this:

float rotationValue = 45.0f;
b2Vec2 vect = ??????
b2Vec2 PointVector = body->GetPosition();
body->ApplyLinearImpulse(vect, PointVector);

Any clues would be greatly appreciated.

Thanks

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

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

发布评论

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

评论(1

梦醒时光 2024-10-13 07:12:45

叹息 - 这就是为什么你应该在数学课上注意的原因:)

将角度转换为 vect 非常简单:

float angle = 45.0f;
b2Vec2 vect = b2Vec2(cos(angle), sin(angle));

sigh - this is why you should pay attention in your Math class :)

Converting angles to vect is super simple:

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