Keep perception and gameplay apart. In CamPlay, eight of the nine games read one shared pose engine through a single per-frame snapshot (speed, jump, duck, lean, lane). Inference is capped at about 22 Hz so the 3D renderer keeps its frame budget, and multiplayer syncs a random seed instead of the game world, so no game server is needed.
CamPlay is nine quite different games: runners, party games, a dodge game, a skier, three slap games and a hand-tracked rock paper scissors duel. Eight of them are driven by one webcam pose engine, running alongside real-time 3D rendering and live multiplayer, all in a browser tab. This article is the architecture tour: the boundaries that keep it maintainable and the budgets that keep it fast. It is written for people building their own motion-controlled games, so take whatever is useful.
How should input and game code be separated?
The codebase enforces one rule above all: input and game never touch. The pose side (camera, model, signal extraction) knows nothing about games, and the game side (physics, scoring, rendering) knows nothing about cameras. They meet at exactly one interface, a per-frame snapshot function each game polls:
flowchart TD
subgraph INPUT["Input stack — knows nothing about games"]
CAM["Camera"] --> ML["PoseLandmarker<br/>WASM · GPU · ~22 Hz"] --> ENG["Pose engine<br/>landmarks → signals"]
end
ENG -- "poll(): one snapshot per tick" --> GAME
KB["Keyboard fallback"] -. "merges on top" .-> GAME
subgraph GAMES["Game stack — never sees the camera"]
GAME["Game loop<br/>physics · scoring · calories"] --> R3["three.js renderer<br/>60 fps budget"]
end
GAME -- "CustomEvents (camplay:*)" --> PLAT["Platform (React)<br/>auth · rooms · leaderboards"]// the entire input⇄game contract
const snap = controls.poll(now);
// {
// usingPose: bool — pose or keyboard fallback?
// tracking: bool — is the body confidently visible?
// speed: 0..1 — continuous jog intensity
// jump: bool — one-shot edge, true for one poll
// ducking: bool — held state
// lean: -1..1 — smoothed analog
// lane: 0|1|2 — which third of the frame
// }A synchronous snapshot beats an event stream for game input. The game samples state exactly once per simulation tick, so there is no event-queue lag between what the player did and what this frame simulates. One-off actions (jump) are edge flags inside the snapshot, and held actions (duck, lean) are levels. A staleness guard rides along: if the pose stream stalls for 350 ms, the snapshot reports tracking lost instead of replaying the last known speed forever. That is the difference between “the game paused politely” and “the character sprinted into a wall while the model hiccuped”.
The strict boundary pays off when adding a game: it costs a registry entry, a folder of game code and one mount registration, and the input engine, tutorial scaffolding, camera plumbing and frame coach come with it. The runners’ keyboard fallback (arrow keys when no camera is available) merges into the same snapshot, so they support both input modes without per-game input code. And, as covered in the on-device tracking article, the only code that touches the camera has no network pathway at all.
Games that need more than the snapshot
Two kinds of game needed input the shared snapshot doesn’t carry, and both were added without changing the pose engine:
- The slap games (Slap Derby, Zombie Slap and Pera nai, Chill!) import an opt-in hand-swing module. It reads the same pose landmarks the game already has and tracks each hand’s path and speed in shoulder widths per second, plus how straight the arm is and how much the torso turns, so a game can judge how much body went into a swing. Games that don’t import it pay nothing for it.
- Rock Paper Shoot needs finger shapes, which a body model can’t see. It runs its own copy of MediaPipe’s Hand Landmarker (21 points per hand) on its own page only, and never touches the shared pose stack. Its fallback is number keys or on-screen buttons.
How do you fit ML inference and a 3D renderer on one laptop?
The performance problem in a motion game is that the two expensive things compete. Pose inference wants the GPU and the main thread, and so does the three.js renderer chasing 60 fps. CamPlay’s budget looks like this:
- Inference runs at about 22 Hz, not every frame. Detection is limited to one run per 45 ms. Movement signals are smoothed over several frames anyway, so 60 Hz inference would burn battery for no gameplay difference.
- The model is the lite variant of Google’s MediaPipe Pose Landmarker, on the GPU delegate, at 640×480 camera resolution. The full and heavy variants and bigger frames improve landmark stability and hurt frame rate. This corner of the trade-off is where the games stay playable on ordinary laptops.
- The renderer keeps its own discipline: cheap materials, a stylized look and a device-pixel-ratio cap of 1.5. A stylized look that holds 60 fps beats a realistic one that stutters, because in a motion game dropped frames are dropped inputs.
- On content pages, ads load lazily. Game routes have no ads and no ad scripts at all, partly as a product decision and partly because a third-party script competing with inference for the main thread is exactly the jank you cannot afford.
How does multiplayer work without a game server?
An eight-player race sounds like it needs a server simulating the world. It doesn’t. It needs everyone to experience the same world, which is a much cheaper problem. When a room starts, all players receive a shared random seed and a synchronized start time. Every machine then generates the identical course (same obstacles, same gates, same trees) from that seed, and simulates only its own player.
flowchart TD
SEED["Shared seed + synced start time"] --> A["Player A machine<br/>generates the identical course"]
SEED --> B["Player B machine<br/>generates the identical course"]
SEED --> C["… up to 8 players"]
A <-- "progress, a few times/s" --> RT[("Realtime<br/>Database")]
B <-- "progress, a few times/s" --> RT
C <-- "progress, a few times/s" --> RTWhat crosses the network is small. Each player publishes a progress update a few times per second (position, alive or out), and everyone renders everyone else as ghosts from those updates. Ghosts are only visual, and your collisions are yours alone, so latency cannot kill you. It can only make a ghost look slightly behind. In Red Light, Green Light, even the lights are a deterministic function of the seed and the shared clock, so every player’s red light lands at the same instant without a light-switch message ever being sent.
Two kinds of room sit on this base. Race rooms (Rune Run, Red Light, Ski Slalom) rank by first to the finish, then distance. Party rooms (Simon Says, Dino Dodge and the three slap games) put everyone through the same seeded command or obstacle stream at once, mostly as last-one-standing elimination. Each client judges itself and reports its own result, and the host settles the outcome. If the host disconnects, the host seat moves automatically to the player who has been in the room longest, so a game never dies with its creator. Both kinds hold up to eight players. Rock Paper Shoot uses the same room system with two seats for a 1v1 duel. Fairness in every room relies on the rule the input engine provides: a player the camera loses for more than 1.2 seconds is out.
The game and platform bridge: custom events
There is a second boundary, less obvious than input and game. The games are mostly framework-free JavaScript (a canvas, a loop, module-level state), while the platform around them (auth, rooms, leaderboards) is React. They talk through namespaced browser CustomEvents: the game dispatches “run finished” with the result, and the platform listens and saves it. Room setup, rosters, countdowns and standings flow the other way as events into the game. Neither side imports the other, which keeps game code portable and lets one set of platform components wrap nine differently built games.
What I would tell someone building one
- Put a hard wall between perception and simulation on day one. Every motion-game codebase I have seen go bad went bad at that seam.
- Design detection for the room your players actually have, not an idealized studio. CamPlay’s upper-body-only constraint (playable a meter from the screen) shaped its whole signal design, described in the detection article.
- Treat feel as a budget line, not a polish task. Cap the inference rate, cap render cost, and treat any third-party script on a game route as a frame-rate bug.
- For multiplayer, generating the world from a seed turns a hard distributed-systems problem into an easy broadcast problem. Sync the inputs to the world, not the world itself.