Spatial Physics Queries
Relevant source files
The following files were used as context for generating this wiki page:
- pkg/plugin/physics2d/README.md
- pkg/plugin/physics2d/test/query_test.go
- pkg/plugin/physics2d/test/cardinal_integration_test.go
- pkg/plugin/physics2d/internal/cbridge/bridge.h
- pkg/plugin/physics2d/third_party/box2d/src/physics_world.c
- pkg/plugin/physics2d/proto/pkg/plugin/physics2d/component/component.proto
Spatial physics queries allow Cardinal systems to inspect and interact with the 2D physics world. The physics2d plugin provides a high-level Go API to perform common operations such as raycasting, AABB overlaps, and circle sweeps against the simulation state managed by Box2D. These queries return data about ECS entities, enabling gameplay logic to respond to physical positioning and collisions.
Sources: pkg/plugin/physics2d/README.md:89-94, pkg/plugin/physics2d/test/query_test.go:1-10
Query Architecture
The plugin bridges Go-side ECS data with a C-side Box2D world. Queries are executed through CGO calls that traverse the Box2D dynamic tree. The results map Box2D fixture data back to Cardinal EntityID values by unpacking metadata stored in body and shape user data.
The system requires a valid b2WorldId to function. This ID is packed as a uint32 and can be retrieved using physics2d.WorldID(). If the world does not exist (e.g., before initialization or after ResetRuntime), queries return zero or empty results.
Sources: pkg/plugin/physics2d/README.md:96-105, pkg/plugin/physics2d/internal/cbridge/bridge.h:173-180, pkg/plugin/physics2d/third_party/box2d/src/physics_world.c:1342-1355
Primary Query Types
The plugin exposes three built-in query functions that handle the most common spatial requirements.
Raycast
Raycast determines if a line segment intersects any colliders in the world. It returns the closest hit, including the entity ID, shape index, hit point, and surface normal.
| Parameter | Type | Description |
|---|---|---|
Origin | Vec2 | Starting point of the ray. |
End | Vec2 | Ending point of the ray. |
Filter | *Filter | Optional category and mask bits to filter hits. |
Sources: pkg/plugin/physics2d/test/query_test.go:13-30, pkg/plugin/physics2d/internal/cbridge/bridge.h:182-189
Overlap AABB
OverlapAABB finds all shapes whose axis-aligned bounding boxes (AABB) overlap a specified rectangular region.
Circle Sweep
CircleSweep casts a circle of a specific radius along a line segment to find the first collision point. This is useful for character movement or projectile logic where volume matters.
Sources: pkg/plugin/physics2d/test/query_test.go:248-262, pkg/plugin/physics2d/internal/cbridge/bridge.h:196-200
Filtering and Configuration
Queries use a Filter structure to determine which objects should be considered for hits. This allows systems to ignore specific layers (categories) or include/exclude sensors.
Filter Data Model
The filter logic matches the underlying Box2D filtering system.
| Field | Type | Description |
|---|---|---|
CategoryBits | uint64 | The collision categories this query represents. |
MaskBits | uint64 | The categories the query is allowed to hit. |
IncludeSensors | bool | If true, the query includes shapes marked as IsSensor. |
Sources: pkg/plugin/physics2d/proto/pkg/plugin/physics2d/component/component.proto:30-32, pkg/plugin/physics2d/test/cardinal_integration_test.go:417-425
Compound Collider Queries
Entities may possess multiple ColliderShape entries within their PhysicsBody2D. When a query hits a compound body, it returns the ShapeIndex, which corresponds to the index of the shape in the entity's Shapes slice.
- Default Behavior: Queries typically exclude sensors unless
IncludeSensorsis set totrue. - Identity: Shape identity is determined by its slot in the
Shapesarray. Developers should not reorder these shapes if they rely on index-based logic in systems.
Sources: pkg/plugin/physics2d/README.md:81-87, pkg/plugin/physics2d/test/query_test.go:348-360
Best Practices and Constraints
- World Null-Checks: Always verify
WorldID() != 0before attempting custom CGO queries to avoid panics or undefined behavior. - Read-Only Access: Systems should only perform read-only queries during the update cycle. Direct mutation of Box2D state (e.g., calling
b2Body_SetTransform) causes desynchronization with the Cardinal ECS reconciler. - Handle Lifetime: Do not cache the world handle across ticks. The world ID is invalidated whenever
ResetRuntimeis called (such as during a snapshot restore). - Coordinate Handling: The
OverlapAABBimplementation automatically corrects swappedMinandMaxcoordinates (e.g., ifMin.X > Max.X).
Sources: pkg/plugin/physics2d/README.md:164-178, pkg/plugin/physics2d/test/query_test.go:210-220