Field Notes: Physics
Engines hide the solver, but they don't hide its limits. Most physics bugs reported in game studios come down to the same handful of setup mistakes, and every one of them has a physical or numerical reason you saw earlier in this course. Here they are, with small models that show why.
1. Never scale colliders non-uniformly
A sphere collider stays a sphere: no physics engine turns it into an ellipsoid when you scale its object by (2, 1, 1). Unity and others pick the largest axis for the radius, so the collider no longer matches the mesh. Boxes have a worse problem: a box under a rotated parent with non-uniform scale gets sheared into a parallelogram, which isn't a box at all, so the engine approximates it (or gives up).
#include <iostream>
#include <iomanip>
#include <cmath>
struct V { double x, y; };
double dot(V a, V b) { return a.x * b.x + a.y * b.y; }
double len(V a) { return std::sqrt(dot(a, a)); }
int main() {
std::cout << std::fixed << std::setprecision(3) << std::boolalpha;
// 1) a unit sphere (here a circle) scaled by (2, 1)
V p{0, 1.5}; // a point above the visible mesh
bool insideMesh = (p.x / 2) * (p.x / 2) + p.y * p.y <= 1; // the real ellipse
bool insideCollider = len(p) <= 2; // the engine's sphere, radius = max scale
std::cout << "point (0, 1.5): inside mesh " << insideMesh << ", inside collider " << insideCollider << "\n";
// 2) a child box rotated 45 degrees under a parent scaled (2, 1)
const double c = std::cos(3.141592653589793 / 4), s = std::sin(3.141592653589793 / 4);
V ax{c, s}, ay{-s, c}; // child's local axes after its rotation
V wx{2 * ax.x, ax.y}, wy{2 * ay.x, ay.y}; // then the parent's non-uniform scale
double angle = std::acos(dot(wx, wy) / (len(wx) * len(wy))) * 180 / 3.141592653589793;
std::cout << "child box axes in world: angle between them " << angle << " deg (a box needs 90)\n";
}point (0, 1.5): inside mesh false, inside collider true
child box axes in world: angle between them 126.870 deg (a box needs 90)
The fix is a habit, not a setting: keep scale (1, 1, 1) on every object with a collider, put scale on a child that only holds the visible mesh, and size colliders with their own radius/extents properties. Import models at the right size in the first place.
2. Keep mass ratios moderate
Iterative solvers (Stacking, Iterations and Sleeping) pass impulses between contacts a few times per step. A heavy body resting on a light one needs many passes to "tell" the ground about its weight, so with a fixed iteration count the stack sinks and jitters. Here is a 1D two-box stack, the top box heavier than the bottom one, with no position correction or warm starting, so you see the raw solver error for different iteration counts:
#include <iostream>
#include <iomanip>
#include <algorithm>
double sink(double mBottom, double mTop, int iterations) {
const double dt = 1.0 / 60, g = -9.8;
double yA = 0, yB = 1, vA = 0, vB = 0; // A on the ground, B on A (1 m boxes)
for (int step = 0; step < 120; step++) {
vA += g * dt; vB += g * dt;
double lamGround = 0, lamAB = 0; // accumulated impulses, clamped >= 0
for (int it = 0; it < iterations; it++) {
double d = -vA * mBottom; // ground contact
double nl = std::max(0.0, lamGround + d); d = nl - lamGround; lamGround = nl;
vA += d / mBottom;
double mEff = 1 / (1 / mBottom + 1 / mTop); // A-B contact
d = -(vB - vA) * mEff;
nl = std::max(0.0, lamAB + d); d = nl - lamAB; lamAB = nl;
vA -= d / mBottom; vB += d / mTop;
}
yA += vA * dt; yB += vB * dt;
}
return (0 - yA) + (yA + 1 - yB); // total overlap after 2 s
}
int main() {
std::cout << std::fixed << std::setprecision(3);
std::cout << std::setprecision(1) << "sinking after 2 s, in cm\n";
std::cout << "mass ratio 4 iterations 8 iterations 16 iterations\n";
for (double ratio : {1.0, 5.0, 10.0, 100.0}) {
std::cout << "1:" << std::left << std::setw(9) << int(ratio) << std::right;
for (int it : {4, 8, 16}) std::cout << std::setw(13) << sink(1, ratio, it) * 100 << (it < 16 ? " " : "");
std::cout << "\n";
}
}sinking after 2 s, in cm
mass ratio 4 iterations 8 iterations 16 iterations
1:1 2.2 0.1 0.0
1:5 30.2 9.9 1.9
1:10 69.1 28.4 9.1
1:100 640.8 354.6 180.1
Read across a row: more iterations always help. Read down a column: every tenfold increase in mass ratio needs several times more iterations for the same accuracy, and at 1:100 even 16 iterations leave the stack sinking almost two meters. Real engines add position correction, warm starting and sub-stepping, which hide most of this error, but the trend is the same and shows up as jitter and slow sinking. Rules of thumb: keep the mass ratio of touching bodies under about 1:10, give a character's "heavy" crates realistic but similar masses, and if gameplay needs a giant mass (a boulder crushing a cart), make the giant kinematic or raise the solver iterations for that body only (Unity's per-Rigidbody solverIterations, Jolt's per-world solver settings, more sub-steps in Box2D v3).
3. Fast projectiles use raycasts
A bullet at 900 m/s moves 15 m per 60 Hz step. A discrete overlap test checks where it is, never where it was in between, so it tunnels through walls (Tunneling and Continuous Collision Detection). CCD helps, but for bullets the better answer is to not make them bodies at all: cast a ray from the previous position to the new one each step.
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double dt = 1.0 / 60, speed = 900, wallMin = 50.0, wallMax = 50.2; // a 20 cm wall at x = 50
double x = 0;
bool overlapHit = false, rayHit = false;
double rayHitX = 0;
for (int step = 1; step <= 5 && !rayHit; step++) {
double prev = x;
x += speed * dt;
if (x >= wallMin && x <= wallMax) overlapHit = true; // discrete test
if (prev < wallMin && x >= wallMin) { rayHit = true; rayHitX = wallMin; } // segment prev -> x
std::cout << "step " << step << ": x " << x << "\n";
}
std::cout << "overlap test hit: " << (overlapHit ? "yes" : "no") << "\n";
std::cout << "raycast hit: " << (rayHit ? "yes" : "no") << " at x " << rayHitX << "\n";
}step 1: x 15.000
step 2: x 30.000
step 3: x 45.000
step 4: x 60.000
overlap test hit: no
raycast hit: yes at x 50.000
Hitscan weapons are a single raycast; visible slow projectiles (rockets, arrows) can be bodies or kinematic objects that raycast along their path each step. Either way the ray also gives you the exact hit point and normal for decals and ricochets.
4. Physics belongs in the fixed step
Forces applied from the variable-rate update run once per rendered frame, but each one is integrated over a physics step. In Unity, AddForce(F) called from Update adds F · fixedDeltaTime / m per call, so the faster the computer, the stronger the push:
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double fixedDt = 0.02, force = 10, mass = 1;
for (int fps : {30, 60, 144}) {
double vUpdate = 0, vFixed = 0;
for (int frame = 0; frame < fps; frame++) vUpdate += force * fixedDt / mass; // AddForce in Update
for (int step = 0; step < 50; step++) vFixed += force * fixedDt / mass; // AddForce in FixedUpdate
std::cout << fps << " FPS: speed after 1 s, force in Update " << vUpdate
<< ", in FixedUpdate " << vFixed << "\n";
}
}30 FPS: speed after 1 s, force in Update 6.000, in FixedUpdate 10.000
60 FPS: speed after 1 s, force in Update 12.000, in FixedUpdate 10.000
144 FPS: speed after 1 s, force in Update 28.800, in FixedUpdate 10.000
The same applies to Godot (_physics_process, not _process) and to Unreal when you use async physics callbacks. Read input every frame; apply it in the fixed step.
5. Interpolate rendering between steps
With 50 physics steps per second on a 144 Hz display, the raw body position changes on about one frame in three, so motion stutters even though physics is perfect. Show how far the drawn position moves each frame, raw versus interpolated:
#include <iostream>
#include <iomanip>
int main() {
std::cout << std::fixed << std::setprecision(3);
const double fixedDt = 0.02, frameDt = 1.0 / 144, speed = 5;
double acc = 0, x = 0, prevX = 0, lastRaw = 0, lastSmooth = 0;
std::cout << "frame raw move interpolated move\n";
for (int f = 1; f <= 8; f++) {
acc += frameDt;
while (acc >= fixedDt) { prevX = x; x += speed * fixedDt; acc -= fixedDt; }
double smooth = prevX + (x - prevX) * (acc / fixedDt);
std::cout << std::setw(5) << f << " " << std::setw(8) << x - lastRaw << " " << std::setw(17) << smooth - lastSmooth << "\n";
lastRaw = x; lastSmooth = smooth;
}
}frame raw move interpolated move
1 0.000 0.000
2 0.000 0.000
3 0.100 0.004
4 0.000 0.035
5 0.000 0.035
6 0.100 0.035
7 0.000 0.035
8 0.000 0.035
The raw column jumps 0.1 m (one physics step at 5 m/s) on every third frame and stands still on the others. Once the first step has run, the interpolated column moves the same 0.035 m (5 m/s ÷ 144) every frame, at the cost of drawing the body up to one step behind the simulation. Turn on RigidbodyInterpolation.Interpolate for the player and anything the camera follows (Unity), Physics Interpolation (Godot), or interpolate yourself in a custom engine (Fixed Timestep and Interpolation). After a teleport, reset the interpolation so the body doesn't glide across the level.
6. Short notes
- 1 unit = 1 meter. Engines tune sleep thresholds, contact offsets and gravity for human-sized objects in meters (Unreal: centimeters). A 0.01-unit pebble or a 1,000-unit ship both behave badly.
- Move static colliders and you pay. Anything that moves needs a body: kinematic for doors, platforms and elevators. Moving a "static" collider forces broad-phase rebuilds and gives other bodies no velocity to ride on.
- Use layers before code. A collision matrix (Unity), channels/profiles (Unreal) or layer and mask bits (Godot, Box2D filters) remove pairs before the narrow phase runs; filtering in a callback runs after the expensive part.
- Thin and small is fragile. Walls should be thicker than the fastest object's per-step movement, and objects smaller than about 5 cm need CCD or a bigger collider.
- Don't fight the solver from scripts. Writing
positionorvelocityof a dynamic body every frame to "fix" it cancels contacts and wakes islands. Apply forces or impulses, or make it kinematic. - Sleeping is a feature. If a pile never sleeps, look for a script that keeps waking it (setting velocity to the same value counts).
- Combine modes surprise people. Friction and bounciness of a contact combine both materials (average, minimum, maximum or multiply); one ice material can make the whole level slippery.
Test yourself
Why should colliders keep a uniform scale of (1, 1, 1)?
- It is faster to render
- Engines can't represent scaled spheres as ellipsoids, and rotated non-uniform scale shears boxes
- Physics only supports integer scales
- Scale changes mass
A 100 kg crate rests on a 1 kg crate and the stack slowly sinks and jitters. What is the cause?
- Gravity is too high
- A large mass ratio that the iterative solver can't converge within its iterations
- Friction is zero
- The crates are asleep
Calling Unity's AddForce from Update makes a car accelerate faster on a 144 Hz monitor. Why?
- Rendering adds force
- Each call adds force times the fixed step, and Update runs more often at higher frame rates
- Unity caps forces at 60 FPS
- Interpolation adds speed