Math for Programming / Part 1: Space and motion

Interpolation: finding the in-between

One small formula, lerp, powers health bars, color fades, smooth cameras, and almost every animation you see in an app.

Lesson 7 of 13About 10 minutesBuilds on: linear equations

Going part of the way

To interpolate is to find a value between two others. The workhorse is linear interpolation, nicknamed lerp: lerp(a, b, t) = a + (b − a) × t. The number t is a fraction of the way: t = 0 gives a, t = 1 gives b, and t = 0.5 lands exactly in the middle.

Worked example

lerp(10, 50, 0.25) = 10 + 40 × 0.25 = 20. A quarter of the way from 10 to 50 is 20. Lerp works on each coordinate of a vector too: a quarter of the way from (0, 0) to (100, 40) is (25, 10).

Running it backward and remapping

Inverse lerp asks the opposite question: what fraction of the way is a value? inverse_lerp(10, 50, 20) = (20 − 10) ÷ (50 − 10) = 0.25. Combine the two and you can remap a number from one range to another. A health bar is a remap: health from 0 to 100 becomes a bar width from 0 to 200 pixels. Health 75 is 0.75 of the way, so the bar is 150 pixels wide.

GDScript
var t := inverse_lerp(0.0, 100.0, health)   # 75 -> 0.75
bar.size.x = lerp(0.0, 200.0, t)            # 0.75 -> 150
# or in one step:
bar.size.x = remap(health, 0.0, 100.0, 0.0, 200.0)

Easing: making motion feel natural

If t grows at a constant rate, lerp moves at a constant speed, which can look robotic. Real objects speed up and slow down. Easing bends t before using it. Squaring it, t², starts slowly and finishes fast (an ease-in). The smoothstep curve, 3t² − 2t³, starts slowly, speeds up, and settles gently (an ease-in-out). At t = 0.25, smoothstep gives 0.15625, while at t = 0.5 it gives exactly 0.5.

Figure

Slide t from 0 to 1, or press play.

linear: tease-in: t²smoothstep: 3t² − 2t³

The “lerp every frame” trick

A common camera trick is to move 10% of the remaining distance toward the target each frame: position = position.lerp(target, 0.1). The camera rushes at first and then glides in. After 10 frames, 0.9¹⁰ ≈ 0.35 of the distance remains. That is exponential decay, the same shape as a cooling cup of coffee.

Common mistake

The fixed 0.1 makes the trick frame-rate dependent: at 120 fps the camera catches up twice as fast as at 60 fps. A frame-rate-independent version uses a weight that depends on delta, such as 1 - exp(-speed * delta).

Where this comes from in school math

Lerp is the equation of a line in disguise. Rewrite a + (b − a)t and you get a slope (b − a) and an intercept a: y = mx + b with t in place of x. Inverse lerp solves that linear equation for t. Easing curves are polynomial functions, the kind you graph in Algebra II.

Where you will see it

  • Animation: sliding menus, fading colors, and camera movement.
  • Interfaces: progress bars, volume sliders, and health bars.
  • Color: blending between two colors channel by channel.
  • Data: estimating a value between two measurements.

Exercises

Compute lerp(−4, 8, 0.75).

−4 + 12 × 0.75 = 5.

A slider runs from x = 100 to x = 300 and controls volume from 0 to 1. The handle is at x = 160. What is the volume?

inverse_lerp(100, 300, 160) = 60 ÷ 200 = 0.3.

What does the ease-in curve t² give at t = 0.5?

0.25. The object is only a quarter of the way there at the halfway time.