Camera limits (view)
CameraLimits (in @ferret/view) says where a camera may go. It acts on the camera's movement: a move that would break a limit slides along the limit's boundary instead, and a long move is cut into short steps (maxStep, default 1 m) so that a camera flying at 400 m/s cannot pass through a man in one frame. The core is pure (numbers in, numbers out) and is tested without a scene; attachCameraLimits applies it to a Babylon camera before every render, whatever moved the camera (keys, mouse, a script, the VR rig).
The limits#
| type | what it keeps | default when the world moves into it |
|---|---|---|
keepOff | at least distance m off a list of shapes, or off the shapes a function returns for a point ((p, reach) => shapes, asked only for what is near) | stay |
keepIn | inside a shape, margin m from its boundary (an arena, a room, a map's edge) | stay |
floor | at least clearance m over height(x, z) (the ground, the sea at this tide, a deck) | push |
ceiling | at least clearance m under height(x, z) | push |
custom | your own: margin(p) returns how far p is inside the allowed space (g >= 0 allowed) and the way back in | stay |
Shapes: sphere (c, r), capsule (a rod from a to b with radius r: a man, a hull, a mole), box (centre, half sizes, yaw about y: a lorry, a building), cylinder (upright: a gun pit, a tower).
Intrusion. A limit says what happens when the world has moved into it while the camera stood (a ship steams up to it, the tide rises): stay leaves the camera where it is and lets it move only away or sideways, never nearer (the camera may not go to a thing; a thing may come to the camera); push moves the camera out (a floor under a rising sea). jump(p) (a cut, a teleport, a scripted camera) always lands on the nearest allowed place.
Use#
import { CameraLimits, attachCameraLimits } from '@ferret/view';
const limits = new CameraLimits([
// 10 m off every ship and every man near the camera
{ type: 'keepOff', name: 'objects', distance: 10, shapes: (p, reach) => nearShapes(p, reach) },
// an eye's height over the ground and the sea
{ type: 'floor', name: 'ground', clearance: 1.6, height: (x, z) => Math.max(ground(x, z), seaLevel + 0.6) },
{ type: 'keepIn', name: 'map', shape: { kind: 'box', c: [0, 200, 0], half: [2000, 200, 2000] }, margin: 20 },
]);
const att = attachCameraLimits(scene, camera, limits);
att.jump([100, 5, 40]); // a cut: lands out of every limit
limits.enable('objects', false); // a photo mode without it
A game that moves its own camera (a free flying observer) can call limits.resolve(from, to) itself each frame and use what it returns; that is what examples/dunkirk does, with the keys and the VR sticks going through the same call. limits.last says which limits stopped the last move and how far of the asked way the camera got.
In the examples#
examples/dunkirk: the observer camera keeps 10 m off the destroyer, the small craft, every lorry and gun (a box on its own sand), the AA guns, the aircraft, the mole and every man (a rod 1.8 m tall), and stays 1.6 m over the sand, the sea at the tide and the mole's deck. tests/e2e/dunkirk-keepoff.spec.ts flies the camera at each kind of thing with the keys at 400 m/s and measures the gap after every frame.
What usually goes wrong#
- A shape at the wrong height (a lorry on a dune, a body measured at y 0): the camera passes over the shape and into the thing. Put the shape where the thing is.
maxSteplarger than the thinnest thing plus its distance: a fast camera steps across it.- A dense crowd with
keepOff: the camera stops at the edge of it (it may not come within the distance of any man); that is the rule, not a bug. - Moving the camera after the limits ran (a game's own code after
resolve, or VR sticks writing the position directly): the limits never see that move.