4 ms·
Yup, I felt incredibly stupid about six months ago finding out about atan2 for the first time, given the number of times I've made stupid errors using atan, and
by SamBam 6y ago
Yup, I felt incredibly stupid about six months ago finding out about atan2 for the first time, given the number of times I've made stupid errors using atan, and then correcting those errors by manually checking what quadrant I was in.
- ajuc 6y agoSame. 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 :)
- OskarS 6y agoIsn’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.
- jacobolus 6y agoThis 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.