Search code examples
javaandroidrotationangle

Figure out smallest needed rotation to target rotation from current rotation


I have the current rotation (where the smartphone is looking at) and a target angle I want the user to look at. Both are in the range of 0 to 360.

int current = 340;
int target = 45;

How can I figure out the smallest needed rotation either left or right to the target angle? Simply substracting the values makes for an inefficient rotation. A rotation to the right should be a positive value, a rotation to the left should be negative.


Solution

  • Solved it with the following one-liner:

    int neededRotation = (int) (-1* ((currentDirection - calculateAngle(x, y) + 540) % 360 - 180));