ToyMaker Game Engine 0.0.2
ToyMaker is a game engine developed and maintained by Zoheb Shujauddin.
Loading...
Searching...
No Matches
sweep_prune.hpp
Go to the documentation of this file.
1
10
11#ifndef TOYMAKERENGINE_SPATIALQUERYSWEEPPRUNE_H
12#define TOYMAKERENGINE_SPATIALQUERYSWEEPPRUNE_H
13
14#include "types.hpp"
15#include "../core/ecs_world.hpp"
16
17namespace ToyMaker {
18 class SweepPrune;
19
30 private:
36
42
43 public:
50 assert(first != second && "Entities in constraint must be distinct");
51 if (second < first) {
52 std::swap(mFirst, mSecond);
53 }
54 }
55
60 inline EntityID first() const { return mFirst; }
61
66 inline EntityID second() const { return mSecond; }
67
68 inline bool operator < (const CollisionPair& other) const {
69 return (
70 mFirst < other.mFirst
71 || (
72 mFirst == other.mFirst
73 && mSecond < other.mSecond
74 )
75 );
76 }
77 };
78
85 class SweepPrune {
86 public:
91 std::set<CollisionPair> getCollisionPairs();
92
97 void addObject(EntityID entity, const AxisAlignedBounds& bounds);
98
103 void removeObject(EntityID entity);
104
109 void updateObject(EntityID entity, const AxisAlignedBounds& bounds);
110
111 private:
112 using EdgeIndex = std::size_t;
113
114 enum Axis: uint8_t {
115 X,
116 Y,
117 Z,
118 };
124 struct Object {
125 std::array<std::pair<EdgeIndex, EdgeIndex>, 3> mIndices {};
126 };
127
135 struct Edge {
136 EntityID mEntity;
137 float mValue;
138 bool mBegin;
139
140 inline bool operator<(const Edge& other) {
141 return (
142 mValue < other.mValue || (
143 mValue == other.mValue
144 && mEntity < other.mEntity
145 || (
146 mValue == other.mValue
147 && mEntity == other.mEntity
148 && mBegin > other.mBegin
149 )
150 )
151 );
152 }
153 };
154
159 void sortEdges();
160
165 void sortEdges(Axis axis);
166
171 void findCollisionPairs();
172
177 std::unordered_map<EntityID, Object> mObjects {};
178
183 std::set<CollisionPair> mCollisionPairs {};
184
189 std::array<std::vector<Edge>, 3> mEdges {};
190
195 std::array<std::set<CollisionPair>, 3> mOverlaps {};
196
201 bool mRecomputeCollisions { true };
202 };
203}
204
205#endif
An object containing a coarse simplified representation, AABB, of spatially queryable objects.
Definition types.hpp:1403
Names two distinct entities participating in a collision, with their entity IDs sorted in ascending o...
Definition sweep_prune.hpp:29
EntityID second() const
Returns the second entity in the link.
Definition sweep_prune.hpp:66
EntityID mFirst
The first entity in a constraint link.
Definition sweep_prune.hpp:35
EntityID first() const
Returns the first entity in the link.
Definition sweep_prune.hpp:60
CollisionPair(EntityID first, EntityID second)
Creates a collision pair out of two distinct entities, where the first entity's ID is lesser than the...
Definition sweep_prune.hpp:49
EntityID mSecond
The second entity in a constraint link.
Definition sweep_prune.hpp:41
A class designed around finding which pairs of AABBs may be intersecting (so as to limit the number o...
Definition sweep_prune.hpp:85
std::set< CollisionPair > mCollisionPairs
Storage for all detected collision pairs.
Definition sweep_prune.hpp:183
void updateObject(EntityID entity, const AxisAlignedBounds &bounds)
Definition sweep_prune.cpp:79
void sortEdges()
Sorts edges along every axis.
Definition sweep_prune.cpp:96
void removeObject(EntityID entity)
Removes an object from Sweep and Prune's tracking lists.
Definition sweep_prune.cpp:60
std::set< CollisionPair > getCollisionPairs()
Returns all collision pairs that have been detected thus far.
Definition sweep_prune.cpp:6
std::array< std::set< CollisionPair >, 3 > mOverlaps
Pairs of entities which are overlapping on each axis.
Definition sweep_prune.hpp:195
void findCollisionPairs()
Scans edge lists to find likely collision pairs.
Definition sweep_prune.cpp:137
void addObject(EntityID entity, const AxisAlignedBounds &bounds)
Adds an object for sweep and prune to track.
Definition sweep_prune.cpp:13
std::array< std::vector< Edge >, 3 > mEdges
Sorted list of edges along each axis.
Definition sweep_prune.hpp:189
std::unordered_map< EntityID, Object > mObjects
Indices into edge lists for each axis per entity.
Definition sweep_prune.hpp:177
bool mRecomputeCollisions
Whether collision pairs need to be recomputed, as they would after an update.
Definition sweep_prune.hpp:201
ToyMaker Engine's implementation of an ECS system.
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:25
Classes and structs representing data related to the engine's spatial query system (the precursor to ...
Represents the projection of a bounding box along the axis represented by the edge list that this ite...
Definition sweep_prune.hpp:135
Contains the start and end indices of a sweep and prune object in each dimension's edge list.
Definition sweep_prune.hpp:124