[SOLVED] Smoothly interpolate between two points?

What would be the best way to smoothly interpolate between two points? For example, I have a player that is controlled by WASD, I also have a ‘camera’ which would follow the player’s x & y position smoothly. Then the game would render the level+player based off the cameras position.

Also I’m not sure how to make the camera x & y slow down the closer it gets to the player x & y. Any ideas?

Its pretty subtle in this example.

chTQaTEvXy0

I believe you are looking for a lerp function http://en.wikipedia.org/wiki/Linear_interpolation

float lerp(float point1, float point2, float alpha)
{
    return point1 + alpha * (point2 - point1);
}

Thats it! Just implemented it and it works perfectly. Thanks!


float cubic_scurve3(float alpha)
{
  return alpha*alpha*( 3.0f - 2.0f*alpha );
}

float smoothed = lerp(point1, point2, cubic_scruve3(alpha));

Just for some examples of more elaborate ones.


See InterpolationTest in libgdx to visualize them.