身体向前或向后行驶 box2d
我有一个 b2_dynamicBody
,我通过 _body->ApplyForce
在屏幕上移动它。
我如何知道它是向前移动还是向后移动?
我知道如何找出速度
b2Vec2 currentVelocity = _body->GetLinearVelocity();
float32 speed = currentVelocity.Normalize();
,但不知道是向前还是向后。
I have a b2_dynamicBody
which I move by _body->ApplyForce
across the screen.
How do I find out, if it's moving forward or backwards?
I know how to find out the speed
b2Vec2 currentVelocity = _body->GetLinearVelocity();
float32 speed = currentVelocity.Normalize();
but not if its forward or backward.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以使用 currentVelocity.Length() 获取速度,这不会更改值。
如果您将向前定义为从左到右(增加 X),那么如果 currentVelocity.x > 则您的身体正在向前移动。如果 x < 则为 0 并向后0. 如果向前向上,则 y > 0 为正向,y < 0 是向后的。
You can also get the speed using
currentVelocity.Length()
which doesn't change the value.If you define forward as left to right (increasing X), then your body is moving forward if currentVelocity.x > 0 and backwards if x < 0. If forward is up, then y > 0 is forward, y < 0 is backwards.
速度是一个矢量,由大小和方向组成。通常,轴上的方向由值的符号指示。
我想说,如果物体相对速度的 y 部分为负值,则可以说物体正在向后移动。
Velocity is a vector, made up from magnitude and direction. Normally direction on an axis is indicated by the sign of the value.
I would say you can say a body is moving backwards if the y portion of the body relative velocity is negative.