2
votes

Many apologies if this has been answered before but I wasn't able to quite find what I was looking for.

I have a Box2D dynamic body that I apply linear impulse to to turn it into a projectile. So when I click anywhere on the screen I want the body to be projected towards the touch location. I'm not concerned about the force yet, just the angle.

I already have my sprite rotating to point to the touch location so I can detect the initial rotation angle but how do I turn this angle, say 45 degrees into a "b2Vec2" value so that I can launch the body at exactly 45 degrees? something like this:

float rotationValue = 45.0f;
b2Vec2 vect = ??????
b2Vec2 PointVector = body->GetPosition();
body->ApplyLinearImpulse(vect, PointVector);

Any clues would be greatly appreciated.

Thanks

1

1 Answers

6
votes

sigh - this is why you should pay attention in your Math class :)

Converting angles to vect is super simple:

float angle = 45.0f;
b2Vec2 vect = b2Vec2(cos(angle), sin(angle));