Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::CollisionGrid Class Reference

Broad phase of collision detection: a uniform grid over the play area. More...

#include <aegis2d/ecs/collision.hpp>

Public Member Functions

 CollisionGrid (Vec2 area, float cell_size)
 Creates an empty grid.
 
void clear () noexcept
 Removes every box; the cells keep their memory.
 
void insert (Entity entity, const Aabb &box)
 Records the box of an entity.
 
void build (Registry &registry)
 Clears the grid and records every entity that has a Transform and a Collider.
 
std::vector< CollisionPair > pairs () const
 Entities that share at least one cell.
 
void pairs (std::vector< CollisionPair > &out) const
 Same as pairs(), written into a vector the caller reuses.
 
void query (const Aabb &area, std::vector< Entity > &out) const
 Entities recorded in the cells an area touches.
 
std::size_t size () const noexcept
 Number of recorded boxes.
 
std::size_t columns () const noexcept
 Number of cell columns.
 
std::size_t rows () const noexcept
 Number of cell rows.
 
float cell_size () const noexcept
 Side of a cell.
 

Static Public Attributes

static constexpr std::size_t max_cells = std::size_t{1} << 20U
 Most cells a grid can have (one million).
 

Detailed Description

Broad phase of collision detection: a uniform grid over the play area.

The play area, from (0, 0) to its size, is cut into square cells. Each box is recorded in every cell it touches, and pairs() returns the entities that share at least one cell: the only ones a narrow phase needs to test, in about O(n) instead of the O(n²) of testing every pair. Every pair whose boxes overlap is returned; pairs whose boxes are only close may be returned too.

Boxes partly or fully outside the area are kept in the border cells, so entities entering or leaving the screen still collide. The grid is rebuilt every tick:

ecs::CollisionGrid grid{Vec2{1280.0F, 720.0F}, 64.0F}; // once
grid.build(registry); // every tick
for (const ecs::CollisionPair& pair : grid.pairs()) { /* narrow phase */ }
Broad phase of collision detection: a uniform grid over the play area.
Definition collision.hpp:120
void build(Registry &registry)
Clears the grid and records every entity that has a Transform and a Collider.
Definition collision.hpp:150
std::vector< CollisionPair > pairs() const
Entities that share at least one cell.
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Two entities that may collide, the one with the smaller handle first.
Definition collision.hpp:59

Cells keep their memory between rebuilds, so a grid used every tick stops allocating once it has seen its largest scene.

Constructor & Destructor Documentation

◆ CollisionGrid()

aegis2d::ecs::CollisionGrid::CollisionGrid ( Vec2  area,
float  cell_size 
)

Creates an empty grid.

Parameters
areaWidth and height of the play area, in logical pixels.
cell_sizeSide of a cell, in logical pixels; about the size of the largest common entity.
Exceptions
std::invalid_argumentIf a dimension or cell_size is not positive, or the grid would have more than max_cells cells.

Member Function Documentation

◆ build()

void aegis2d::ecs::CollisionGrid::build ( Registry &  registry)
inline

Clears the grid and records every entity that has a Transform and a Collider.

Defined in the header on purpose: the registry finds its pools by typeid, and with libc++ a component type does not have the same typeid inside the engine's shared library and in the game, so the view must be made in the caller's code.

Parameters
registryWorld to read.

◆ cell_size()

float aegis2d::ecs::CollisionGrid::cell_size ( ) const
noexcept

Side of a cell.

Returns
The size given to the constructor.

◆ clear()

void aegis2d::ecs::CollisionGrid::clear ( )
noexcept

Removes every box; the cells keep their memory.

◆ columns()

std::size_t aegis2d::ecs::CollisionGrid::columns ( ) const
noexcept

Number of cell columns.

Returns
ceil(area.x / cell_size).

◆ insert()

void aegis2d::ecs::CollisionGrid::insert ( Entity  entity,
const Aabb &  box 
)

Records the box of an entity.

A box whose corners are not finite (NaN, infinity) is recorded in the border cells it is clamped to, as boxes outside the area are.

Parameters
entityEntity owning the box; insert each entity once between two clear().
boxBox of the entity.
Exceptions
std::length_errorIf the grid already holds 2^32 boxes.

◆ pairs() [1/2]

std::vector< CollisionPair > aegis2d::ecs::CollisionGrid::pairs ( ) const

Entities that share at least one cell.

Each pair is reported once, even when the two boxes share several cells, in canonical order (CollisionPair::of()). The order of the pairs is deterministic: cells from top-left to bottom-right, then insertion order.

Returns
Candidate pairs for the narrow phase.

◆ pairs() [2/2]

void aegis2d::ecs::CollisionGrid::pairs ( std::vector< CollisionPair > &  out) const

Same as pairs(), written into a vector the caller reuses.

Parameters
outCleared, then filled with the pairs.

◆ query()

void aegis2d::ecs::CollisionGrid::query ( const Aabb &  area,
std::vector< Entity > &  out 
) const

Entities recorded in the cells an area touches.

Each entity is listed once, even when its box covers several of those cells, in a deterministic order: cells from top-left to bottom-right, then insertion order. Every entity whose box overlaps the area is listed; entities whose box is only close to it may be listed too, so the caller makes the exact test, as find_targets() does.

Parameters
areaArea to look in, such as the square around a tower's range.
outCleared, then filled with the entities.

◆ rows()

std::size_t aegis2d::ecs::CollisionGrid::rows ( ) const
noexcept

Number of cell rows.

Returns
ceil(area.y / cell_size).

◆ size()

std::size_t aegis2d::ecs::CollisionGrid::size ( ) const
noexcept

Number of recorded boxes.

Returns
Inserted boxes since the last clear().

Member Data Documentation

◆ max_cells

constexpr std::size_t aegis2d::ecs::CollisionGrid::max_cells = std::size_t{1} << 20U
staticconstexpr

Most cells a grid can have (one million).


The documentation for this class was generated from the following file: