# Physics

Rigid bodies that fall, tumble and collide, joints that link them, cloth that drapes, and soft pads that squash on contact.

`"body": {}` on a node makes it a rigid body. The world is simulated once when the scene loads, then scrubs like keyframes, so it stays deterministic.

```json title="apps/docs/examples/physics.json"
{
  "camera": { "position": [0, 3, 10], "target": [0, 1, 0], "fov": 40 },
  "lighting": { "rig": "studio_soft" },
  "environment": {
    "background": {
      "radial_gradient": { "inner": "#1b2233", "outer": "#07090f" }
    }
  },
  "physics": { "gravity": [0, -9.81, 0], "ground": 0 },
  "materials": { "floor": { "preset": "matte_clay", "color": "#2a2f3a" } },
  "nodes": {
    "floor": {
      "mesh": {
        "geometry": { "plane": { "size": [30, 30] } },
        "material": "floor"
      },
      "transform": { "rotation": [-90, 0, 0] }
    },
    "wall": {
      "transform": { "position": [0, 1, 0] },
      "mesh": {
        "geometry": { "box": { "size": [3, 2, 0.6] } },
        "material": "preset:ceramic"
      },
      "shatter": {
        "start": 0.8,
        "pieces": [6, 4, 1],
        "speed": 4,
        "from": [0, 0, 0.5]
      }
    },
    "ball": {
      "transform": { "position": [0, 1, 6] },
      "mesh": {
        "geometry": { "sphere": { "radius": 0.4 } },
        "material": "preset:chrome"
      },
      "body": { "mass": 20, "velocity": [0, 1, -10] }
    }
  },
  "timeline": { "duration": 3, "loop": "once" }
}
```

![Render of physics](/renders/physics.jpg)

## Bodies

- The collision shape comes from the mesh: spheres stay spheres, everything else is its box. Or set `shape` to a `box`, `sphere` or `capsule`.
- `type`: `dynamic` (simulated), `static` (walls and ramps), or `kinematic` (keyframed pushers).
- `mass`, `bounce` (0 to 1) and `friction`.
- `start`: until then the body follows its keyframes, then it's released with the speed it had, plus `velocity` and `spin` (degrees per second). Throw something by keyframing it, then release it.
- Put bodies at the top level or under groups that don't move.

## World

`physics: {"gravity": [0, -9.81, 0], "ground": 0}` sets gravity and an endless floor. `"ground": null` removes the floor.

## Shatter

`"shatter": {"start": 2.5, "pieces": [6, 5, 1], "speed": 4, "from": [0, 0, -0.5]}` on a box breaks it into chunk bodies at `start`, bursting away from `from`.

## Joints

`"joint": {"parent": "upper", "kind": "hinge", "limits": [0, 90]}` on a body links it to another body. The chain is simulated with the rest of the physics, so it scrubs like keyframes.

- `kind`: `hinge` (one axis), `universal` (two), `ball` (a socket), or `fixed` (welded).
- `axis` and, for `universal`, `axis2`.
- `limits` are `[min, max]` degrees. On a `ball`, `max` is the swing cone.
- `stiffness` and `target` drive a motor. `track_keyframes: true` follows the node's keyed pose and still yields on contact.
- `couple` copies another joint's angle, like a fingertip following the middle knuckle.

Both nodes need a `body`. Parent links must not form a cycle.

## Cloth

`"cloth": {"size": [2, 3], "preset": "flag", "pins": [{"edge": "top_edge", "node": "pole"}]}` is a sheet in the node's local XY plane. Rotate `[-90, 0, 0]` to lay it flat. Pins hold an edge, the corners, or a UV point, and can ride on another node.

`wind` blows on the sheet. `self_collide` stops it passing through itself.

## Soft tissue

`"soft": {"preset": "fingertip"}` is a flesh pad on the node, usually a finger or palm. It squashes against bodies and the ground, then returns. Presets are `fingertip`, `palm`, `jelly` and `rubber`.

