Skip to content
GameDev Formulas
Math & physics for real games
Back to topics

All levels

Camera & Interpolation

Smoothing, blending, easing, and frame-rate-aware interpolation used for cameras and animation.

Formula

Linear Interpolation (Lerp)

Blends between two values by a parameter t.

Intuition

Lerp draws a straight line between A and B. t=0 gives A, t=1 gives B, t=0.5 gives halfway.

Equation
lerp⁡(a,b,t)=a+(b−a)t,t∈[0,1]\operatorname{lerp}(a,b,t) = a + (b-a)t, \quad t \in [0,1]

Variables

a\text{a}
Start value.
b\text{b}
End value.
t\text{t}
Interpolation factor, usually clamped to 0..1.

Explanation

Lerp is used for blending positions, colors, health bars, camera movement, and animation parameters. The key is choosing the right t. For frame-rate-independent smoothing, t should be derived from delta time.

Where it shows up in games

Camera follow, UI transitions, animation blending, damage flashes, gradual stat changes.

Implementation

JavaScript & C++
JavaScriptjavascript
function lerp(a, b, t) {
  return a + (b - a) * t;
}

function lerpVec3(a, b, t) {
  return {
    x: lerp(a.x, b.x, t),
    y: lerp(a.y, b.y, t),
    z: lerp(a.z, b.z, t),
  };
}

// Frame-rate-independent smoothing
function damp(current, target, smoothing, dt) {
  // smoothing: higher = snappier
  const t = 1 - Math.exp(-smoothing * dt);
  return lerp(current, target, t);
}
C++cpp
#include <cmath>

float lerp(float a, float b, float t) {
    return a + (b - a) * t;
}

float damp(float current, float target, float smoothing, float dt) {
    float t = 1.0f - std::exp(-smoothing * dt);
    return lerp(current, target, t);
}

Formula

Clamp

Restricts a value to a minimum and maximum range.

Intuition

Clamp is a guardrail: if a value goes below min, return min; above max, return max; otherwise keep it.

Equation
clamp⁡(x,min,max)=min⁡(max⁡(x,min),max)\operatorname{clamp}(x, min, max) = \min(\max(x, min), max)

Variables

x\text{x}
Input value.
min\text{min}
Lower bound.
max\text{max}
Upper bound.

Explanation

Clamping prevents invalid or unstable values. It's used in health systems, camera bounds, input normalization, physics limits, and UI progress. It's simple, but missing clamps cause many gameplay bugs.

Where it shows up in games

Health/stamina bounds, camera pitch limits, volume sliders, normalized input, safe interpolation parameters.

Implementation

JavaScript & C++
JavaScriptjavascript
function clamp(x, min, max) {
  return Math.min(Math.max(x, min), max);
}

let health = 100;
health = clamp(health - 150, 0, 100); // 0

let cameraPitch = clamp(95, -89, 89); // 89 degrees
C++cpp
#include <algorithm>

float clamp(float x, float minVal, float maxVal) {
    return std::min(std::max(x, minVal), maxVal);
}

// C++17 also provides std::clamp(x, minVal, maxVal);