# Physics (Jolt Physics integration, collision, BVH broadphase) > **Scope:** `src/Engine/Physics/` — the engine's rigid-body physics. The heavy lifting is done by > **Jolt Physics** (the external library in `external/JoltPhysics`, linked in `src/Engine/CMakeLists.txt`); > Seele provides a **thin ECS/Scene wrapper** on top: `PhysicsSystem`, `CollisionSystem`, and a > `BVH` broadphase. You almost never touch Jolt directly — you add components and let the systems > do the rest. **Read `AGENTS.md` first** — it has a dedicated "Physics (Jolt)" section with the ground rules (this doc complements it with navigation). Jolt is a **third-party black box**: you configure it, you don't modify it, and you don't assume its internals. ## Key types & where they live | Type | Defined in | |------|------------| | `PhysicsSystem` | `src/Engine/Physics/PhysicsSystem.h` (impl `PhysicsSystem.cpp`, ~18 KB — the main integration) | | `PhysicsSystem::Body` | `src/Engine/Physics/PhysicsSystem.h:18` — Seele's mirror of a Jolt rigid body | | `PhysicsSystem::Contact` | `src/Engine/Physics/PhysicsSystem.h` — a collision contact | | `CollisionSystem` | `src/Engine/Physics/CollisionSystem.h` (impl `CollisionSystem.cpp`) | | `CollisionSystem::Witness` | `src/Engine/Physics/CollisionSystem.h:21` — witness-point broadphase state | | `BVH` | `src/Engine/Physics/BVH.h` (impl `BVH.cpp`) — the broadphase spatial structure | | `Component::RigidBody`, `Component::Collider`, `Component::ShapeBase`, `Box/Mesh/Sphere/Plane` | `src/Engine/Component/` (see [scene-ecs-components-actors.md](scene-ecs-components-actors.md)) | `PhysicsSystem` is **embedded in `Scene`** (it is a member, not a `System` in the `SystemGraph`) — it is *not* a node you add to the system graph. It is driven by the scene's update tick. The `CollisionSystem` likewise operates on the `entt::registry`. This is a key difference from the "everything is a `System`" pattern — physics is *scene-owned*, not *graph-owned*. ## The data flow (how a body actually moves) ``` Component::RigidBody + Component::Collider + Component::Transform (on an Actor's entity) │ (Scene collects these into PhysicsSystem bodies) ▼ PhysicsSystem::update(deltaTime) ├─ readRigidBodies() # gather current Component state → Array ├─ serializeArray(bodies, x) # pack Body[] → float[] (the Jolt API boundary) ├─ [Jolt step] # Jolt integrates the float[] (forces, velocities, collisions) ├─ deserializeArray(...) # unpack Jolt's result back into Body[] └─ write results back to Component::Transform / RigidBody (positions, velocities) ▼ CollisionSystem::detectCollisions() # broadphase (BVH) + narrowphase (witness points) └─ → Array / Contacts (consumed by gameplay / events) ``` The **`serializeArray`/`deserializeArray` pair is the Jolt API boundary** (`PhysicsSystem.h:83-87`). Jolt's public API works on `float*` state vectors, not C++ objects. Seele's `Body` struct is a *C++-side mirror* of what Jolt stores; every step it's serialized across into Jolt's float world and back. **If you add a new physical property, you must add it to all four**: the `Body` field, `serializeRB`, `deserializeRB`, and the component that feeds it — miss one and the value silently doesn't round-trip (Jolt integrates a stale copy). ## The `Body` struct (what Seele tracks per body) `PhysicsSystem::Body` (`PhysicsSystem.h:18`) holds the full rigid-body state, with Jolt-aligned naming (so the serialization is a straight field map): - `id` (the `entt::entity`), `centerOfMass`, `iBody`/`iBodyInv` (body-space inverse inertia, from `collider.physicsMesh.bodyInertia`), `scale` - state: `x` (position), `q` (orientation quat), `R` (orientation matrix), `v` (linear vel), `omega` (angular vel), `P` (linear momentum), `L` (angular momentum), `iInv` (world-space inverse inertia, recomputed in the ctor as `R * iBodyInv * Rᵀ`) - `force` / `torque` (inputs from `Component::RigidBody`) - `matrix` (the final world transform, `updateMatrix()` composes scale→rotation→translation) - `ptVelocity(p)` — a rigid-body point velocity helper (`v + ω × (p - x + com)`) There are **two ctors**: one that takes a `RigidBody` (dynamic, momentum-carrying) and one that doesn't (kinematic/static, zero momentum). Both normalize the quaternion and recompute `iInv`. ## CollisionSystem (broadphase + narrowphase) `CollisionSystem` (`CollisionSystem.h`) produces `Array` each tick: - **Broadphase** via `BVH` — a bounding-volume hierarchy over collider AABBs. `CollisionSystem` builds/maintains it and queries candidate pairs (broadphase = "which pairs *might* collide"). - **Narrowphase** via **witness points** (`CollisionSystem::Witness`, `createWitness`/ `updateWitness`/`witnessValid`): a classic GJK-style witness-point test that decides, for a *candidate* pair, whether the shapes actually intersect. This is the "do these two shapes actually collide" step. - `checkCollision(pair)` / `detectCollisions(&out)` are the entry points. **Gotcha:** the broadphase is a *filter*, not a guarantee. A pair passing the broadphase still needs the narrowphase witness test. Don't treat "broadphase said they might collide" as "they collided." ## The BVH `BVH` (`Physics/BVH.h` + `BVH.cpp`, ~10 KB) is a generic bounding-volume hierarchy. It's the spatial index the `CollisionSystem` uses for the broadphase. It's a reusable structure — if you need broadphase for something else (culling queries, picking), this is the building block. (For **rendering** culling, see the mesh culling in [meshes-and-meshlets.md](render-graph-and-passes.md) — that's a *different* BVH/`MeshCullingSystem` in `src/Engine/Rendering/`, do not confuse the two.) ## Gotchas - **Physics is scene-owned, not graph-owned.** `PhysicsSystem` is a `Scene` member; you do *not* add it to a `SystemGraph`. Don't look for it in the graph. - **Adding a property = 4 places** (Body field + serialize + deserialize + component). Silent failure mode: value doesn't round-trip through Jolt. - **Jolt is a black box.** Configure via the components; don't reach into `external/JoltPhysics` to "fix" behavior. (The one legitimate exception is linking/version, in `src/Engine/CMakeLists.txt`.) - **Don't confuse the two BVHs.** `Physics/BVH` = collision broadphase. `Rendering` culling = a separate structure. Similar names, different subsystems, different files. - **`iInv` is recomputed in the `Body` ctor** from `iBody` and orientation. If you mutate `q`/`R` after construction, you must recompute `iInv` or the dynamics are wrong. - **Contacts are a transient view.** `Contact`/`Collision` are filled per tick; don't cache them across frames or they go stale. ## Where to look | What you're doing | Start here | |---|---| | Add a rigid body to a scene | `Component::RigidBody` + `Component::Collider` on an Actor (see scene-ecs doc) | | Add a new collider shape | `src/Engine/Component/` shapes (`ShapeBase` + `Box`/`Mesh`/`Sphere`/`Plane`) + `physicsMesh` (the Jolt mesh) | | Add a physical property that round-trips through Jolt | `PhysicsSystem::Body` + `serializeRB`/`deserializeRB` + the component | | Understand a "body doesn't move / wrong velocity" bug | the serialize→Jolt→deserialize path in `PhysicsSystem.cpp` | | Add a new collision response / query | `CollisionSystem.h` (witness) + `BVH.h` (broadphase) | | Jolt linking / version | `src/Engine/CMakeLists.txt` (`JOLT_PHYSICS_LIB`) | | Ground rules (read first) | `AGENTS.md` § "Physics (Jolt)" |