Friday, March 2, 2012

2D Angular Interpolation

It has been a while since my last post. I've done a lot of work with the Farseer physics engine and intend to write up a post on how I've managed to use the Tiled polygon tool to create static Farseer bodies that are loaded though Nick Gravelyn's TiledLib library.

Anyways I'm getting off topic. I just came across this stackoverflow question. I expected googling "2d angular interpolation" to give me a simple function I could pass on to the asker. However, I found few and they seemed overly complex so I wrote my own.

The concept is that I can lerp to either the angle specified or to the angle a full revolution from the one specified.

So for example if the object is rotated 50 degrees and the destination is 290 degrees. I add a second destination of -60 degrees.

Since 50 - -60 = 110
And 290 - 50 = 240
We know that rotating towards -60 is quicker and I simply lerp the current angle towards -60.

Anyways I hope that now anyone googling "2d angular interpolation" finds this a simple, easy to understand funtion to use.