Rotation Interpolation

Rob picture Rob · Apr 25, 2010 · Viewed 22.6k times · Source

NB: I'll present this question in degrees purely for simplicity, radians, degrees, different zero-bearing, the problem is essentially the same.

Does anyone have any ideas on the code behind rotational interpolation? Given a linear interpolation function: Lerp(from, to, amount), where amount is 0...1 which returns a value between from and to, by amount. How could I apply this same function to a rotational interpolation between 0 and 360 degrees? Given that degrees should not be returned outside 0 and 360.

Given this unit circle for degrees:

Unit Circle

where from = 45 and to = 315, the algorithm should take the shortest path to the angle, i.e. it should go through zero, to 360 and then to 315 - and not all the way round 90, 180, 270 to 315.

Is there a nice way to achieve this? Or is it going to just be a horrid mess of if() blocks? Am I missing some well understood standard way of doing this? Any help would be appreciated.

Answer

user151496 picture user151496 · Jan 24, 2013

I know this is 2 years old, but I've recently been looking around for the same problem and I don't see an elegant solution without ifs posted in here, so here it goes:

    shortest_angle=((((end - start) % 360) + 540) % 360) - 180;
    return start + (shortest_angle * amount) % 360;

that's it

ps: of course, % is meaning modulo and shortest_angle is the variable that holds the whole interpolation angle