TExcelTAD
Home › Game Physics & Animation › Physics and Animation Toolkit

Physics and Animation Toolkit

Physics bugs are hard to see. An object is pushed out of a wall a centimeter too far, a contact normal points the wrong way for one frame, a bone flips for three frames: you can't print your way through that. Every serious physics programmer leans on four kinds of tool: debug drawing, recorders that let you scrub a session, profilers, and reference implementations to compare against. This lesson lists the ones worth installing for each stack.

Debug drawing in the engines

The first thing to turn on when physics misbehaves is a view of the collision shapes, not the meshes. Half of all "physics bugs" are a collider that doesn't match the art.

Engine Where What you get
Unity Window > Analysis > Physics Debugger colliders colored by body type, sleeping state, contacts and queries; filter by layer
Unity OnDrawGizmos + Gizmos.DrawWireSphere / Debug.DrawRay your own shapes: raycasts, ground probes, IK targets
Unreal show collision console command collision shapes in the viewport
Unreal Chaos Visual Debugger (Tools menu) records a session; scrub frames, inspect bodies, contacts and scene queries
Unreal DrawDebugLine, DrawDebugSphere, DrawDebugCapsule your own shapes, with a lifetime
Godot Debug > Visible Collision Shapes (before running) collision shapes and contact points in the running game
Godot Debugger > Monitors physics counters: active objects, collision pairs, islands

Draw your own debug shapes for everything the engine can't know about: the ground probe of a character controller, the predicted landing point, IK targets, the cone of a vision check. Leave them in the code behind a toggle; you will need them again.

