@@ -13,51 +13,10 @@ struct DynamicBox;
1313struct DynamicSphere ;
1414struct DynamicCapsule ;
1515struct DynamicDoor ;
16+ struct AxisAlignedBoundingBox ;
17+ template <typename T>
18+ struct BoundingVolumeHierarchy ;
1619
17- struct World {
18- std::vector<Box> boxes;
19- std::vector<Sphere> spheres;
20- std::vector<Capsule> capsules;
21- std::vector<DynamicBox> dynamic_boxes;
22- std::vector<DynamicSphere> dynamic_spheres;
23- std::vector<DynamicCapsule> dynamic_capsules;
24- std::vector<DynamicDoor> dynamic_doors;
25- u32 size = 0 ;
26-
27- host_fn void add_box (const Box& box);
28- host_fn void add_box (Vec3 center_point, Vec3 half_width, Mat3 rotation_matrix);
29-
30- host_fn void add_sphere (const Sphere& sphere);
31- host_fn void add_sphere (Vec3 center_point, real radius);
32-
33- host_fn void add_capsule (const Capsule& capsule);
34- host_fn void add_capsule (Vec3 point1, Vec3 point2, real radius);
35-
36- host_fn void add_dynamic_box (const DynamicBox& box);
37- host_fn void add_dynamic_box (const std::vector<Box>& new_boxes, u32 n_points, real start_time, real end_time);
38-
39- host_fn void add_dynamic_sphere (const DynamicSphere& sphere);
40- host_fn void add_dynamic_sphere (const std::vector<Sphere>& new_spheres, u32 n_points, real start_time, real end_time);
41-
42- host_fn void add_dynamic_capsule (const DynamicCapsule& capsule);
43- host_fn void add_dynamic_capsule (const std::vector<Capsule>& new_capsules, u32 n_points, real start_time, real end_time);
44- };
45-
46- struct CollisionModel {
47- std::vector<Box> boxes;
48- std::vector<Sphere> spheres;
49- std::vector<Capsule> capsules;
50- u32 size = 0 ;
51-
52- host_fn void add_box (const Box& box);
53- host_fn void add_box (Vec3 center_point, Vec3 half_width, Mat3 rotation_matrix);
54-
55- host_fn void add_sphere (const Sphere& sphere);
56- host_fn void add_sphere (Vec3 center_point, real radius);
57-
58- host_fn void add_capsule (const Capsule& capsule);
59- host_fn void add_capsule (Vec3 point1, Vec3 point2, real radius);
60- };
6120
6221struct PointCloud {
6322 Vec3 position;
@@ -122,6 +81,146 @@ struct DynamicDoor {
12281 inline blast_fn Box lookup (real t) const ;
12382};
12483
84+ enum class CollisionObjectType {
85+ box,
86+ sphere,
87+ capsule,
88+ aabb,
89+ door,
90+ };
91+ struct CollisionEntities {
92+ CollisionObjectType other_object_type = CollisionObjectType::box;
93+ union {
94+ Box box{};
95+ Sphere sphere;
96+ Capsule capsule;
97+ };
98+
99+ int point_in_segment = 0 ;
100+ };
101+
102+ struct AxisAlignedBoundingBox {
103+
104+ Vec3 center{};
105+ Vec3 extents{};
106+ size_t children[2 ];
107+ CollisionObjectType child_type = CollisionObjectType::aabb;
108+ const void * child_ptr = nullptr ;
109+ int point_in_segment = -1 ;
110+ real dist = 0.0 ;
111+ };
112+
113+ struct AABBPair {
114+ int aabb_obj;
115+ int aabb_cap;
116+ real dist;
117+ };
118+
119+ struct Compare {
120+ const std::vector<AxisAlignedBoundingBox>* leaves = nullptr ;
121+
122+ bool operator ()(int id1, int id2) const {
123+ return (*leaves)[id1].dist > (*leaves)[id2].dist ;
124+ }
125+
126+ bool operator ()(const AABBPair& a, const AABBPair& b) const {
127+ return a.dist > b.dist ;
128+ }
129+ };
130+ template <typename T = int >
131+ struct BoundingVolumeHierarchy {
132+
133+ std::vector<AxisAlignedBoundingBox> leaves{};
134+
135+ int root = -1 ;
136+ int num_objects = 0 ;
137+ real time = 0.0 ;
138+
139+ struct PriorityQueue : public std ::priority_queue<T, std::vector<T>, Compare> {
140+ PriorityQueue (const std::vector<AxisAlignedBoundingBox>* leaves_ptr = nullptr ) :
141+ std::priority_queue<T, std::vector<T>, Compare>(Compare{leaves_ptr}) {}
142+
143+ void clear_and_reserve (size_t capacity) {
144+ this ->c .clear (); // Clears elements without deallocating vector capacity
145+ this ->c .reserve (capacity); // Ensures internal buffer is pre-allocated
146+ }
147+ void rebind (const std::vector<AxisAlignedBoundingBox>* leaves_ptr) {
148+ this ->c .clear ();
149+ this ->comp = Compare{leaves_ptr};
150+ }
151+ };
152+ PriorityQueue queue{&leaves};
153+
154+ BoundingVolumeHierarchy () :
155+ queue (&leaves) {}
156+
157+ // Ensure copy/move operations rebind the comparator's pointer to the local leaves vector
158+ BoundingVolumeHierarchy (const BoundingVolumeHierarchy& other) :
159+ leaves (other.leaves),
160+ root (other.root),
161+ num_objects (other.num_objects),
162+ time (other.time),
163+ queue (&leaves) {}
164+
165+ BoundingVolumeHierarchy& operator =(const BoundingVolumeHierarchy& other) {
166+ if (this != &other) {
167+ leaves = other.leaves ;
168+ root = other.root ;
169+ num_objects = other.num_objects ;
170+ time = other.time ;
171+ queue.rebind (&leaves);
172+ }
173+ return *this ;
174+ }
175+ };
176+
177+ struct World {
178+ std::vector<Box> boxes;
179+ std::vector<Sphere> spheres;
180+ std::vector<Capsule> capsules;
181+ std::vector<DynamicBox> dynamic_boxes;
182+ std::vector<DynamicSphere> dynamic_spheres;
183+ std::vector<DynamicCapsule> dynamic_capsules;
184+ std::vector<DynamicDoor> dynamic_doors;
185+ BoundingVolumeHierarchy<int > static_bounding_volume_hierarchy;
186+ BoundingVolumeHierarchy<int > dynamic_bounding_volume_hierarchy;
187+ u32 size = 0 ;
188+ // ...
189+ host_fn void add_box (const Box& box);
190+ host_fn void add_box (Vec3 center_point, Vec3 half_width, Mat3 rotation_matrix);
191+
192+ host_fn void add_sphere (const Sphere& sphere);
193+ host_fn void add_sphere (Vec3 center_point, real radius);
194+
195+ host_fn void add_capsule (const Capsule& capsule);
196+ host_fn void add_capsule (Vec3 point1, Vec3 point2, real radius);
197+
198+ host_fn void add_dynamic_box (const DynamicBox& box);
199+ host_fn void add_dynamic_box (const std::vector<Box>& new_boxes, u32 n_points, real start_time, real end_time);
200+
201+ host_fn void add_dynamic_sphere (const DynamicSphere& sphere);
202+ host_fn void add_dynamic_sphere (const std::vector<Sphere>& new_spheres, u32 n_points, real start_time, real end_time);
203+
204+ host_fn void add_dynamic_capsule (const DynamicCapsule& capsule);
205+ host_fn void add_dynamic_capsule (const std::vector<Capsule>& new_capsules, u32 n_points, real start_time, real end_time);
206+ };
207+
208+ struct CollisionModel {
209+ std::vector<Box> boxes;
210+ std::vector<Sphere> spheres;
211+ std::vector<Capsule> capsules;
212+ u32 size = 0 ;
213+
214+ host_fn void add_box (const Box& box);
215+ host_fn void add_box (Vec3 center_point, Vec3 half_width, Mat3 rotation_matrix);
216+
217+ host_fn void add_sphere (const Sphere& sphere);
218+ host_fn void add_sphere (Vec3 center_point, real radius);
219+
220+ host_fn void add_capsule (const Capsule& capsule);
221+ host_fn void add_capsule (Vec3 point1, Vec3 point2, real radius);
222+ };
223+
125224/* *
126225 * @struct CollisionModelCapsule
127226 * @brief Simple capsule primitive for collision checking.
@@ -146,6 +245,7 @@ inline blast_fn real distance(const Capsule& capsule1, const Capsule& capsule2);
146245inline blast_fn real distance (const Capsule& capsule, const Vec3& point);
147246inline blast_fn real distance (const Box& box, const Vec3& point);
148247inline blast_fn real distance (const Sphere& sphere, const Vec3& point);
248+ inline blast_fn real distance (const AxisAlignedBoundingBox& aabb1, const AxisAlignedBoundingBox& aabb2);
149249
150250inline blast_fn Vec3 get_point (const Array& x, const Matrix& capsule_list);
151251
@@ -162,6 +262,7 @@ inline blast_fn Vec3 get_point(const Array& x, const Matrix& capsule_list);
162262#include " world/dynamicsphere.hpp"
163263
164264#include " world/CoDO.hpp"
265+ #include " world/broadphase.hpp"
165266
166267#include " world/scenes.hpp"
167268
0 commit comments