So, assuming you have angles previousAngle and nextAngle, and you want to transition from the first to the second smoothly. If it’s like from 358 to 2, a simple linear interpolation will awfully go through almost a full circle down from 358 to 2, when a simple four degree transition forward would have been enough, i.e. you should have been going to 362 degrees instead. So, how to figure out what’s the shortest transition?
I remember hitting against this problem numerous times, always remembering that I have solved it previously but always seem unable to find the previous solution. And I still can’t just write it out by heart.
Once again I had to solve this, so this time I tried to Google for it (“angle wrap delta”), and found out that somebody named Jason S had posted a nice generalization to a related question in Stackoverflow.com.
For your convenience, I’m posting a modified C version of the code here.
(more…)