Native rendering and export (the CLI) include physics. The browser authoring and WebGL2 bundles leave it out for now, so they stay under the size caps; a follow-up will load it as an opt-in bundle, like WebXR.

## API Reference

### Physics

World settings for every `body`.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `gravity` | `[x, y, z]` | `[0,-9.81,0]` | Acceleration in m/s² (default Earth gravity, down). |
| `ground` | `number` | `0` | Height of an endless solid floor; `null` for none. |
| `ground_bounce` | `number` | `0.2` | Bounce and friction of the floor. |
| `ground_friction` | `number` | `0.6` | Friction of the floor. |
### Body

Makes a node a rigid body. Its collision shape comes from its mesh (a sphere for spheres and icosahedra, else the box around it) unless `shape` says otherwise. Until `start` it follows its keyframes; then it is released with the speed it had (plus `velocity` and `spin`). Put bodies at the top level or under unmoving groups.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `bounce` | `number` | `0.3` | 0 = dead stop, 1 = bounces back to full height. |
| `buoyancy` | `number` | — | How it floats in a `flood`: 1 rides half under the surface (wood, a crate), 2 bobs high (a beach ball), below 0.5 sinks (stone), 0 sinks like a rock (water still drags it). |
| `friction` | `number` | `0.6` | Surface friction, 0 (ice) to 1 (rubber). |
| `mass` | `number` | `1` | Kilograms (only ratios between bodies matter). |
| `shape` | `BodyShape` | — | An explicit collision shape. Defaults to the mesh's sphere or bounding box. |
| `spin` | `[x, y, z]` | — | Extra launch spin at release, degrees/s about x, y, z. |
| `start` | `number` | — | Release time, seconds (dynamic bodies). |
| `type` | `BodyType` | — | How the body moves: simulated, or fixed in place for others to hit. |
| `velocity` | `[x, y, z]` | — | Extra launch velocity at release, m/s. |
### BodyType

How a body moves.

| Value | Description |
| --- | --- |
| `"dynamic"` | Falls, tumbles and collides. |
| `"static"` | Never moves; others collide with it (walls, ramps). |
| `"kinematic"` | Moves only by its keyframes and pushes dynamic bodies out of the way (a wrecking ball, a bulldozer blade). |
### BodyShape

An explicit collision shape, in the node's local units (before scale).

- `{"box": { … }}`: A box collider.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `size` (required) | `[x, y, z]` | — | Full size `[x, y, z]` in the node's local units. |

- `{"sphere": { … }}`: A sphere collider.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `radius` (required) | `number` | — | Radius in the node's local units. |

- `{"capsule": { … }}`: Capsule along the local Y axis: cylinder of `length` capped by hemispheres of `radius`.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `length` (required) | `number` | — | Length of the straight middle part, along the local Y axis, in the node's local units. |
| `radius` (required) | `number` | — | Radius of the cylindrical body and the hemispherical caps, in the node's local units. |
### Shatter

Break a box into a grid of chunks that burst apart at `start` and fall as rigid bodies (a wall smashed, a crate blown open). Until then the chunks sit together and look like the whole box. Needs a `box` mesh.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `from` | `[x, y, z]` | — | Where the blow lands, in the node's local space (default: its centre, so it bursts evenly). Chunks fly away from it. |
| `pieces` | `integer[]` | `[4,3,2]` | Chunks along x, y and z (1 to 12 each). |
| `speed` | `number` | `4` | Burst speed in m/s, away from `from`. |
| `spin` | `number` | `240` | Random tumble, degrees/s. |
| `start` (required) | `number` | — | Seconds into the timeline when it breaks. |
### Joint

