I'm writing a 3D space flightsim, and I'm trying to display a 2D arrow on screen that points to the player's selected target. To clarify, the arrow needs to point in the direction that the player has to turn to reach the target.
I've tried several approaches but I can't seem to make this work for all cases. My current attempt involves constructing a plane from the player's local X/Y, projecting the target position onto it (to make the calculation 2D) then calculating the angle between the player's up and the vector to the projected target position.
I can't help but think I'm over-thinking this and there must be a better, simpler way. Anyone have any suggestions?
Based on Sam's answer, I've tried the following code:
//Find the direction to the target, and normalize it kglt::Vec3 target_dir = (target_pos - ship_pos); target_dir.normalize(); kglt::Vec3 v1, v2; v1 = ship_up.cross(ship_dir); v2 = ship_up.cross(target_dir); v1.normalize(); v2.normalize(); //Get the angle of rotation for the arrow float angle = kmRadiansToDegrees(atan2(ship_up.dot(v1.cross(v2)), v1.dot(v2)));
But the returned angle is always around 180.0 (although normally -179.xx). Is there any obvious error somewhere? I'll continue debugging and update if I figure it out!
Nevermind, the bug isn't in this section of code. My bad!