将主体移动到特定位置 - Box2D
我有一个 b2Body,我想将其移动到某个目标位置。我不想使用 SetPosition 函数。我怎样才能实现这一点:
- 改变线速度。
- 使用鼠标关节。 (目标位置是固定的。不涉及鼠标。)
我使用的是 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 :
- Changing linear velocities.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的方法是设置物体的线速度,使其在一个时间步长内走完必要的距离。例如。如果身体需要移动 (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
最简单的方法实际上是使用
SetPosition
/SetTransform(position,angle)
。例如:显然,瞬间跳跃意味着你颠覆了物理模拟,但它是设置身体位置的最简单最直接的方法。
如果您不想使用
SetPosition
(相当于上面发布的代码),则使用适当的力(基于物体的质量和当前速度)ApplyLinearImpulse
body) 就可以解决这个问题,从模拟的角度来看更正确,但考虑到潜在的副作用等,可能会出现更多问题。无论如何,iforce2d 涵盖了
SetLinearVelocity
...,我想补充一点,即使不涉及“鼠标”,鼠标关节也非常有用。The simplest way is actually to use
SetPosition
/SetTransform(position,angle)
. For example: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) thenApplyLinearImpulse
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.