I've written a program (see on JS Fiddle) that rotates my player towards the mouse cursor at a constant speed. I've computed the angle to the point using the Math.atan2
function, but when incrementing or decrementing the player's rotation towards that value, it does not always take the shortest path.
This image describes the logic I intend to evaluate:
The problem appears when you cross a specific angle, the player rotates the long way around to reach the cursor, instead of taking the shortest path. How can I always take the shortest path at a constant speed?
Many thanks.
var dot = player.x*-mouse.y + player.y*mouse.x;if(dot > 0){ player.r -= inc; }else if(dot < 0){ player.r += inc; }
\$\endgroup\$