8#include <aegis2d/ecs/export.hpp>
31 const Vec2 center = transform.position + collider.offset;
32 const Vec2 half = collider.size * 0.5F;
33 return Aabb{.
min = center - half, .max = center + half};
42 return min.x < other.max.x && other.min.x <
max.x &&
min.y < other.max.y &&
55 return (attacker.mask & target.layer) != 0U;
99 const Contact&)
noexcept =
default;
131 static constexpr std::size_t max_cells = std::size_t{1} << 20U;
154 insert(entity, Aabb::of(transform, collider));
163 [[nodiscard]] std::vector<CollisionPair>
pairs()
const;
167 void pairs(std::vector<CollisionPair>& out)
const;
177 void query(
const Aabb& area, std::vector<Entity>& out)
const;
181 [[nodiscard]] std::size_t
size() const noexcept;
185 [[nodiscard]]
std::
size_t columns() const noexcept;
189 [[nodiscard]]
std::
size_t rows() const noexcept;
193 [[nodiscard]]
float cell_size() const noexcept;
197 std::size_t first_column = 0;
198 std::size_t last_column = 0;
199 std::size_t first_row = 0;
200 std::size_t last_row = 0;
208 [[nodiscard]] std::size_t cell_of(
float coordinate, std::size_t count)
const noexcept;
211 std::size_t columns_ = 0;
212 std::size_t rows_ = 0;
213 std::vector<Box> boxes_;
214 std::vector<std::vector<std::uint32_t>> cells_;
227 std::vector<Contact>& out) {
232 if (!first_collider || !second_collider) {
236 .second = pair.second,
237 .first_hits_second =
hits(*first_collider, *second_collider),
238 .second_hits_first =
hits(*second_collider, *first_collider)};
239 if (!contact.first_hits_second && !contact.second_hits_first) {
244 if (!first_transform || !second_transform) {
247 if (
Aabb::of(*first_transform, *first_collider)
249 out.push_back(contact);
260 std::vector<Contact> contacts;
Broad phase of collision detection: a uniform grid over the play area.
Definition collision.hpp:120
CollisionGrid(Vec2 area, float cell_size)
Creates an empty grid.
std::size_t size() const noexcept
Number of recorded boxes.
void query(const Aabb &area, std::vector< Entity > &out) const
Entities recorded in the cells an area touches.
void pairs(std::vector< CollisionPair > &out) const
Same as pairs(), written into a vector the caller reuses.
void clear() noexcept
Removes every box; the cells keep their memory.
std::vector< CollisionPair > pairs() const
Entities that share at least one cell.
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Entry point of the ECS: creates entities and stores their components.
Definition registry.hpp:34
std::optional< std::reference_wrapper< Component > > try_get(Entity entity)
Component of an entity, if it has one.
Definition registry.hpp:147
Components shared by the client and the server.
Generational entity handle: an index into the component pools plus a generation.
Vector and matrix types of the engine (GLM behind engine names).
Entity-Component-System used by client and server.
Definition animation.hpp:13
void find_contacts(const Registry ®istry, std::span< const CollisionPair > candidates, std::vector< Contact > &out)
Narrow phase: keeps the candidate pairs whose layers interact and boxes overlap.
Definition collision.hpp:226
constexpr bool hits(const Collider &attacker, const Collider &target) noexcept
Tells whether a collider can hit another one, according to their layers.
Definition collision.hpp:54
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Definition endpoint.hpp:85
Registry: owns the entities and one component pool per component type.
Axis-aligned box given by its two corners, in logical pixels.
Definition collision.hpp:20
Vec2 max
Bottom-right corner (largest x and y).
Definition collision.hpp:24
static Aabb of(const Transform &transform, const Collider &collider) noexcept
Box of an entity's collider, placed on its transform.
Definition collision.hpp:30
bool overlaps(const Aabb &other) const noexcept
Tells whether two boxes overlap.
Definition collision.hpp:41
Vec2 min
Top-left corner (smallest x and y).
Definition collision.hpp:22
Axis-aligned box an entity collides with.
Definition components.hpp:101
Two entities that may collide, the one with the smaller handle first.
Definition collision.hpp:59
friend constexpr std::strong_ordering operator<=>(const CollisionPair &, const CollisionPair &) noexcept=default
Orders the pairs by first, then second entity.
Entity second
Entity with the larger handle.
Definition collision.hpp:63
friend constexpr bool operator==(const CollisionPair &, const CollisionPair &) noexcept=default
Compares the pairs.
static constexpr CollisionPair of(Entity a, Entity b) noexcept
Builds a pair in canonical order, whatever the order of the arguments.
Definition collision.hpp:69
Entity first
Entity with the smaller handle.
Definition collision.hpp:61