C++: Checking if an object faces a point (within a certain range)

Posted by bojoradarial on Game Development See other posts from Game Development or by bojoradarial
Published on 2014-08-24T22:03:45Z Indexed on 2014/08/24 22:35 UTC
Read the original article Hit count: 265

Filed under:

I have been working on a shooter game in C++, and am trying to add a feature whereby missiles shot must be within 90 degrees (PI/2 radians) of the direction the ship is facing. The missiles will be shot towards the mouse. My idea is that the ship's angle of rotation is compared with the angle between the ship and the mouse (std::atan2(mouseY - shipY, mouseX - shipX)), and if the difference is less than PI/4 (45 degrees) then the missile can be fired. However, I can't seem to get this to work. The ship's angle of rotation is increased and decreased with the A and D keys, so it is possible that it isn't between 0 and 2*PI, hence the use of fmod() below.

Code:

float userRotation = std::fmod(user->Angle(), 6.28318f);
if (std::abs(userRotation - missileAngle) > 0.78f) return;

Any help would be appreciated. Thanks!

© Game Development or respective owner

Related posts about c++