FERRETGame engine over Babylon.js
Ferret

Camera limits

Skill ferret-camera-limits · download all skills · SKILL.md

When to use#

A free or orbiting camera must not fly into ships, people, walls, the ground or the sea, must stay in the map, or must keep a distance from things.

The model#

CameraLimits holds limits (keepOff, keepIn, floor, ceiling, custom) over shapes (sphere, capsule, box with yaw, cylinder). resolve(from, to) turns a wished move into an allowed one: sliding along a boundary, in steps of maxStep. A limit's intrusion is stay (the world may come to the camera; the camera may only move away) or push (the camera is moved out). jump(p) lands a cut or a teleport on the nearest allowed place.

Steps#

  1. List the limits; for many moving things give keepOff a function (p, reach) => shapes that returns only what is near p.
  2. Put each shape where the thing really is (its own ground height, its heading, its size).
  3. Either attachCameraLimits(scene, camera, limits), or call limits.resolve(pos, pos + move) in your own camera code; route every source of movement (keys, mouse, VR sticks, scripts) through it.
  4. Cuts and teleports: jump(p).

Check afterwards#

Fly at each kind of thing with the real keys at the highest speed and measure the gap to the thing after every frame (not every 100 ms): it must not go under the distance. Unit-test resolve on the shapes without a scene.

What usually goes wrong#

  • A shape at a fixed height while the thing stands on a dune: the camera passes over the shape into the thing.
  • Code that writes the camera position after the limits ran: that move is never checked.
  • maxStep larger than the thin things: a fast camera crosses them.
  • Measuring with a test whose camera never moved: check that it got near (the gap fell) before checking the minimum.

Files#

  • reference/camera-limits.md — Where a camera may go: keep off things by a distance, stay in a region, over a floor and under a ceiling, or by a rule of your own; moves slide along the limits, fast moves cannot jump through, and the world may come to the camera without shoving it.
  • reference/api-view.md — every export of @ferret/view with signature and doc comment
  • reference/commands.md — 3 commands of these packages with their arguments