TExcelTAD
Home › Game Physics & Animation › Game Physics & Animation Intro

Game Physics & Animation Intro

A crate tumbles down stairs, a rope swings, a character walks up a ramp and plants a foot on a step. Each of these looks effortless on screen, and each is a small program running 50 to 120 times a second: integrate the motion, find what touches what, push things apart, then pose a skeleton on top. This course opens that box. You will write the pieces of a physics engine and an animation system yourself, in short C++ programs you can run, and then see how PhysX (Unity), Chaos (Unreal), Jolt (Godot), Box2D and Rapier do the same jobs.

You will almost never ship your own rigid-body engine. You will, however, spend years configuring, debugging and working around someone else's. Knowing why a stack jitters, why a bullet passes through a wall or why a foot slides makes those years much shorter.

What you will learn

Section The question it answers
Motion How do positions and velocities move forward in time without blowing up?
Collision detection Which of thousands of objects might touch, and which actually do?
Collision response How hard, and in which direction, do I push them apart? How do they slide, bounce and come to rest?
Constraints How do I build springs, ropes, hinges and ragdolls?
Character controllers How does a player walk on slopes and stairs without rolling like a ball?
Animation How do keyframes, skeletons, skinning, blending and IK turn data into a moving character?
Engine mapping What are these called in Unity, Unreal, Godot, Box2D and Rapier?
Toolkit and field notes Which tools find physics bugs, and which habits prevent them?

The physics step in one program

Every physics engine runs the same loop with a fixed time step. Here it is at its smallest: one ball, gravity, a floor, and a bounce that loses some energy.

cpp · try it yourself
#include <iostream>
#include <iomanip>

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double dt = 1.0 / 60.0, gravity = -9.81, restitution = 0.5, radius = 0.25;
    double y = 2.0, vy = 0.0;                       // a ball dropped from 2 m

    for (int step = 1; step <= 120; step++) {
        vy += gravity * dt;                         // 1. integrate forces into velocity
        y += vy * dt;                               // 2. integrate velocity into position
        if (y < radius) {                           // 3. detect: is the ball inside the floor?
            y = radius;                             // 4. resolve: push it out...
            if (vy < 0) vy = -vy * restitution;     //    ...and reflect the velocity, losing energy
        }
        if (step % 15 == 0) std::cout << "t=" << step * dt << " s  y=" << y << "  vy=" << vy << "\n";
    }
}
t=0.250 s  y=1.673  vy=-2.453
t=0.500 s  y=0.733  vy=-4.905
t=0.750 s  y=0.569  vy=1.472
t=1.000 s  y=0.610  vy=-0.981
t=1.250 s  y=0.307  vy=0.981
t=1.500 s  y=0.250  vy=0.736
t=1.750 s  y=0.250  vy=0.057
t=2.000 s  y=0.250  vy=0.054

Look at the last two lines: the ball sits on the floor, yet vy is still a small positive number. Every step gravity pulls it into the floor and the bounce throws it back out, so it never quite comes to rest. That buzzing "resting contact" is a real problem in real engines, and the Stacking, Iterations and Sleeping lesson shows how they handle it.

Those four numbered comments are the whole course in miniature. Real engines replace each line with something better: step 1 and 2 with a stable integrator (the Motion section), step 3 with a broad phase and exact shape tests (Collision Detection), and step 4 with impulses, friction and position correction that work for many bodies at once (Collision Response).

How the examples work

  • C++20, run on Compiler Explorer. Each example defines the small structs it needs (Vec2, Body, Quat) so you can see every line. Output uses fixed precision (std::setprecision(3)) so it matches on every compiler, and tiny negative values are cleaned up so you never see -0.000.
  • Canvas demos in plain JavaScript run in a sandboxed frame in the lesson: integrator comparison, tunneling, a box stack, a rope and an IK chain you drag with the mouse.
  • Engine code (Unity C#, Unreal C++, GDScript, Box2D's C API, Rapier) can't run on this site. It is shown as real, current API code next to a runnable C++ model of the same idea.

Conventions

  • SI units: meters, kilograms, seconds. Gravity is -9.81 m/s² on the y axis (most 2D examples use y-up, like Box2D).
  • Fixed step: dt = 1/60 s unless a lesson says otherwise.
  • Inverse mass: bodies store invMass = 1 / mass, and static bodies have invMass = 0, which is how engines make walls immovable without special cases.
  • Math notation follows Math for Games: · for the dot product, × for the cross product, |v| for length.

Prerequisites

This course builds directly on Math for Games. You should be comfortable with vectors, the dot product, the cross product, AABBs, the Separating Axis Theorem and quaternions. Each lesson links to the math it uses instead of repeating it. You also need basic C++ (structs, std::vector, loops); see the C++ course.

If you haven't written a fixed-step game loop yet, The Fixed-Step Loop in the canvas course is a playable introduction.

Where this course fits

You want to… Take afterwards
Use physics and animation in an engine Unity with C#, Godot 4 with GDScript or Unreal Engine 5 with C++
Write engine systems Game Engine Architecture in C++
Make characters think Game AI
Keep physics in sync over a network Multiplayer Game Networking

Test yourself

In the bouncing-ball program, what does restitution = 0.5 do?

  1. Halves gravity
  2. Keeps half of the speed after each bounce
  3. Moves the floor up by half a meter
  4. Runs the loop at half speed

Why do engines store invMass instead of mass?

  1. It uses less memory
  2. An inverse mass of 0 makes a body immovable without special-case code
  3. Division is faster than multiplication
  4. Mass can't be negative

Which part of the physics step does a broad phase speed up?

  1. Integrating velocity
  2. Finding which pairs of objects might be touching
  3. Applying friction
  4. Blending animations