Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
collision.hpp
Go to the documentation of this file.
1
4#pragma once
8#include <aegis2d/ecs/export.hpp>
10
11#include <compare>
12#include <cstddef>
13#include <cstdint>
14#include <span>
15#include <vector>
16
17namespace aegis2d::ecs {
18
20struct Aabb {
25
30 [[nodiscard]] static Aabb of(const Transform& transform, const Collider& collider) noexcept {
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};
34 }
35
41 [[nodiscard]] bool overlaps(const Aabb& other) const noexcept {
42 return min.x < other.max.x && other.min.x < max.x && min.y < other.max.y &&
43 other.min.y < max.y;
44 }
45};
46
54[[nodiscard]] constexpr bool hits(const Collider& attacker, const Collider& target) noexcept {
55 return (attacker.mask & target.layer) != 0U;
56}
57
64
69 [[nodiscard]] static constexpr CollisionPair of(Entity a, Entity b) noexcept {
70 return b < a ? CollisionPair{.first = b, .second = a}
71 : CollisionPair{.first = a, .second = b};
72 }
73
75 friend constexpr bool operator==(const CollisionPair&, const CollisionPair&) noexcept = default;
77 friend constexpr std::strong_ordering operator<=>(const CollisionPair&,
78 const CollisionPair&) noexcept = default;
79};
80
85struct Contact {
91 bool first_hits_second = false;
93 bool second_hits_first = false;
94
96 friend constexpr bool operator==(const Contact&, const Contact&) noexcept = default;
98 friend constexpr std::strong_ordering operator<=>(const Contact&,
99 const Contact&) noexcept = default;
100};
101
120class AEGIS2D_ECS_EXPORT CollisionGrid {
121public:
128 CollisionGrid(Vec2 area, float cell_size);
129
131 static constexpr std::size_t max_cells = std::size_t{1} << 20U;
132
134 void clear() noexcept;
135
142 void insert(Entity entity, const Aabb& box);
143
150 void build(Registry& registry) {
151 clear();
152 registry.view<Transform, Collider>().each(
153 [this](Entity entity, const Transform& transform, const Collider& collider) {
154 insert(entity, Aabb::of(transform, collider));
155 });
156 }
157
163 [[nodiscard]] std::vector<CollisionPair> pairs() const;
164
167 void pairs(std::vector<CollisionPair>& out) const;
168
177 void query(const Aabb& area, std::vector<Entity>& out) const;
178
181 [[nodiscard]] std::size_t size() const noexcept;
182
185 [[nodiscard]] std::size_t columns() const noexcept;
186
189 [[nodiscard]] std::size_t rows() const noexcept;
190
193 [[nodiscard]] float cell_size() const noexcept;
194
195private:
196 struct CellRange {
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;
201 };
202
203 struct Box {
204 Entity entity;
205 CellRange cells;
206 };
207
208 [[nodiscard]] std::size_t cell_of(float coordinate, std::size_t count) const noexcept;
209
210 float cell_size_;
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_;
215};
216
226inline void find_contacts(const Registry& registry, std::span<const CollisionPair> candidates,
227 std::vector<Contact>& out) {
228 out.clear();
229 for (const CollisionPair& pair : candidates) {
230 const auto first_collider = registry.try_get<Collider>(pair.first);
231 const auto second_collider = registry.try_get<Collider>(pair.second);
232 if (!first_collider || !second_collider) {
233 continue;
234 }
235 const Contact contact{.first = pair.first,
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) {
240 continue;
241 }
242 const auto first_transform = registry.try_get<Transform>(pair.first);
243 const auto second_transform = registry.try_get<Transform>(pair.second);
244 if (!first_transform || !second_transform) {
245 continue;
246 }
247 if (Aabb::of(*first_transform, *first_collider)
248 .overlaps(Aabb::of(*second_transform, *second_collider))) {
249 out.push_back(contact);
250 }
251 }
252}
253
258[[nodiscard]] inline std::vector<Contact> find_contacts(const Registry& registry,
259 const CollisionGrid& grid) {
260 std::vector<Contact> contacts;
261 find_contacts(registry, grid.pairs(), contacts);
262 return contacts;
263}
264
265} // namespace aegis2d::ecs
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 &registry, 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
Two entities whose boxes overlap and whose layers interact.
Definition collision.hpp:85
Entity second
Entity with the larger handle.
Definition collision.hpp:89
bool first_hits_second
hits(first's collider, second's collider).
Definition collision.hpp:91
bool second_hits_first
hits(second's collider, first's collider).
Definition collision.hpp:93
Entity first
Entity with the smaller handle.
Definition collision.hpp:87
friend constexpr bool operator==(const Contact &, const Contact &) noexcept=default
Compares the contacts.
friend constexpr std::strong_ordering operator<=>(const Contact &, const Contact &) noexcept=default
Orders the contacts by entities, then flags.
Where an entity is.
Definition components.hpp:17