GridCollisionDetectionSystem Class
Collision detection system using uniform spatial partitioning for Broadphase and AABB overlaps in the Narrowphase. More...
Declaration
Base class
| class | System |
|
Abstract base class for game systems. More... | |
Public Constructors Index
| GridCollisionDetectionSystem (const helios::math::aabbf &bounds, const float cellSize) | |
|
Constructs a GridCollisionDetectionSystem with specified bounds and cell size. More... | |
Public Member Functions Index
| void | update (helios::engine::runtime::world::UpdateContext &updateContext) noexcept override |
|
Performs collision detection for all active entities. More... | |
| helios::math::aabbi | worldBoundsToGridBounds (const helios::math::aabbf &aabbf) const noexcept |
|
Converts world-space AABB bounds to grid cell indices. More... | |
| void | updateCollisionCandidate (helios::engine::ecs::GameObject *go, const helios::math::aabbi &bounds, AabbColliderComponent *aabbColliderComponent, CollisionComponent *collisionComponent, CollisionStateComponent *collisionStateComponent) |
|
Inserts a collision candidate into all grid cells it overlaps. More... | |
| void | solveCell (GridCell &cell, helios::engine::runtime::world::UpdateContext &updateContext) |
|
Performs narrow-phase collision detection for all candidates in a cell. More... | |
| GridCell & | cell (const unsigned int x, const unsigned int y, const unsigned int z) noexcept |
|
Accesses a grid cell by its 3D coordinates. More... | |
| constexpr size_t | cellIndex (const unsigned int x, const unsigned int y, const unsigned int z) const noexcept |
|
Computes the 1D index of a cell in a 3D grid based on its x, y, and z coordinates. More... | |
| unsigned int | cellsX () const noexcept |
|
Returns the number of cells along the X axis. More... | |
| unsigned int | cellsY () const noexcept |
|
Returns the number of cells along the Y axis. More... | |
| unsigned int | cellsZ () const noexcept |
|
Returns the number of cells along the Z axis. More... | |
Private Member Functions Index
| void | initGrid () |
|
Initializes the grid based on world bounds and cell size. More... | |
| void | prepareCollisionDetection () |
|
Prepares the grid for a new collision detection pass. More... | |
| void | postEvent (const helios::engine::ecs::GameObject *candidate, const helios::engine::ecs::GameObject *match, const helios::math::vec3f contact, const CollisionStruct collisionStruct, const helios::engine::runtime::world::UpdateContext &updateContext, CollisionStateComponent *csc_a, CollisionStateComponent *csc_b) const noexcept |
|
Posts collision events to the UpdateContext's event sink. More... | |
| CollisionStruct | findCollisionType (const CollisionComponent *cc, const CollisionComponent *matchCC) const noexcept |
|
Determines the collision type between two entities based on their layer masks. More... | |
Private Member Attributes Index
| std::unordered_set< std::pair< helios::util::Guid, helios::util::Guid >, GuidPairHash > | solvedCollisions_ |
|
Set of already-processed collision pairs to avoid duplicate events. More... | |
| float | cellSize_ |
|
Size of each grid cell in world units. More... | |
| helios::math::aabbf | gridBounds_ |
|
World-space bounds defining the spatial region covered by the grid. More... | |
| std::vector< GridCell > | cells_ |
|
Flat storage for all grid cells, indexed as x + y * cellsX + z * (cellsX * cellsY). More... | |
| unsigned int | cellsX_ |
|
Number of cells along the X axis. More... | |
| unsigned int | cellsY_ |
|
Number of cells along the Y axis. More... | |
| unsigned int | cellsZ_ |
|
Number of cells along the Z axis. More... | |
| std::vector< size_t > | trackedCells_ |
|
Helper to keep track of updated cells (with potential collisions candidates) in one pass. More... | |
Private Static Attributes Index
| static const auto & | logger_ = helios::util::log::LogManager::loggerForScope(HELIOS_LOG_SCOPE) |
|
Scoped logger instance for structured logging within the current context. More... | |
Description
Collision detection system using uniform spatial partitioning for Broadphase and AABB overlaps in the Narrowphase.
This system implements a grid-based spatial partitioning approach for efficient collision detection, following the principles outlined in Ericson's "Real-Time Collision Detection" (Chapter 7). The algorithm divides the world into a uniform 3D grid of cells and assigns each collidable entity to the cells it overlaps.
The detection process consists of two phases: 1. **Broadphase:** Entities are inserted into grid cells based on their AABB. Only entities sharing the same cell are considered potential collision pairs. 2. **Narrowphase:** For each cell with multiple candidates, AABB intersection tests determine actual collisions.
Collision events are published to the `UpdateContext`'s event sink, distinguishing between:
- **Solid collisions:** Symmetric collisions where both entities can physically interact.
- **Trigger collisions:** Asymmetric collisions for gameplay logic (e.g., pickups, zones).
During the broadphase, the system uses additional layer masks to filter which entity types can collide with each other, enabling fine-grained control over collision pairs.
- See Also
- See Also
- See Also
- See Also
- See Also
[Eri05, Chapter 7]
Definition at line 74 of file GridCollisionDetectionSystem.ixx.
Public Constructors
GridCollisionDetectionSystem()
| inline explicit |
Constructs a GridCollisionDetectionSystem with specified bounds and cell size.
The grid is initialized to cover the given world bounds. Cell size determines the granularity of spatial partitioning; smaller cells reduce false positives but increase memory and insertion overhead.
- Parameters
-
bounds World-space AABB defining the region where collision detection is active.
cellSize Size of each grid cell in world units. Must be greater than zero.
Given n objects, the 1/3 rule should be applied for determining the number of cells for partitioning, i.e. with n objects, the cube should consist of k x k x k cells, with k = n^{1/3} - that is, on average, one cell fits one object that should be tested for collision. Dimensions of said cells are derived by taking the reference space into account, e.g. WorldBounds.size/k (see [HS99]).
Definition at line 385 of file GridCollisionDetectionSystem.ixx.
Public Member Functions
cell()
| inline nodiscard noexcept |
Accesses a grid cell by its 3D coordinates.
- Parameters
-
x X-coordinate of the cell (0 to cellsX - 1).
y Y-coordinate of the cell (0 to cellsY - 1).
z Z-coordinate of the cell (0 to cellsZ - 1).
- Returns
Reference to the GridCell at the specified coordinates.
Definition at line 622 of file GridCollisionDetectionSystem.ixx.
Reference cellIndex.
Referenced by solveCell and updateCollisionCandidate.
cellIndex()
| inline nodiscard constexpr noexcept |
Computes the 1D index of a cell in a 3D grid based on its x, y, and z coordinates.
- Parameters
-
x The x-coordinate of the cell (must be less than the grid's x-dimension).
y The y-coordinate of the cell (must be less than the grid's y-dimension).
z The z-coordinate of the cell (must be less than the grid's z-dimension).
- Returns
The 1D index of the cell in the grid.
Definition at line 635 of file GridCollisionDetectionSystem.ixx.
Referenced by cell and updateCollisionCandidate.
cellsX()
| inline nodiscard noexcept |
Returns the number of cells along the X axis.
- Returns
Number of grid cells in the X dimension.
Definition at line 646 of file GridCollisionDetectionSystem.ixx.
cellsY()
| inline nodiscard noexcept |
Returns the number of cells along the Y axis.
- Returns
Number of grid cells in the Y dimension.
Definition at line 655 of file GridCollisionDetectionSystem.ixx.
cellsZ()
| inline nodiscard noexcept |
Returns the number of cells along the Z axis.
- Returns
Number of grid cells in the Z dimension.
Definition at line 664 of file GridCollisionDetectionSystem.ixx.
solveCell()
| inline |
Performs narrow-phase collision detection for all candidates in a cell.
Tests all unique pairs of collision candidates within the cell using AABB intersection. For each collision detected, determines collision type (solid/trigger) based on layer masks and publishes appropriate events. Uses a solved-set to avoid duplicate events when entities span multiple cells.
- Parameters
-
cell The grid cell containing collision candidates to test.
updateContext Context for pushing collision events to the event queue.
Definition at line 546 of file GridCollisionDetectionSystem.ixx.
References helios::engine::modules::physics::collision::components::AabbColliderComponent::bounds, cell, helios::engine::ecs::GameObject::guid, helios::math::aabb< T >::intersects and helios::math::overlapCenter.
Referenced by update.
update()
| inline noexcept virtual |
Performs collision detection for all active entities.
Executes the complete collision detection pipeline each frame: 1. Clears all grid cells and resets solved collision pairs. 2. Iterates all active GameObjects with CollisionComponent and AabbColliderComponent. 3. Inserts each entity into all grid cells its AABB overlaps. 4. For each cell with multiple candidates, runs narrow-phase AABB intersection tests. 5. Publishes collision events (TriggerCollisionEvent, SolidCollisionEvent) to the event queue.
- Parameters
-
updateContext The update context providing access to GameWorld and event queue.
Definition at line 406 of file GridCollisionDetectionSystem.ixx.
References helios::engine::ecs::query::Active, helios::engine::ecs::query::ComponentEnabled, helios::engine::ecs::System::gameWorld_, helios::engine::ecs::Component::isEnabled, helios::engine::modules::physics::collision::components::CollisionComponent::solidCollisionMask, solveCell, helios::engine::modules::physics::collision::components::CollisionComponent::triggerCollisionMask, updateCollisionCandidate and worldBoundsToGridBounds.
updateCollisionCandidate()
| inline |
Inserts a collision candidate into all grid cells it overlaps.
Iterates through all cells within the given bounds and adds the candidate to each cell's collision candidate list for subsequent narrow-phase testing.
- Parameters
-
go Pointer to the GameObject entity.
bounds Grid cell index bounds (integer AABB) the entity spans.
aabbColliderComponent Pointer to the entity's AABB collider component.
collisionComponent Pointer to the entity's collision component.
Definition at line 495 of file GridCollisionDetectionSystem.ixx.
References cell, cellIndex, helios::math::aabb< T >::max and helios::math::aabb< T >::min.
Referenced by update.
worldBoundsToGridBounds()
| inline nodiscard noexcept |
Converts world-space AABB bounds to grid cell indices.
Transforms a floating-point AABB in world space to integer cell coordinates, clamped to the valid grid range. Used to determine which cells an entity occupies.
- Parameters
-
aabbf The world-space AABB to convert.
- Returns
An integer AABB representing the range of grid cell indices the entity spans.
Definition at line 460 of file GridCollisionDetectionSystem.ixx.
References helios::math::aabb< T >::max and helios::math::aabb< T >::min.
Referenced by update.
Private Member Functions
findCollisionType()
| inline nodiscard noexcept |
Determines the collision type between two entities based on their layer masks.
Evaluates both solid and trigger collision masks to determine if and how two entities can collide. Solid collisions are symmetric (both entities must accept collision with each other), while trigger collisions are asymmetric (either entity can trigger).
- Parameters
-
cc Collision component of the first entity.
matchCC Collision component of the second entity.
- Returns
CollisionStruct a struct with the requested collision information.
Definition at line 321 of file GridCollisionDetectionSystem.ixx.
initGrid()
| inline |
Initializes the grid based on world bounds and cell size.
Computes the number of cells needed in each dimension and allocates the cell storage.
Definition at line 225 of file GridCollisionDetectionSystem.ixx.
postEvent()
| inline noexcept |
Posts collision events to the UpdateContext's event sink.
Updates the CollisionStateComponent of both entities with collision information including contact point, collision type, behavior, and reporter status. An entity marked as collision reporter receives the event as the "source" entity.
- Parameters
-
candidate First collision candidate (potential event source).
match Second collision candidate (collision partner).
contact The contact point between the two AABBs.
collisionStruct Struct containing collision type and behavior information.
updateContext Context for pushing events to the event queue.
csc_a Collision state component of the first entity.
csc_b Collision state component of the second entity.
Definition at line 276 of file GridCollisionDetectionSystem.ixx.
prepareCollisionDetection()
| inline |
Prepares the grid for a new collision detection pass.
Clears all collision candidates from every cell and resets the set of already-solved collision pairs.
Definition at line 252 of file GridCollisionDetectionSystem.ixx.
Private Member Attributes
cells_
|
Flat storage for all grid cells, indexed as x + y * cellsX + z * (cellsX * cellsY).
Definition at line 197 of file GridCollisionDetectionSystem.ixx.
cellSize_
|
Size of each grid cell in world units.
Definition at line 187 of file GridCollisionDetectionSystem.ixx.
cellsX_
|
Number of cells along the X axis.
Definition at line 202 of file GridCollisionDetectionSystem.ixx.
cellsY_
|
Number of cells along the Y axis.
Definition at line 207 of file GridCollisionDetectionSystem.ixx.
cellsZ_
|
Number of cells along the Z axis.
Definition at line 212 of file GridCollisionDetectionSystem.ixx.
gridBounds_
|
World-space bounds defining the spatial region covered by the grid.
Definition at line 192 of file GridCollisionDetectionSystem.ixx.
solvedCollisions_
|
Set of already-processed collision pairs to avoid duplicate events.
Stores pairs of GUIDs in canonical order (smaller GUID first) to ensure each collision pair is processed only once per frame, even when entities span multiple cells.
Definition at line 182 of file GridCollisionDetectionSystem.ixx.
trackedCells_
|
Helper to keep track of updated cells (with potential collisions candidates) in one pass.
Definition at line 217 of file GridCollisionDetectionSystem.ixx.
Private Static Attributes
logger_
| static |
Scoped logger instance for structured logging within the current context.
Definition at line 174 of file GridCollisionDetectionSystem.ixx.
The documentation for this class was generated from the following file:
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.