If you try to slowly rotate toward a specific direction in a 360-degree range, the sprite might go left or right based on its position in the range rather than the shortest path. This can cause ugly jumps when passing specific directions. By checking abs(new direction - old direction) < 180, we can see whether rotation to the left or to the right is shorter. By using (dir mod 360), we can make the sprite go one way or the other.