Add a top-down camera
Goal
Pan, rotate and zoom a fixed-tilt perspective view for an RPG or strategy game.
Files you will edit
src/game.ts
Steps
Create the rig during game initialization and update it once per tick:
tsimport { createTopDownCamera, defineGame, type TopDownCamera } from 'gameable'; let rig: TopDownCamera; export default defineGame({ init() { rig = createTopDownCamera({ distance: 14, pitch: (55 * Math.PI) / 180, bounds: { min: [-50, -50], max: [50, 50] }, }); rig.focusOn({ x: 0, y: 0, z: 0 }); }, systems: [(ctx) => rig.update(ctx, ctx.dt)], });Do not also declare
player.cameraindefineGame. The host gives that player camera precedence and skips the game's frame camera. The rig writes the frame throughctx.camera.set()andctx.camera.lookAt().Use WASD or arrows to pan, Q/E to rotate, the wheel to zoom, and middle-button drag to grab the ground. Touch drag pans and pinch zooms.
edgePan: trueenables edge scrolling; it defaults to false. Pan speed scales with distance. Yaw zero places the eye on +Z; forward pan moves toward -Z. Pitch is radians below the horizon;Math.PI / 2looks straight down.Call
rig.follow(entity)to track its Transform. Pan input breaks follow;rig.follow(0)stops it explicitly.pan(dx, dz)uses camera-relative metres, with positive dz forward.zoom(factor)multiplies distance, clamped tominDistance(6) andmaxDistance(40). Saverig.stateas JSON and restore withObject.assign(rig.state, saved); keep methods outside saved actor state.For an actor-owned view, add
topDownCamera(options)to adefineActorclass that spreadsCameraActor.spec, then select it withactivateCamera(ctx, actor). Its initial focus is the actor position, and its pose is saved automatically. Only the active camera's component reads controls and writes the frame.
Verify
Run npm run dev in your game. Pan, rotate, zoom to both limits, and drag on touch. Confirm that follow breaks when panning and that focus stays within bounds.