Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Same.

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 adbc. If this quantity is positive turn left, else turn right.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: