将主体移动到特定位置 - Box2D

发布于 2024-11-10 05:44:28 字数 173 浏览 1 评论 0原文

我有一个 b2Body,我想将其移动到某个目标位置。我不想使用 SetPosition 函数。我怎样才能实现这一点:

  1. 改变线速度。
  2. 使用鼠标关节。 (目标位置是固定的。不涉及鼠标。)

我使用的是 Box2DAS3 2.1a。任何其他语言的帮助也将不胜感激。

I have a b2Body which I would like to move at a certain target position. I don't want to use the SetPosition function. How can I achieve this using :

  1. Changing linear velocities.
  2. Using mouseJoint. (The target position is fixed. Mouse is NOT involved.)

I'm using Box2DAS3 2.1a. Help in any other language would also be appreciated.

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

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

发布评论

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

评论(2

挽心 2024-11-17 05:44:29

最简单的方法是设置物体的线速度,使其在一个时间步长内走完必要的距离。例如。如果身体需要移动 (2,3) 个单位才能到达所需位置,并且您的时间步长为 60Hz,您可以将 SetLinearVelocity(120,180) 设置为一个时间步长。在下一个时间步中,您当然必须将速度设置回零以阻止其飞向远处。其结果与使用 SetTransform 的结果没有太大区别,只是 CCD 可以工作,并且如果有任何东西妨碍它就会被破坏。

如果您不希望物体在一个时间步内到达,只需限制它的最大速度即可。

如果您更愿意使用ApplyForce/ApplyLinearImpulse,您可以做类似的事情,但您也需要考虑身体的当前速度。您可能会发现此页面很有帮助,它解释了此页面的旋转版本,但原理是相同的:http://www .iforce2d.net/b2dtut/旋转到角度

The simplest way would be to set the linear velocity of the body so that it will cover the necessary distance in one time step. eg. if the body needs to move (2,3) units to get to the desired location and your timestep is 60Hz you could SetLinearVelocity(120,180) for one time step. In the next time step you would of course have to set the velocity back to zero to stop it flying of into the distance. The result of this is not much different to if you had used SetTransform, except CCD will work and if anything is in the way it will get whacked.

If you don't want the body to arrive in one time step, just limit the max speed it can have.

If you would rather use ApplyForce/ApplyLinearImpulse, you could do something similar but you need to take into account the current speed of the body too. You might find this page helpful, it explains the rotation version of this but the principle is the same: http://www.iforce2d.net/b2dtut/rotate-to-angle

帥小哥 2024-11-17 05:44:28

最简单的方法实际上是使用SetPosition/SetTransform(position,angle)。例如:

body->SetTransform(b2Vec2(0,0),body->GetAngle())

显然,瞬间跳跃意味着你颠覆了物理模拟,但它是设置身体位置的最简单最直接的方法。

如果您不想使用 SetPosition (相当于上面发布的代码),则使用适当的力(基于物体的质量和当前速度)ApplyLinearImpulse body) 就可以解决这个问题,从模拟的角度来看更正确,但考虑到潜在的副作用等,可能会出现更多问题。

无论如何,iforce2d 涵盖了 SetLinearVelocity...,我想补充一点,即使不涉及“鼠标”,鼠标关节也非常有用。

The simplest way is actually to use SetPosition/SetTransform(position,angle). For example:

body->SetTransform(b2Vec2(0,0),body->GetAngle())

Obviously, the instantaneous jump means you are subverting the physics simulation but it is the simplest most direct way to set the position of a body.

Given that you don't want to use SetPosition (which is equivalent to the code posted above) then ApplyLinearImpulse with the appropriate force (based on the Mass and current speed of the body) will do the trick, and is more correct from a simulation point-of-view, but likely to be more problematic given potential side-effects, etc.

Anyway, iforce2d covered SetLinearVelocity..., and I would add that a mouse joint is very useful even when the "mouse" is not involved.

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