Articulated rigid-body joint constraint (XPBD) connecting this node's `body` to `parent`'s `body`. Supports angular limits, compliant PD motors, half-physics keyframe tracking, and anatomical tendon coupling.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `anchor` | `[x, y, z]` | — | Local-space anchor point on this node. |
| `axis` | `[x, y, z]` | — | Primary rotation axis in this node's local space (default `[1, 0, 0]`). |
| `axis2` | `[x, y, z]` | — | Secondary rotation axis for `universal` joints (default `[0, 0, 1]`). |
| `collide_connected` | `boolean` | — | Allow collision between this body and `parent` (default `false`). |
| `couple` | `JointCouple` | — | Anatomical tendon coupling to another joint's flexion angle. |
| `damping` | `number` | `0.15` | Angular velocity damping across the joint (`0..=10`, default `0.15`). |
| `kind` | `JointKind` | — | Constraint topology: `hinge` (1-DOF), `universal` (2-DOF), `ball` (3-DOF), or `fixed`. |
| `limits` | `[x, y]` | — | Primary angular limits `[min_deg, max_deg]` (for `ball`, `max_deg` is the swing cone). |
| `limits2` | `[x, y]` | — | Secondary angular limits `[min_deg, max_deg]` for `universal` joints. |
| `parent` (required) | `string` | — | Node id of the parent rigid body this joint attaches to. |
| `parent_anchor` | `[x, y, z]` | — | Local-space anchor point on `parent`. When omitted, inferred from the initial world poses at `t = 0` so the joint starts aligned. |
| `stiffness` | `number` | — | Compliant angular PD motor stiffness (0 = passive/free, higher = stiffer tracking). |
| `target` | `number` | — | Fixed motor target angle in degrees along `axis`. |
| `track_keyframes` | `boolean` | — | Half-physics mode: when `true`, the PD motor tracks this node's keyframed orientation relative to `parent` while still deflecting on contact. |
### JointKind

Type of articulated constraint connecting two rigid bodies.

| Value | Description |
| --- | --- |
| `"hinge"` | 1-DOF revolute hinge about `axis` (e.g. finger PIP/DIP knuckles). |
| `"universal"` | 2-DOF universal joint about `axis` and `axis2` (e.g. finger MCP knuckle). |
| `"ball"` | 3-DOF ball-and-socket joint with optional swing cone `limits` (e.g. thumb CMC). |
| `"fixed"` | 0-DOF rigid attachment (welds two bodies together). |
### JointCouple

Tendon coupling: drives this joint's target angle proportionally to another joint's current flexion angle (e.g. human finger DIP ≈ 0.67 × PIP).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `from` (required) | `string` | — | Node id of the driving joint. |
| `ratio` | `number` | `0.67` | Multiplier applied to `from`'s primary angle (default `0.67`). |
### Cloth

Simulated cloth sheet lying in the node's local XY plane (like `plane`, rotate `[-90, 0, 0]` to lay flat for a drape or tablecloth). Simulated once on load with rigid bodies using deterministic substepped XPBD, so every frame scrubs like keyframes.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `bend` | `number` | — | Bending compliance (`0` = stiff cardboard, higher = softer drape; overrides preset). |
| `damping` | `number` | — | Velocity damping fraction (`0..=1`; overrides preset). |
| `drag` | `number` | — | Aerodynamic drag coefficient (overrides preset). |
| `mass` | `number` | — | Total cloth mass in kg (overrides preset). |
| `material` | `string` | — | Material reference for rendering the cloth surface. |
| `pins` | `ClothPin[]` | — | Pinned edges, corners, UV points, or attachments to animated nodes. |
| `preset` | `ClothPreset` | — | Material preset controlling default stiffness, mass, damping and drag. |
| `segments` | `integer[]` | `[20,20]` | Grid subdivisions `[cols, rows]` (`2..=64` each). More is finer and slower. |
| `self_collide` | `boolean` | — | Enable deterministic spatial-hash self-collision (off by default). |
| `size` | `[x, y]` | `[2,2]` | Width (local x) and height (local y) of the sheet, world units. |
| `start` | `number` | — | Release time in seconds (stays in rest pose until `start`). |
| `stretch` | `number` | — | Structural (warp) stretch compliance (`0` = inextensible; overrides preset). |
| `substeps` | `integer` | `6` | XPBD substeps per physics tick (`1..=20`, default `6`). |
| `thickness` | `number` | `0.02` | Collision thickness / offset in world units. |
| `weft_stretch` | `number` | — | Horizontal (weft) stretch compliance for anisotropic weaves (defaults to `stretch`). |
| `wind` | `ClothWind` | — | Optional wind blowing on the cloth. |
### ClothPreset

