# Destruction

# Destruction

`@ferret/destruction` decides how things break. **By default the answer is physics**: a break is a comparison of the energy (or stress) of a hit with what the material holds, taken from the numbers
of the material, never a hand-tuned "health". A different behaviour (the grotesque) is a **style**: data plus failure models registered by name, set for the world, a level, a zone, an entity or one event.

## The hit and the ledger

A contact becomes a `Hit` (`makeHit`): closing speed, reduced mass `mu = m1 m2 / (m1 + m2)`, and the energy available for damage `E = 1/2 mu v^2 (1 - e^2)`, split between the two sides by their
stiffness (the softer side takes more). A resting contact does nothing. A failure model returns an `Outcome` with a **ledger**: `input = crush + fracture + kinetic + heat + sound + residual`, within
1e-9. A style that does not conserve says so (`conserve: false`), and `destruction.explain` shows it.

## Materials and failure models

| kind | model | law |
|---|---|---|
| ductile (sheet metal, plastic) | `crush.ts` | a crush zone with a force-depth curve `F(x)`; absorbed energy is `integral F dx`; the dent is permanent (a 5 x 3 x 3 lattice per panel); a car decelerates along the curve |
| brittle (glass) | `glass.ts` | Roark plate stress against the modulus of rupture; stages none, star, shards; tempered is 4 times stronger and dices at once; laminated stays whole as a star |
| fibrous (wood) | `wood.ts` | bending `sigma = M / Z`; a plank on two supports breaks at `F = 4 sigma Z / L`; grain, knots, nails that pull out |
| brittle stone, crumbly | `stone.ts` | `E = sigma^2 V / 2Y`; Voronoi cells by energy; bricks by mortar links; plaster to dust |
| rigid | hold | never fails |

Pieces come from `fracture.ts` (seeded radial, concentric, grain and Voronoi patterns; the masses of the pieces sum to the parent; the crack area times `fractureEnergy` is the energy that went into
fracture). `debris.ts` keeps them affordable: near pieces are bodies, middle ones become instances, far ones are never made; sleepers merge into clutter; small pieces fade; one sound per break.

## Parts, links and cars

`Assembly` holds parts and breakable links (`tensile x area x count`); a load is shared by the links in proportion to what each holds, a link past its force breaks, and a part with no path to the
root comes off (the mass and the centre of mass are recomputed). `vehicle.ts` is a ray-cast wheel assembly: one rigid body at the true centre of mass, a spring-damper and a friction circle per wheel;
rolling and overturning come out of the rigid dynamics, they are not authored. `autoparts.ts` makes a car from node names: it cuts `Body` into `Hood`, `Trunk`, bumpers and fenders, and gives doors,
glass, wheels, links and crush zones. The library cars `crown-victoria-cpd87` (1814 kg) and `corvette-c4-87-conv` (1500 kg) are the first destructible assets (`CAR_MASS`).

## Styles

```jsonc
{ "name": "cartoon", "energy": { "scale": 4, "conserve": false }, "crush": { "depthScale": 3, "recover": 0.8 },
  "fracture": { "piecesScale": 3, "burstScale": 5 }, "debris": { "bounce": 0.9, "life": 20, "gravityScale": 0.4 } }
```

Shipped: `physical`, `cartoon`, `hollywood`, `brittle-world`, `rigid-world`. A style multiplies numbers and may replace a model by name (`override`); it may not break determinism, the budgets or the net.
`destruction.style {scope, name}` sets it (scopes: world, level, zone, entity; an event wins over all), `destruction.define {spec}` adds one, `destruction.styles` lists them.

## Determinism and the net

Every random number comes from the seed of the break (`breakSeed(entity, step, point)`). A break travels as a record (the style name, the seed and the hit numbers); `replay(record)` on another peer gives
an equal outcome. `save()` keeps dents, damage, broken links and parts that came off.

## Using it

```ts
const service = new DestructionService({ maxPieces: 200 });
service.defineCar('car1', 'crown-victoria-cpd87', nodes);            // or defineThing(block) from a manifest block
host.register(destruction(service)); host.enable('destruction');   // contacts of bound bodies become hits
capability.bind('car1', body);
installDestructionView(world, service, { nodeOf });                 // dents, stars, shards, detached parts, sound and dust
```

Commands: `destruction.style`, `.styles`, `.define`, `.break`, `.replay`, `.explain`, `.autoparts`, `.define-thing`, `.parts`, `.inspect`, `.lod`, `.save`, `.load`. Motion-language words:
`destruction-break`, `destruction-hit`, `destruction-style`, `destruction-world-style`, `destruction-lod`, sensors `damage`, `dent`, `attached`, `stage`; events `destruction:*` for `wait-event`.

Needs a rigid-body kernel (Havok) for cars and boxes; on the light kernel only spheres break.

## Unreal build

The pure files translate to C++ (conformance case `042-destruction`); `view*.ts` are the engine's (`ue`). See `ferret-unreal-build`.
