Lerp, Inverse Lerp and Remap
Lerp (linear interpolation) is the "hello world" of animation math: blend from a to b by a fraction t.
lerp(a, b, t) = a + (b - a) * t t = 0 gives a, t = 1 gives b, t = 0.5 is halfwayIt works for numbers, vectors, and colors alike, component by component. Two close relatives complete the toolbox: inverse lerp answers "how far between a and b is this value?", and remap converts a value from one range to another.
Lerp
#include <iostream>
#include <iomanip>
double lerp(double a, double b, double t) { return a + (b - a) * t; }
struct Vec2 { double x, y; };
Vec2 lerp(Vec2 a, Vec2 b, double t) { return {lerp(a.x, b.x, t), lerp(a.y, b.y, t)}; }
struct Color { double r, g, b; };
Color lerp(Color a, Color b, double t) { return {lerp(a.r, b.r, t), lerp(a.g, b.g, t), lerp(a.b, b.b, t)}; }
int main() {
std::cout << std::fixed << std::setprecision(2);
for (double t : {0.0, 0.25, 0.5, 1.0, 1.5}) {
Vec2 p = lerp(Vec2{0, 0}, Vec2{10, 4}, t);
Color c = lerp(Color{1, 0, 0}, Color{0, 0.5, 1}, t);
std::cout << "t=" << t << " number " << std::setw(6) << lerp(100, 200, t)
<< " point (" << p.x << ", " << p.y << ")"
<< " color (" << c.r << ", " << c.g << ", " << c.b << ")\n";
}
}t=0.00 number 100.00 point (0.00, 0.00) color (1.00, 0.00, 0.00)
t=0.25 number 125.00 point (2.50, 1.00) color (0.75, 0.12, 0.25)
t=0.50 number 150.00 point (5.00, 2.00) color (0.50, 0.25, 0.50)
t=1.00 number 200.00 point (10.00, 4.00) color (0.00, 0.50, 1.00)
t=1.50 number 250.00 point (15.00, 6.00) color (-0.50, 0.75, 1.50)
With t = 1.5 lerp extrapolates past b, and the color goes negative, which is meaningless. Clamp t to [0, 1] whenever overshoot would be a bug. C++20 has std::lerp in <cmath>, which is exact at both ends.
Note: The form
a * (1 - t) + b * tis mathematically the same, and it returns exactlybatt = 1in floating point, whilea + (b - a) * tmay be off by a rounding error. The difference matters for things like "has the door reached its end position?".
Inverse lerp
Inverse lerp is lerp solved for t:
inverseLerp(a, b, v) = (v - a) / (b - a)It turns a raw value into a 0-to-1 fraction: how far through the level is the player, how full is the health bar, how close is the enemy within its detection range.
Remap
Chain the two to move a value from one range to another. "Map speed 0..12 m/s to an engine pitch of 0.8..1.6", "map distance 5..50 m to a volume of 1..0":
#include <iostream>
#include <iomanip>
#include <algorithm>
double lerp(double a, double b, double t) { return a + (b - a) * t; }
double inverseLerp(double a, double b, double v) { return (v - a) / (b - a); }
double remap(double inA, double inB, double outA, double outB, double v, bool clamp = true) {
double t = inverseLerp(inA, inB, v);
if (clamp) t = std::clamp(t, 0.0, 1.0);
return lerp(outA, outB, t);
}
int main() {
std::cout << std::fixed << std::setprecision(3);
std::cout << "health 35 of 0..140 = " << inverseLerp(0, 140, 35) << " of the bar\n";
for (double speed : {0.0, 6.0, 12.0, 20.0})
std::cout << "speed " << std::setw(6) << speed << " -> pitch " << remap(0, 12, 0.8, 1.6, speed) << "\n";
for (double dist : {2.0, 5.0, 27.5, 50.0, 80.0})
std::cout << "distance " << std::setw(6) << dist << " -> volume " << remap(5, 50, 1, 0, dist) << "\n";
}health 35 of 0..140 = 0.250 of the bar
speed 0.000 -> pitch 0.800
speed 6.000 -> pitch 1.200
speed 12.000 -> pitch 1.600
speed 20.000 -> pitch 1.600
distance 2.000 -> volume 1.000
distance 5.000 -> volume 1.000
distance 27.500 -> volume 0.500
distance 50.000 -> volume 0.000
distance 80.000 -> volume 0.000
Notice that the output range can run backwards (volume goes from 1 down to 0), and that clamping keeps a speed of 20 from producing a pitch beyond 1.6.
Moving along a path at constant speed
Lerp with t = elapsed / duration moves an object from A to B in exactly duration seconds, regardless of frame rate, because t comes from time, not from the number of frames:
#include <iostream>
#include <iomanip>
#include <algorithm>
struct Vec2 { double x, y; };
Vec2 lerp(Vec2 a, Vec2 b, double t) { return {a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t}; }
int main() {
std::cout << std::fixed << std::setprecision(2);
Vec2 from{0, 0}, to{6, 3};
const double duration = 1.5; // seconds
for (double fps : {4.0, 2.0}) { // two different frame rates
std::cout << fps << " FPS:";
double elapsed = 0;
while (elapsed < duration) {
elapsed += 1 / fps;
double t = std::min(elapsed / duration, 1.0);
Vec2 p = lerp(from, to, t);
std::cout << " (" << p.x << "," << p.y << ")";
}
std::cout << "\n";
}
}4.00 FPS: (1.00,0.50) (2.00,1.00) (3.00,1.50) (4.00,2.00) (5.00,2.50) (6.00,3.00)
2.00 FPS: (2.00,1.00) (4.00,2.00) (6.00,3.00)
Both frame rates arrive at (6, 3) after 1.5 seconds; the slower one just shows fewer in-between positions. Compare that with the very common position = lerp(position, target, 0.1) every frame, which depends on the frame rate. That pattern is the subject of the Damping lesson.
Test yourself
What is lerp(10, 30, 0.25)?
- 12.5
- 15
- 20
- 7.5
What does inverseLerp(20, 60, 50) return?
- 0.5
- 0.75
- 0.8
- 30
Why is t = elapsed / duration frame-rate independent?
- Because it is clamped
- Because t is computed from time, not from the number of frames
- Because lerp is linear
- It is not