开发者

body driving forward or backward box2d

开发者 https://www.devze.com 2023-02-12 07:47 出处:网络
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 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

精彩评论

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