开发者

body driving forward or backward box2d

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 kn开发者_开发百科ow how to find out the speed

b2Vec2 currentVelocity = _body->GetLinearVelocity();

float32 speed = currentVelocity.Normalize();

but not if its forward or backward.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