My favorite "looks simple but is hard" task is "given object position and angle and target position return +1 if the object should turn clockwise and -1 if the object should turn counterclockwise to face the target".
I've got it subtly wrong so many times and using atan2 only solves half the problems. Now I just copypaste it from my previous game :)
Isn’t that just the sign of the cross product of the direction vector and the vector from the object position to the target (or the orientation of the bivector in geometric algebra)? No need for trig there, I don’t think.
This is a question of whether the target is to the right or left of the line where the object is pointing. As OskarS says, you don’t need trigonometry for this at all. Just take the wedge product of the two vectors, and check the sign.
That is, if your direction is the vector (a, b), and the displacement to the target is the vector (c, d), you can compute ad – bc. If this quantity is positive turn left, else turn right.
My favorite "looks simple but is hard" task is "given object position and angle and target position return +1 if the object should turn clockwise and -1 if the object should turn counterclockwise to face the target".
I've got it subtly wrong so many times and using atan2 only solves half the problems. Now I just copypaste it from my previous game :)