Material preset for a [`Cloth`] node: tuned defaults for stretch, bending, mass, damping and wind drag.

| Value | Description |
| --- | --- |
| `"cotton"` |  |
| `"silk"` |  |
| `"denim"` |  |
| `"flag"` |  |
| `"curtain"` |  |
### ClothPin

Pins vertices of a [`Cloth`] sheet in place, or attaches them to another node so the cloth moves with an animated/kinematic carrier (e.g. a flagpole, curtain rod, or character shoulder).

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `edge` | `ClothPinEdge` | — | Preset edge or corner selection. |
| `node` | `string` | — | Optional node id whose world transform carries the pinned vertices. |
| `uv` | `[x, y]` | — | Pin the vertex nearest to normalized `[u, v]` (`0..=1` across width and height, where `v = 0` is the top edge and `v = 1` is the bottom). |
### ClothPinEdge

Edge or corner shorthand for pinning vertices of a [`Cloth`] sheet.

| Value | Description |
| --- | --- |
| `"top_edge"` |  |
| `"bottom_edge"` |  |
| `"left_edge"` |  |
| `"right_edge"` |  |
| `"top_corners"` |  |
| `"all_corners"` |  |
### ClothWind

Wind field acting on a [`Cloth`] sheet via aerodynamic drag and lift.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `direction` | `[x, y, z]` | `[1,0,0]` | World-space wind direction (normalized by the solver). |
| `strength` | `number` | `0` | Wind speed in m/s. |
| `turbulence` | `number` | `0.3` | Deterministic gust/turbulence amplitude (`0` = steady wind). |
### SoftTissue

PhysHand-style layered skin–flesh–bone XPBD soft tissue pad attached to a node (typically a phalanx rigid body or kinematic bone). Deforms smoothly on contact with other rigid bodies and the ground while preserving local pad volume, baked deterministically for zero-alloc scrubbing.

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `bulge` | `number` | — | Lateral volume-preservation bulge factor (`0..=1`). |
| `compliance` | `number` | — | XPBD bone-anchor compliance override (lower = stiffer). |
| `damping` | `number` | — | Velocity damping (`0..=1`). |
| `dorsal_stiff` | `boolean` | — | Keep the dorsal (+Z) hemisphere stiff like a fingernail backing. |
| `material` | `string` | — | Optional material override (defaults to the node's mesh material). |
| `preset` | `SoftPreset` | — | Tuned material preset (`fingertip`, `palm`, `jelly`, `rubber`). |
| `segments` | `integer` | `14` | Surface subdivision rings (`8..=32`, default `14`). |
| `substeps` | `integer` | `4` | XPBD substeps per physics tick (`1..=16`, default `4`). |
| `thickness` | `number` | — | Maximum soft flesh compression depth in world units. |
### SoftPreset

Material preset for a [`SoftTissue`] pad (PhysHand-style skin-flesh-bone XPBD).

| Value | Description |
| --- | --- |
| `"fingertip"` | Compliant palmar fingertip pad with stiff dorsal/nail backing. |
| `"palm"` | Thicker, firmer palm cushion. |
| `"jelly"` | Very soft, bouncy isotropic gel. |
| `"rubber"` | Firm elastomer gripper pad. |
