# crashCams
**The six cameras of a crash-test lab, a director that cuts between them for slow-motion replays, a "REC" burn-in, and physical camera props (a high-speed camera on a tripod, an overhead boom, a glass-floor pit) — for any crash, impact or vehicle test scene.**
Version **1.0.0** (`Cams.VERSION`). Part of *Crumple Zone: Crash Test Dummy Simulator*; usable on its own.
Every camera is a camera behaviour (a component `Camera.behavior` names), so switching the shot is one write on the camera you already have. Each one reads its subjects where they are **drawn** this frame (`Physics.drawnPose`), so a car the simulation moves between physics ticks holds still on screen: measured in play, the chase camera's car moves at most 0.05° of frame jerk per frame and the onboard dummy camera's head 0.002°.
Cameras are per player: each peer drives its own view, and nothing in this package syncs (the props are scenery; see `synced` below).
## Install
Install the `crashCams` package into your world. Require it by its bare name:
```lua
local Cams = require("crashCams.cams")
```
Optional: the **Juicebox juiceKit** package. With it installed, `Cams.impactShake()` kicks the camera on screen; without it the call returns `false` and does nothing else.
## First example
A component that puts a camera on a car, frames it with the high-speed side camera, and replays from two angles once the car stops. Put it on any entity, set its three fields (or let it fall back to entities named `car`, `head`, `DemoCamera`), and press Play.
```lua
local Cams = require("crashCams.cams")
declare {}
public = {
car = Field.entityRef(nil, NoSync, "The car's chassis."),
head = Field.entityRef(nil, NoSync, "The dummy's head (optional)."),
camera = Field.entityRef(nil, NoSync, "An entity carrying a Camera."),
}
local director: any = nil
function start()
local cam = public.camera or entity.find("DemoCamera")
local car = public.car or entity.find("car")
if cam == nil or car == nil then return end
local targets = { car = car, head = public.head or entity.find("head"), impact = { 0, 0.5, 0 } }
-- One live shot...
Cams.use(cam, "highSpeedSide", targets)
Cams.overlay(cam, true, { label = "CAM 1 · HIGH-SPEED SIDE" })
-- ...then a replay that cuts between two angles every 1.6 s of wall clock.
director = Cams.director(cam, {
shots = { "highSpeedSide", "barrierFace", "dummyView" },
cut = 1.6,
targets = targets,
overlay = true,
})
task.delay(4, function() director:start() end)
end
function onDestroy()
if director ~= nil then director:stop() end
end
```
The full example is the scene `examples/camsDemo.scene`: a lane, a concrete block, a box car with a sprung head towed into the block at 50 km/h (velocity held with `Physics.onBeforeStep` + `Physics.setVelocity` until 1 m before contact, then released), the lab's props, and a director cycling all six cameras, one run per camera, with slow motion around the impact. Load it and press Play. Its layout is `examples/camsDemoLayout.module` (`Layout.build(origin)`), so the same lane can be built anywhere in a live world for testing.
## Camera catalog
| id | name | what it shows |
|---|---|---|
| `highSpeedSide` | High-speed side | Fixed low camera square to the track at the impact zone on a long lens (20°); its aim pans a little to hold the arriving car, then settles on the crush zone. |
| `overhead` | Overhead | Straight down from 9.5 m above the impact zone on a 54° lens, under a lab's light rig (at 13 m it saw the back of the lab's rig light panel); the crash as a plan (line, yaw off the barrier, shortening). Slides a little with the car. |
| `barrierFace` | Barrier face | Low at the side of the obstacle face, looking back up the track at the oncoming car on a wide lens. |
| `dummyView` | Dummy view | Onboard, bolted to the passenger side of the dash, watching the dummy's head whip into the belt or the wheel. Rides the car rigidly; aims at the drawn head in the car's frame. |
| `chase` | Chase | Low behind the car on its line; matches its speed with a short reaction lag, so it punches in when the car stops dead, then settles back. |
| `underCar` | Under car | From a glass-floor pit under the impact zone, looking up at the underside as the front folds. Needs a see-through floor above it (see Known limits). |
Every behaviour takes the same targets as fields — `car` (empty falls back to `Camera.follow`), `head`, `impact` — plus its own tuning fields (distance, height, lens `fov`, smoothing). Read them with `entityOps.component("CrashCamChase")`, or pass them as `fields` to `Cams.use`.
## API (`require("crashCams.cams")`)
Targets are `{ car = chassisId, head = dummyHeadId, impact = {x,y,z} }`: ids or entity proxies, and `impact` is the obstacle face point (`{x,y,z}` or `{x=,y=,z=}`). The track runs along world **-Z** (car starts at +Z, obstacle face points +Z), up is +Y; the ground is taken as `impact.y - 0.5`.
- `Cams.kinds() -> { { id, name, description } }` — the six cameras in catalog order.
- `Cams.use(cameraEntity, kind, targets?, fields?) -> component` — switch the Camera's behaviour to `kind` and set its targets (missing keys keep what the camera had) and any tuning `fields` (`{ side = -1 }`, `{ fov = 30 }`). Also sets `Camera.follow` to the car. `cameraEntity` is a proxy, id or name.
- `Cams.setTargets(cameraEntity, targets)` — change what the current shot frames.
- `Cams.current(cameraEntity) -> kind?`
- `Cams.component(kind) -> assetRef` — the behaviour component for `Camera.behavior`.
- `Cams.director(cameraEntity, opts) -> director` with `opts = { shots, cut = 1.6, targets, loop = true, clock = "real" | "sim", overlay = false, onCut = function(kind, index) }`. A shot is a kind id or `{ kind, hold?, targets?, fields?, label? }`. `clock = "real"` (default) cuts on the wall clock, so a slow-motion replay still cuts at a watchable pace; `"sim"` cuts on the simulation clock.
- `director:start(index?)`, `director:stop()`, `director:next()`, `director:current() -> (kind?, index)`, `director:cutTo(index)`, `director:setTargets(targets)`.
- The cutting task belongs to whoever called `start` (a component's hook: it ends with that component).
- `Cams.overlay(cameraEntity, on?, opts?) -> component` — the recording burn-in: blinking REC dot, label, `fps`/shutter, timecode and a 1000 fps frame counter, corner marks. The counter runs on the simulation clock, so slow motion counts slowly like real high-speed footage. `opts = { label, fps = 1000, shutter = 2000, showFrame = true, tint }`; the component has `:resetClock()` and `:elapsed()`. Local UI on this peer.
- `Cams.impactShake(intensity?) -> boolean` — a heavy local camera shake through juiceKit (`shakeHeavy`, scope local), when it is installed.
- `Cams.dressing(position, kind?, opts?) -> rootEntity` — spawn a camera prop: `"tripod"` (default; a high-speed camera with lens, focus ring, coated front element, heat-sink fins, side monitor, red tally lamp, handle, pan bar, three splayed legs with locks, spreaders and rubber feet, and a cable), `"boom"` (a counterweighted overhead boom with a camera hung lens-down and a flood lamp), `"pit"` (a concrete glass-floor pit sunk below `position`, yellow steel frame, a glass pane with a collider, two floods with point lights and its own camera on a stand). `opts = { aimAt = {x,y,z}, yaw, height, reach, width, length, depth, glass = true, collider = false, synced = false, name, parent }`. Built from the builtin primitives and this package's materials; call it from a scene build (baked, every peer gets it) or from code every peer runs; `synced = true` spawns with `entity.spawnSynced` from one peer instead.
- `Cams.dressingKinds() -> { "tripod", "boom", "pit" }`
## How replays use the director
The engine has no record and playback of the simulation (**#20544**). The replay is recorded by the test rig (the `crashRig` part records poses and replays them locally); the cameras only frame it. Point the director's `targets` at the entities the rig moves during playback (the replayed car and head), set the rig's playback time scale for slow motion, and call `director:start()`: with the default `clock = "real"` each angle holds `cut` wall-clock seconds whatever the replay speed. `onCut` tells the rig when an angle changes, so it can restart the replay for each angle (the classic "same crash, three angles").
## Contents
| asset | what it is |
|---|---|
| `cams.module` | The API above. |
| `camCore.module` | Shared internals of the behaviours: drawn-pose reads, placement, lens. |
| `CrashCamHighSpeedSide`, `CrashCamOverhead`, `CrashCamBarrierFace`, `CrashCamDummyView`, `CrashCamChase`, `CrashCamUnderCar` (components) | The six camera behaviours, tagged `cameraBehavior`. |
| `CrashCamOverlay` (component) | The REC / timecode burn-in. |
| `camBody`, `camAccent`, `lensBlack`, `lensGlass`, `tripodBlack`, `rubberBlack`, `tallyRed`, `screenGlow`, `labLamp`, `pitConcrete`, `hazardYellow`, `labFloor` (materials) | The props' and example's surfaces. |
| `examples/camsDemo.scene`, `examples/camsDemoLayout.module`, `examples/CamsDemo` (component) | The runnable example. |
## Gallery
`examples/camsGallery.scene` shows all six cameras live at the same time. The crash moment stands
still: a hatchback (crashCars meshes, no physics) with its bumper on the block, a dummy in the
driver's seat, the glass pit and the lab props. The `Gallery` entity holds one child camera per
kind, named by kind id. In play, the `examples/CamsGallery` component calls `Cams.use` on each one
and renders it into a texture. The six pictures share one screen, 3 x 2 on a wide screen and 2 x 3
on a narrow one, each titled with its camera number and name. In edit mode the tiles ask you to
press play, because the behaviours place their cameras only while the clock runs. Tests:
`crashCams.testSuite` (`tools.use("tests", "run", "crashCams")`).
## Known limits
- **No simulation record/playback (#20544):** the director frames whatever moves; the replay itself is the rig's job.
- **A ragdoll is drawn at physics-tick poses (#21188):** `dummyView` follows the head where it is drawn, so on a ragdoll dummy the head can step at the 60 Hz tick even though the camera itself is smooth. `dummyView`'s `aimTime` (35 ms) softens it.
- **`underCar` needs a see-through floor:** leave a hole in your floor over the pit (`Cams.dressing(p, "pit")` fills it with glass and a collider), or put the floor slab over the pit on a render layer and name it in the behaviour's `hideLayers`.
- **The time scale is one number for the whole engine:** the example only slows time when nobody else holds a scale, and only puts back a scale it set itself.
- **The overlay draws in the viewport camera's picture:** show it only on the camera that is on screen.
- The checker reports `asset-unresolved` warnings for the package's own `~.` references; they resolve when running (the same harmless warning juiceKit documents, #12937).