0

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!

jaylawl
  • 105
  • 6
  • 1
    Does this help: [Smallest difference between two angles?](https://stackoverflow.com/questions/6722272/smallest-difference-between-two-angles) Although the code is C#, I believe you can adapt it to your needs. – Abra Mar 05 '22 at 11:44
  • 1
    Does this help: [How can I find the difference between two angles?](https://stackoverflow.com/questions/1878907/how-can-i-find-the-difference-between-two-angles) – Abra Mar 05 '22 at 11:46
  • 1
    Here's one with Java code: [How do I calculate the difference of two angle measures?](https://stackoverflow.com/questions/7570808/how-do-i-calculate-the-difference-of-two-angle-measures) – Abra Mar 05 '22 at 11:47
  • 1
    Thank you guys! I am sure to find my answer in those. I assure you that i tried to web-search an answer before opening my own question - it seems like my phrasing was just way off, otherwise i should have found these. Thanks again --- i marked this question as duplicate – jaylawl Mar 05 '22 at 11:49

0 Answers0