|
| 1 | +# physics2d |
| 2 | + |
| 3 | +Box2D v3–backed 2D physics plugin for Cardinal. All simulation state lives |
| 4 | +on the C side; Cardinal drives it through three ECS components attached to |
| 5 | +the entities you want simulated. |
| 6 | + |
| 7 | +Registration: |
| 8 | + |
| 9 | +```go |
| 10 | +world := cardinal.NewWorld(cardinal.WorldOptions{TickRate: 60}) |
| 11 | +cardinal.RegisterPlugin(world, physics2d.NewPlugin(physics2d.Config{ |
| 12 | + Gravity: physics2d.Vec2{X: 0, Y: -9.8}, |
| 13 | + TickRate: 60, // match WorldOptions.TickRate |
| 14 | + SubStepCount: 4, |
| 15 | +})) |
| 16 | +world.StartGame() |
| 17 | +``` |
| 18 | + |
| 19 | +## Enrolling an entity into physics |
| 20 | + |
| 21 | +An entity participates in the simulation when it carries **all three** of |
| 22 | +these components. Missing any one → the reconciler skips the entity and no |
| 23 | +body is created on the C side. |
| 24 | + |
| 25 | +| Component | Purpose | |
| 26 | +|---|---| |
| 27 | +| [`Transform2D`](component/spatial.go) | World-space position + rotation (authoritative pose) | |
| 28 | +| [`Velocity2D`](component/spatial.go) | Linear + angular velocity | |
| 29 | +| [`PhysicsBody2D`](component/physics_body.go) | Body kind, damping, flags, and the compound collider (`Shapes`) | |
| 30 | + |
| 31 | +Use the `NewPhysicsBody2D` constructor — bare struct literals leave |
| 32 | +`Active`, `Awake`, `SleepingAllowed` at `false` and `GravityScale` at `0`, |
| 33 | +which produces an inactive, sleeping, gravity-less body. |
| 34 | + |
| 35 | +### Example: a dynamic circle |
| 36 | + |
| 37 | +```go |
| 38 | +import ( |
| 39 | + "github.com/argus-labs/world-engine/pkg/cardinal" |
| 40 | + "github.com/argus-labs/world-engine/pkg/plugin/physics2d" |
| 41 | +) |
| 42 | + |
| 43 | +func SpawnBallSystem(ctx cardinal.WorldContext) error { |
| 44 | + id, err := cardinal.Create(ctx, |
| 45 | + physics2d.Transform2D{ |
| 46 | + Position: physics2d.Vec2{X: 0, Y: 10}, |
| 47 | + Rotation: 0, |
| 48 | + }, |
| 49 | + physics2d.Velocity2D{ |
| 50 | + Linear: physics2d.Vec2{X: 0, Y: 0}, |
| 51 | + Angular: 0, |
| 52 | + }, |
| 53 | + physics2d.NewPhysicsBody2D( |
| 54 | + physics2d.BodyTypeDynamic, |
| 55 | + physics2d.ColliderShape{ |
| 56 | + ShapeType: physics2d.ShapeTypeCircle, |
| 57 | + Radius: 0.5, |
| 58 | + Density: 1.0, |
| 59 | + Friction: 0.3, |
| 60 | + Restitution: 0.2, |
| 61 | + CategoryBits: 0x0001, |
| 62 | + MaskBits: 0xFFFF, |
| 63 | + }, |
| 64 | + ), |
| 65 | + ) |
| 66 | + _ = id |
| 67 | + return err |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### Example: a static box (world geometry) |
| 72 | + |
| 73 | +```go |
| 74 | +cardinal.Create(ctx, |
| 75 | + physics2d.Transform2D{Position: physics2d.Vec2{X: 0, Y: 0}}, |
| 76 | + physics2d.Velocity2D{}, |
| 77 | + physics2d.NewPhysicsBody2D( |
| 78 | + physics2d.BodyTypeStatic, |
| 79 | + physics2d.ColliderShape{ |
| 80 | + ShapeType: physics2d.ShapeTypeBox, |
| 81 | + HalfExtents: physics2d.Vec2{X: 25, Y: 1}, |
| 82 | + Friction: 0.5, |
| 83 | + CategoryBits: 0x0002, |
| 84 | + MaskBits: 0xFFFF, |
| 85 | + }, |
| 86 | + ), |
| 87 | +) |
| 88 | +``` |
| 89 | + |
| 90 | +### Body-type cheat sheet |
| 91 | + |
| 92 | +- **`BodyTypeStatic`** — immovable world geometry. No writeback. |
| 93 | +- **`BodyTypeDynamic`** — full simulation: forces, gravity, collisions. Writeback updates `Transform2D`/`Velocity2D` each tick. |
| 94 | +- **`BodyTypeKinematic`** — velocity-driven; Box2D integrates position. Writeback on. |
| 95 | +- **`BodyTypeManual`** — gameplay owns position/velocity; Box2D is used only for contact detection. No writeback; the reconciler pushes ECS → Box2D each tick. Use for characters/enemies driven by input or AI. |
| 96 | + |
| 97 | +### Compound colliders |
| 98 | + |
| 99 | +`PhysicsBody2D.Shapes` is a slice — each entry is a child fixture with its |
| 100 | +own `LocalOffset`, `LocalRotation`, material, and filter. Shape identity is |
| 101 | +by index (slot `i` in `Shapes` ↔ fixture slot `i`), so don't reorder shapes |
| 102 | +after creation if you care about per-shape references in contact events. |
| 103 | + |
| 104 | +## Built-in queries |
| 105 | + |
| 106 | +Use these first; they cover most needs and don't require CGO: |
| 107 | + |
| 108 | +- `physics2d.Raycast(RaycastRequest) RaycastResult` |
| 109 | +- `physics2d.OverlapAABB(AABBOverlapRequest) AABBOverlapResult` |
| 110 | +- `physics2d.CircleSweep(CircleSweepRequest) CircleSweepResult` |
| 111 | + |
| 112 | +All three return zero results when no C-side world exists yet (e.g. before |
| 113 | +the first reconcile, or right after `ResetRuntime`). |
| 114 | + |
| 115 | +## Custom queries via CGO |
| 116 | + |
| 117 | +If you need a Box2D feature the plugin doesn't expose (joints, shape casts, |
| 118 | +custom query filters, sensor-only overlap, etc.), call Box2D directly from |
| 119 | +your own CGO package. The plugin exposes the raw world handle via |
| 120 | +[`physics2d.WorldID()`](plugin.go), which returns the Box2D v3 `b2WorldId` |
| 121 | +packed as a `uint32`. Reconstruct it in C with `b2LoadWorldId`. |
| 122 | + |
| 123 | +### Userdata encoding |
| 124 | + |
| 125 | +The bridge stuffs identity into Box2D userdata pointers (see [bridge.c:214-217](internal/cbridge/bridge.c#L214-L217)): |
| 126 | + |
| 127 | +- **Body userdata** = entity ID, packed as `(void*)(uintptr_t)entity_id` — unpack with `(uint32_t)(uintptr_t)b2Body_GetUserData(bodyId)`. |
| 128 | +- **Shape userdata** = shape slot index (the index into `PhysicsBody2D.Shapes`), packed the same way but as `int32_t`. |
| 129 | + |
| 130 | +So inside any Box2D callback you can recover the ECS entity with one line. |
| 131 | + |
| 132 | +### Example: custom AABB overlap that returns every hit, including sensors |
| 133 | + |
| 134 | +```go |
| 135 | +package myphysics |
| 136 | + |
| 137 | +/* |
| 138 | +#cgo CFLAGS: -I${SRCDIR}/../../vendor/world-engine/pkg/plugin/physics2d/third_party/box2d/include |
| 139 | +#include "box2d/box2d.h" |
| 140 | +#include <stdint.h> |
| 141 | +
|
| 142 | +static bool overlap_cb(b2ShapeId shapeId, void* ctx) { |
| 143 | + // Recover the ECS entity ID from body userdata. |
| 144 | + b2BodyId body = b2Shape_GetBody(shapeId); |
| 145 | + uint32_t* out = (uint32_t*)ctx; |
| 146 | + // ... append unpack_uint32(b2Body_GetUserData(body)) to your buffer ... |
| 147 | + (void)out; |
| 148 | + return true; // keep going |
| 149 | +} |
| 150 | +
|
| 151 | +static int my_overlap_all( |
| 152 | + uint32_t world_id_packed, |
| 153 | + float minX, float minY, float maxX, float maxY, |
| 154 | + uint32_t* out_entities, int32_t cap |
| 155 | +) { |
| 156 | + b2WorldId world = b2LoadWorldId(world_id_packed); |
| 157 | + if (!b2World_IsValid(world)) return 0; |
| 158 | +
|
| 159 | + b2AABB aabb = { {minX, minY}, {maxX, maxY} }; |
| 160 | + b2QueryFilter filter = b2DefaultQueryFilter(); // matches everything |
| 161 | + b2World_OverlapAABB(world, aabb, filter, overlap_cb, out_entities); |
| 162 | + // ... return fill count ... |
| 163 | + return 0; |
| 164 | +} |
| 165 | +*/ |
| 166 | +import "C" |
| 167 | + |
| 168 | +import "github.com/argus-labs/world-engine/pkg/plugin/physics2d" |
| 169 | + |
| 170 | +func OverlapAll(minX, minY, maxX, maxY float64) []uint32 { |
| 171 | + worldID := physics2d.WorldID() |
| 172 | + if worldID == 0 { |
| 173 | + return nil // no world yet: before init or after ResetRuntime |
| 174 | + } |
| 175 | + buf := make([]uint32, 256) |
| 176 | + n := C.my_overlap_all( |
| 177 | + C.uint32_t(worldID), |
| 178 | + C.float(minX), C.float(minY), C.float(maxX), C.float(maxY), |
| 179 | + (*C.uint32_t)(&buf[0]), C.int32_t(len(buf)), |
| 180 | + ) |
| 181 | + return buf[:int(n)] |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +### Rules of engagement |
| 186 | + |
| 187 | +- **Always null-check `WorldID()`** — it returns `0` before the first |
| 188 | + `PreUpdate` reconcile and after `ResetRuntime`. Treat `0` as "no world, |
| 189 | + skip the query." |
| 190 | +- **Never mutate world state from a system.** `WorldID()` gives you a raw |
| 191 | + Box2D handle; calling `b2Body_SetTransform` / `b2DestroyBody` / etc. |
| 192 | + directly will desync the bridge's entity→body map and the reconciler |
| 193 | + will fight you next tick. For mutations, go through ECS components — the |
| 194 | + reconciler pushes changes to Box2D before each step. |
| 195 | +- **Queries are fine.** Raycasts, overlaps, shape casts, sensor iteration, |
| 196 | + contact walks — read-only Box2D calls are safe to make any time after |
| 197 | + you've confirmed `WorldID() != 0`. |
| 198 | +- **Don't cache the handle across ticks.** `WorldID()` is cheap; call it |
| 199 | + at the top of each query. After `ResetRuntime` (e.g. snapshot restore) |
| 200 | + the old id is invalid. |
| 201 | + |
| 202 | +## Contact events |
| 203 | + |
| 204 | +Contacts and triggers flow through Cardinal's system-event bus. Register an |
| 205 | +emitter with `physics2d.SetStepContactEmitter` and the plugin flushes |
| 206 | +`ContactBeginEvent` / `ContactEndEvent` / `TriggerBeginEvent` / |
| 207 | +`TriggerEndEvent` each tick. The events carry both entity IDs and both |
| 208 | +shape indices, so you can look up the exact `ColliderShape` that produced |
| 209 | +the contact. |
0 commit comments