It is often useful when interpolating to accelerate into the interpolation and/or decelerate out of the interpolation. For example, an object moving from one place to another looks a little better if it accelerates from its stationary state into the move, and then slows down before stopping again. Here I'll talk about how to smooth out a linear interpolation as much (or as little) as you want.
Without any smoothing, you just linearly increase the interpolation factor from zero to one. Instead of this linear movement, let's try increasing the interpolation factor as a function of time f(t). Now let's say that the length in time of the interpolation is 1 (you can easily scale this to any interpolation over any time length), and that the length of the acceleration is a, and the length of the deceleration is b. Then we want the derivative of f(t) to look something like this
Now we just need to determine the height of this trapezoid, such that the area under f'(t) is 1 so that f(1) = 1. The area under the curve is
So for the acceleration (0 ≤ t < a)
And for the linear part (a ≤ t < 1-b)
Our initial condition for this part is that f is continuous at f(a), so
So the linear segment is
And finally, for the deceleration (1-b ≤ t ≤ 1) we have
Again, to satisfy continuity at f(1-b), we need
So the deceleration segment is
And the function with the desired acceleration and deceleration is
Here is a graph of f(t) for various values of a and b.
Seriously i love your blog!
ReplyDeleteI mean as an indie game developer and a math-lover.. seriously! MOAR!
Thanks for the post
ReplyDelete