将身体移动到特定位置-Box2D [英] Move body to a specific position - Box2D
问题描述
我有一个b2Body,我想移动到某个目标位置。我不想使用SetPosition函数。如何使用以下方法实现此目的:
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 :
- 更改线速度。
- 使用mouseJoint。 (目标位置是固定的。不涉及鼠标。)
我正在使用Box2DAS3 2.1a。任何其他语言的帮助也将不胜感激。
I'm using Box2DAS3 2.1a. Help in any other language would also be appreciated.
推荐答案
最简单的方法实际上是使用 SetPosition
/ SetTransform(position,angle)
。例如:
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.
鉴于您不想使用 SetPosition
(与代码等效),然后在适当的作用力下(根据质量和当前身体速度) ApplyLinearImpulse
即可解决问题,并且从仿真角度来看更正确
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.
无论如何,iforce2d涵盖了 SetLinearVelocity
。 ..,我还要补充一点,即使不涉及鼠标,鼠标关节也非常有用。
Anyway, iforce2d covered SetLinearVelocity
..., and I would add that a mouse joint is very useful even when the "mouse" is not involved.
这篇关于将身体移动到特定位置-Box2D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!