Library tools

  • PhysX (Unity, many custom engines): the PhysX Visual Debugger records a PhysX scene from a running application; PhysX 5 moved to OmniPVD, a recording format viewed in NVIDIA's tools. Inside Unity you use the Physics Debugger instead.
  • Jolt Physics (Godot's default 3D engine since 4.6, also used standalone): the repository's Samples application has dozens of interactive test scenes (stacking, ragdolls, character controllers, vehicles) and the JoltViewer replays recordings made with its debug renderer.
  • Box2D: the samples app in the repository is the best way to learn the library and to reproduce a bug in isolation, with every option (sub-steps, sleeping, continuous collision) on a checkbox.
  • Rapier: the rapier_testbed2d / rapier_testbed3d crates give you a windowed testbed for your own scenes, and the JS bindings have a debugRender() call that returns line buffers to draw with any renderer.

Tip: When a bug looks like "the engine is broken", rebuild the smallest scene that shows it in the library's own samples app. Nine times out of ten you find a scale, mass or unit problem in your setup; the tenth time you have a perfect bug report.

Write your own debug draw

A custom engine needs debug draw on day one. The interface is tiny: line(a, b, color), circle(c, r, color), box(min, max, color), collected each frame and drawn last, on top of everything. Even a text-mode version is useful in a headless test. Here is a debug draw that rasterizes shapes and a contact normal into characters:

cpp · try it yourself
#include <iostream>
#include <cmath>
#include <string>
#include <vector>

struct Canvas {
    int w, h;
    std::vector<std::string> rows;
    Canvas(int width, int height) : w(width), h(height), rows(height, std::string(width, '.')) {}
    void plot(double x, double y, char c) {
        int ix = int(std::lround(x)), iy = int(std::lround(y));
        if (ix >= 0 && ix < w && iy >= 0 && iy < h) rows[h - 1 - iy][ix] = c;   // y up
    }
    void line(double ax, double ay, double bx, double by, char c) {
        int n = int(std::ceil(std::max(std::fabs(bx - ax), std::fabs(by - ay)))) + 1;
        for (int i = 0; i <= n; i++) plot(ax + (bx - ax) * i / n, ay + (by - ay) * i / n, c);
    }
    void box(double x0, double y0, double x1, double y1, char c) {
        line(x0, y0, x1, y0, c); line(x1, y0, x1, y1, c); line(x1, y1, x0, y1, c); line(x0, y1, x0, y0, c);
    }
    void circle(double cx, double cy, double r, char c) {
        for (int i = 0; i < 48; i++) {
            double a = i * 2 * 3.141592653589793 / 48;
            plot(cx + r * std::cos(a), cy + r * std::sin(a), c);
        }
    }
    void print() const { for (const auto& r : rows) std::cout << r << "\n"; }
};

int main() {
    Canvas dbg(36, 12);
    dbg.box(2, 1, 33, 3, '#');                  // a static ground box
    double cx = 12, cy = 6.6, r = 3.8;           // a ball sinking 0.2 into the ground
    dbg.circle(cx, cy, r, 'o');
    double contactY = 3, depth = (contactY + r) - cy;
    dbg.line(cx, contactY, cx, contactY + 3, '|');   // contact normal, pointing up out of the ground
    dbg.plot(cx, contactY + 3, '^');
    dbg.plot(cx, contactY, '*');                 // contact point
    dbg.print();
    std::cout << "contact at (" << cx << ", " << contactY << "), normal (0, 1), depth " << depth << "\n";
}
....................................
..........ooooo.....................
.........o.....o....................
........o.......o...................
........o.......o...................
........o...^...o...................
........oo..|..oo...................
.........oo.|.oo....................
..########oo*oo###################..
..#..............................#..
..################################..
....................................
contact at (12, 3), normal (0, 1), depth 0.2

The ball's outline dips into the ground box, the * marks the contact point and the arrow the normal: the same three things the Unity, Unreal and Godot debuggers draw. Colour-code your real debug draw: one colour for sleeping bodies, one for awake, red for contacts deeper than the allowed slop.

Profilers

Physics cost grows with contact pairs and solver islands, not with object count, so you want counters next to the timings:

  • Unity Profiler: the Physics module shows active bodies, contacts and the time spent in the simulation step; the CPU timeline shows how many FixedUpdate calls ran per frame (more than one per frame means you are paying for catch-up).
  • Unreal Insights: trace Chaos and animation work on the game thread and worker threads; stat physics and stat anim give quick on-screen numbers.
  • Godot: the Debugger's Profiler (physics frame time) and Monitors (physics objects, pairs, islands).
  • Tracy: an open-source frame profiler for C and C++ engines. Wrap your broad phase, narrow phase, solver and skinning in zones and you see exactly which stage blew the frame.

Animation tools

Tool Use
Blender rigging, skinning weights, animation, export to glTF 2.0 or FBX; the Rigify add-on generates game-ready rigs
glTF 2.0 the open runtime format: skins, joints, inverse bind matrices, keyframe channels with STEP, LINEAR and CUBICSPLINE interpolation
ozz-animation an open-source C++ runtime: sampling, blending, additive layers, IK helpers and skinning; a good reference for your own engine
ACL (Animation Compression Library) open-source compression for keyframe data, with plugins for Unreal
Unity Animation Rigging, Unreal Control Rig / IK Rig, Godot 4.6 IK modifiers runtime IK and procedural constraints inside each engine

Online motion libraries and auto-riggers are handy for prototypes; check their current licence terms before shipping their animations.

Debugger tips

  • Trap NaNs where they are born. On Linux with glibc, feenableexcept(FE_INVALID | FE_DIVBYZERO) turns the first invalid float operation into a crash at the exact line; on MSVC use _controlfp_s. Turn it on in debug builds of your simulation.
  • Conditional breakpoints on a body ID (body.id == 42 && step == 1180) beat stepping through a loop over 2,000 bodies.
  • Data breakpoints (watchpoints) on a body's position find the code that teleports it.
  • Visualizers: add a .natvis file (Visual Studio) or GDB/LLDB pretty-printers for your Vec2, Quat and Body types so the debugger shows (1.2, 3.4) instead of nested structs.

The next lesson, Debugging Physics, turns these into techniques: record and replay, state hashes and energy monitoring. For the math types the tools display, see Math Libraries.

Test yourself

What is the first thing to enable when a physics object behaves strangely?

  1. A higher frame rate
  2. A view of the collision shapes (Physics Debugger, show collision, Visible Collision Shapes)
  3. More solver iterations
  4. Shadows

Which counters explain physics cost best?

  1. Number of meshes
  2. Contact pairs and solver islands, next to the step time
  3. Texture memory
  4. Number of scripts

How do you find the exact line that first produces a NaN in a C++ simulation on Linux?

  1. Print every value
  2. Enable floating-point exceptions with feenableexcept(FE_INVALID) in a debug build
  3. Switch to float
  4. Use a release build