I am trying to find the shortest "path" between two rotations, measured in degrees.
Example problem:
I am given two rotations: 10
and 340
. The shortest path between these rotations would be -30
, because going below 0 degrees underflows to 359 degrees. However, I seem to be unable to get the value programmatically! Everything i've tried before (lots of random code snippets), always returned the longer path, which would be 330
with this example.
Short problem version:
- Rotation A:
10
- Rotation B:
340
- Shortest path:
-30
- Longest path:
330
- I am unable to find the logic to compute the shortest path, which is my goal.
XY Problem addendum:
I am working on game project. For this matter, i have an entity that moves through 3D-space. That movement is determined by a 3D-vector. The entity also has yaw and pitch. In the previously described example problem, Rotation A
is the yaw that the entity currently has. Rotation B
is the yaw of the current movement vector. The entity is supposed to slowly adjust its yaw to match the current vector.
I am coding this project using Java.
Thank you!