Debug System

UI overlay with live stats, and in-world draw layers for physics, bounds, and cameras.

Table of Contents

Debug System

The debug system has two parts: a UI overlay with live stats, and in-world draw layers for physics, bounds, and cameras.

Enabling Debug

Pass flags in the game config:

js
new MyGame({
    width: 800,
    height: 600,
    fps: 60,
    debug: true,           // UI overlay + stats
    debugPhysics: true,    // rigidbody visuals
    debugBounds: true,     // renderer bounds + colliders
    debugCameras: true,    // camera frustums
    debugConfig: "./debug.config.json", // optional key remap
}).start();

All draw flags can also be toggled at runtime with the keys below.


Debug Overlay

DebugOverlay builds four flat panels on a dedicated "Debug" UI layer (order 40, above "Overlay"), hidden until toggled.

CornerPanelShows
Top-leftPerformanceFPS, frame time, average frame, CPU, GC spikes
Top-rightSceneEntities, visible, physics, scene name, camera
Bottom-leftInputActive device, axis H/V, last action, gamepad status
Bottom-rightAudioBGM state, active SFX, master and SFX volume, context state

FPS and CPU are color coded green, yellow, red.

Each label needs its own style object. If you spread a shared style constant into many labels, change colors through a copy:

js
style: { ...DebugOverlay.#ROW.style }

Otherwise changing one label's color changes all of them, because they share one object.


Debug Draw Layers

Drawn in world space by the renderer on top of the scene.

Colliders (debugBounds)

VisualColorMeaning
Solid rectGreen #00E676Normal collider
Dashed rectYellow #FFD600Trigger collider
DotCyan #00E5FFCollider center
Crosshair + circleRed #FF1744Anchor point (collider.anchor, default center)
Dashed arrowOrange #FF6D00Facing direction, 0° = up, clockwise (transform.rotation.z)

Renderer bounds (debugBounds) — shown only for renderers with no collider. Blue dashed rect, center dot, and a facing arrow.

Rigidbodies (debugPhysics)

VisualMeaning
Bright purple dotMoving
Dim purple dotNearly still
Dashed purple arrowVelocity direction and magnitude
Green dot + GisGrounded is true
Red dot + AAirborne
px/s labelSpeed
m: labelMass

Cameras (debugCameras) — dashed view frustum with center crosshair. Primary camera in orange #FF6E40, others in light orange #FFAB91.


Toggle Keys

ActionDefault
Toggle overlayF2 or Ctrl + D
Toggle rigidbody debugF4
Toggle bounds + collidersF5
Toggle camerasF6

They are static properties, so you can change them in code:

js
DebugOverlay.TOGGLE_KEYS       = [KeyCode.F3];
DebugOverlay.TOGGLE_COMBO      = { key: KeyCode.G, modifier: "ctrl" };
DebugOverlay.PHYSICS_DEBUG_KEY = KeyCode.F7;
DebugOverlay.BOUNDS_DEBUG_KEY  = KeyCode.F8;
DebugOverlay.CAMERAS_DEBUG_KEY = KeyCode.F9;

Remapping Keys with JSON

Browsers use some function keys for their own shortcuts, so keys can be remapped from a JSON file without touching code.

debug.config.json:

json
{
    "keys": {
        "toggle":      "F2",
        "toggleCombo": { "key": "D", "modifier": "ctrl" },
        "physics":     "F4",
        "bounds":      "F5",
        "cameras":     "F6"
    }
}

Load it before the game starts:

js
await DebugOverlay.loadConfig("./debug.config.json");

Or pass debugConfig in the game options and the engine loads it for you. Key names match KeyCode property names. A name not found in KeyCode is used as a raw key string. Only the keys listed in the JSON change.


DebugStats

DebugStats collects performance numbers independently of the overlay, so you can read them from anywhere.

js
game.debugStats.fps
game.debugStats.frameTime   // ms for the last frame
game.debugStats.avgFrame    // average over the last 60 frames
game.debugStats.cpuUsage    // frame time as % of the 16.67ms budget
game.debugStats.gcSpikes    // long-gap frames in the current second

The game loop wraps each frame:

js
update(dt) {
    this.debugStats?.beginFrame();
    // ... update logic ...
    this.debugStats?.endFrame();
}

GC spikes are a heuristic: a gap over 50ms between frames counts as a spike. CPU usage is an estimate from frame time, not a real CPU measurement.


Debug API Reference

DebugOverlay

MemberDescription
init()Build panels and the "Debug" layer
update(dt)Handle toggle keys and refresh labels
show() / hide() / toggle()Control visibility
isVisibleCurrent visibility
trackAction(name)Record the last fired input action
DebugOverlay.loadConfig(path)Remap keys from JSON (async)

Renderer flags

FlagControls
renderer.debugPhysicsRigidbody visuals
renderer.debugBoundsRenderer bounds and colliders
renderer.debugCamerasCamera frustums

Game config

OptionDescription
debugCreate DebugStats and DebugOverlay
debugPhysics / debugBounds / debugCamerasInitial state of each draw layer
debugConfigPath to the key remap JSON