Derivatives measure rates of change, how fast something is changing at any given moment. In game engines, this translates to velocity from position, acceleration from velocity, and many other real-time calculations.

The Mathematical Definition

A derivative is defined using Limit:

This says: “What’s the slope as we make the interval infinitely small?”

Why Care About Derivatives?

Derivatives are essential for simulating realistic physics in video games, allowing us to approximate continuous processes in a digital world.

In game engines like Unity, derivatives help calculate velocities, accelerations, and smooth animations. For instance, the physics timestep controls how finely we discretize time, approaching the continuous limit as the step size approaches zero.

Game Engine Applications

Velocity from Position

If you have a position function p(t) = t², the derivative gives velocity:

Collision Detection

Engines use derivatives to find the exact moment and location of collisions by analyzing the rate of change of distance between objects.

Animation Curves

Smooth animations use Bézier curves, whose derivatives give smooth transitions between keyframes.

Code Implementation

// Numerical derivative approximation
float derivative(std::function<float(float)> f, float x, float h = 1e-5) {
    return (f(x + h) - f(x)) / h;
}
 
// Physics example: velocity from position
Vector3 calculateVelocity(Vector3 oldPos, Vector3 newPos, float deltaTime) {
    return (newPos - oldPos) / deltaTime; // Approximate derivative
}