WHAT IS A LERP?
The easy, boring bit
Linear Interpolate (Lerp) is a simple mathematical function that is used to find a value somewhere between two inputs. Specifically, we’re looking at the use of Lerp in relation to a transform position.
There are 3 parameters in a Lerp. The first two, a & b, could also be called Start & End, Low & High or Min & Max, depending on context.
The final parameter, t, is sometimes called Time, Alpha, Delta, Blend, or Mix.
Most math libraries support a & b being floating point numbers or vectors (and therefore also colours). In all cases, t is a floating point number.
Some functions clamp t into the range of 0 -> 1, others allow any value for t.
a + (b – a) * t;
It’s that simple. Whilst it’s good to understand the maths behind the function, bare in mind most engines have a math library which has the function built in.
HOW DO WE USE IT?
The classic method
There are many uses for Lerp. The one which we will be focusing on in this article is;
Each frame, interpolate a vector position from point a to point b by a fraction of delta time.
Specifically we’re looking at the following case:
lerp(currentPosition, targetPosition, deltaTime * strength)
By moving some fraction of the remaining distance between where we are currently, and where we would like to be, we move in progressively smaller steps towards our target, giving the appearance of smooth motion.
As we can see above, the movement eases in. Over the course of the movement, the velocity is largest on the first frame and smallest on last frame. This is because the distance between a and b is largest on the first frame, and lerp moves a fraction of the distance between a and b per frame.
We don’t have any concept of persistent velocity here, so the moment the target moves we get a sharp change in velocity followed by a smooth ease in.
3D POSITION INTERPOLATION
How many dimensions?
The maths being spring motion in a single dimension can easily be transferred across to multidimensional equations by simply isolating each dimension as its own spring, calculating all the springs, then recombining into a multidimensional vector.
The code included on this page has methods for 1D, 2D, and 3D calculations. If anyone was feeling especially clever it wouldn’t be difficult to add methods for 4D/Colour interpolation.
The original code comes from Ryan Juckett. We have translated his code into C# and made it Unity friendly. There are two stages to its use – The first is to call CalcDampedSpringMotionParams in order to convert from AF/DR into a set of coefficients which can be used in the Calculate method. This can be done every frame if you’re tweaking values, but should really be cached at the start of your application to avoid the expensive function call in the hot path.
We’ve opted to use ref parameters for current state and velocity.