|
| | 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 ®istry) |
| | 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.
|
| |
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:
Broad phase of collision detection: a uniform grid over the play area.
Definition collision.hpp:120
void build(Registry ®istry)
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.
| 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
-
| area | Area to look in, such as the square around a tower's range. |
| out | Cleared, then filled with the entities. |