GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Broadphase.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_PHYSICS_BROADPHASE_HPP
5#define GREM_PHYSICS_BROADPHASE_HPP
6
8
21
22#include <utility> // std::forward
23
24namespace grem::physics {
25
30 constexpr BroadphaseID() noexcept = default;
31
32 constexpr BroadphaseID(LooseOrthtreeID id) noexcept
33 : LooseOrthtreeID(id) {}
34
37
40
43
46
47 using LooseOrthtreeID::operator==;
48 using LooseOrthtreeID::operator bool;
49};
50
56template <size_t N>
58private:
60
61public:
62 using value_type = typename Tree::value_type;
63 using reference = typename Tree::reference;
65 using pointer = typename Tree::pointer;
67 using size_type = typename Tree::size_type;
69 using iterator = typename Tree::iterator;
73
78
83
91 [[nodiscard]] ElementsView getElements() noexcept {
92 return tree;
93 }
94
102 [[nodiscard]] ConstElementsView getElements() const noexcept {
103 return tree;
104 }
105
109 void clear() noexcept {
110 tree.clear();
111 }
112
121 void reset(const Box<N>& worldBoundingBox, Distance minEntitySizeApproximation) noexcept {
122 tree.reset(worldBoundingBox.in(Box<N>::UNIT), minEntitySizeApproximation.in(Distance::UNIT));
123 }
124
130 [[nodiscard]] Distance getMinEntitySizeApproximation() const noexcept {
131 return tree.getMinOrthantSize() * Distance::UNIT;
132 }
133
139 [[nodiscard]] Box<N> getRootBox() const noexcept {
140 return tree.getRootBox() * Box<N>::UNIT;
141 }
142
155 iterator insert(const Box<N>& boundingBox, EntityID entityID) {
156 return tree.insert(boundingBox.in(Box<N>::UNIT), entityID);
157 }
158
167 tree.erase(pos);
168 }
169
178 tree.erase(id);
179 }
180
188 [[nodiscard]] EntityID operator[](LooseOrthtreeID id) const {
189 return tree[id];
190 }
191
238 template <typename Callback, typename Predicate>
239 auto traverseNodes(Callback&& callback, Predicate&& predicate) const { // NOLINT(cppcoreguidelines-missing-std-forward)
240 return tree.traverseNodes([callback = std::forward<Callback>(callback)](const grem::Box<N, Scalar>& boundingBox, const const_local_iterator& first,
241 const const_local_iterator& last) mutable { return callback(boundingBox * Box<N>::UNIT, first, last); },
242 [predicate = std::forward<Predicate>(predicate)](const grem::Box<N, Scalar>& boundingBox) mutable { return predicate(boundingBox * Box<N>::UNIT); });
243 }
244
282 template <typename Callback>
283 auto traverseNodes(Callback&& callback) const {
284 return traverseNodes(std::forward<Callback>(callback), [](const Box<N>&) -> bool { return true; });
285 }
286
327 template <typename Callback, typename Predicate>
328 auto traverseEntities(Callback&& callback, Predicate&& predicate) const { // NOLINT(cppcoreguidelines-missing-std-forward)
329 return tree.traverseElements(std::forward<Callback>(callback),
330 [predicate = std::forward<Predicate>(predicate)](const grem::Box<N, Scalar>& boundingBox) mutable { return predicate(boundingBox * Box<N>::UNIT); });
331 }
332
364 template <typename Callback>
365 auto traverseEntities(Callback&& callback) const {
366 return traverseEntities(std::forward<Callback>(callback), [](const Box<N>&) -> bool { return true; });
367 }
368
406 GREM_API(physics)
408 Position<N> point, CollisionFilter filter, execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
409 CollisionFilterTest filterTest = {}, FunctionView<bool(EntityID objectID, CollisionFilterTestResult filterTestResult)> callback = [](EntityID) -> bool { return true; },
410 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const;
411
455 GREM_API(physics)
457 Length1D minPenetrationDepth, ShapeView<N> shape, CollisionFilter filter, const Transformation<N>& transformation,
458 execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
459 const CollisionAlgorithmOptions<N>& options, CollisionFilterTest filterTest = {},
460 FunctionView<bool(EntityID objectID, CollisionFilterTestResult filterTestResult)> callback = [](EntityID) -> bool { return true; },
461 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const;
462
479
521 GREM_API(physics)
523 ShapeView<N> shape, CollisionFilter filter, const Transformation<N>& transformation,
524 execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
525 const CollisionAlgorithmOptions<N>& options, CollisionFilterTest filterTest = {},
526 FunctionView<bool(const CollisionResult& collision)> callback = [](const CollisionResult&) -> bool { return true; },
527 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const;
528
561
601 GREM_API(physics)
603 const Ray<N>& ray, CollisionFilter filter, execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
604 CollisionFilterTest filterTest = {}, FunctionView<bool(const RaycastResult& hit)> callback = [](const RaycastResult&) -> bool { return true; },
605 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const;
606
641 const Ray<N>& ray, CollisionFilter filter, execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
642 CollisionFilterTest filterTest = {}, FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const {
644 raycast(
645 ray, filter, entities, filterTest,
646 [&](const RaycastResult& hit) -> bool {
647 if (hit.distance == 0) {
648 result = hit;
649 return true;
650 }
651 if (!result || hit.distance < result->distance) {
652 result = hit;
653 }
654 return false;
655 },
656 predicate);
657 return result;
658 }
659
694 const Ray<N>& ray, CollisionFilter filter, execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
695 CollisionFilterTest filterTest = {}, FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const {
697 raycast(
698 ray, filter, entities, filterTest,
699 [&](const RaycastResult& hit) -> bool {
700 result = hit;
701 return true;
702 },
703 predicate);
704 return result;
705 }
706
739
785 GREM_API(physics)
787 ConvexShapeView<N> convexShape, CollisionFilter filter, const Transformation<N>& transformation, Direction<N> direction, Distance maxDistance,
788 execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
789 const CollisionAlgorithmOptions<N>& options, CollisionFilterTest filterTest = {},
790 FunctionView<bool(const ShapecastResult& hit)> callback = [](const ShapecastResult&) -> bool { return true; },
791 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const;
792
833 ConvexShapeView<N> convexShape, CollisionFilter filter, const Transformation<N>& transformation, Direction<N> direction, Distance maxDistance,
834 execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
835 const CollisionAlgorithmOptions<N>& options, CollisionFilterTest filterTest = {},
836 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const {
838 shapecast(
839 convexShape, filter, transformation, direction, maxDistance, entities, options, filterTest,
840 [&](const ShapecastResult& hit) -> bool {
841 if (hit.distance == 0) {
842 result = hit;
843 return true;
844 }
845 if (!result || hit.distance < result->distance) {
846 result = hit;
847 }
848 return false;
849 },
850 predicate);
851 return result;
852 }
853
894 ConvexShapeView<N> convexShape, CollisionFilter filter, const Transformation<N>& transformation, Direction<N> direction, Distance maxDistance,
895 execution::Entities<const Position<N>, const Orientation<N>, const Scale<N>, const Collider<N>, const ObjectBounds<N>> entities,
896 const CollisionAlgorithmOptions<N>& options, CollisionFilterTest filterTest = {},
897 FunctionView<bool(EntityID objectID)> predicate = [](EntityID) -> bool { return true; }) const {
899 shapecast(
900 convexShape, filter, transformation, direction, maxDistance, entities, options, filterTest,
901 [&](const ShapecastResult& hit) -> bool {
902 result = hit;
903 return true;
904 },
905 predicate);
906 return result;
907 }
908
920 template <typename Predicate>
921 friend size_type erase_if(Broadphase& c, Predicate&& predicate) {
922 return erase_if(c.tree, std::forward<Predicate>(predicate));
923 }
924
925private:
926 Tree tree{};
927};
928
931
932} // namespace grem::physics
933
934#endif
Definition FunctionView.hpp:29
Opaque handle to an element in a LooseOrthtree.
Definition LooseOrthtree.hpp:32
constexpr LooseOrthtreeID() noexcept=default
Orthtree-based space subdivision structure, optimized for intersection queries between axis-aligned b...
Definition LooseOrthtree.hpp:65
const EntityID & const_reference
Definition LooseOrthtree.hpp:71
size_t size_type
Definition LooseOrthtree.hpp:74
EntityID & reference
Definition LooseOrthtree.hpp:70
LocalIterator< true > const_local_iterator
Definition LooseOrthtree.hpp:215
EntityID value_type
Definition LooseOrthtree.hpp:69
Iterator< false > iterator
Iterator type of the container.
Definition LooseOrthtree.hpp:212
const EntityID * const_pointer
Definition LooseOrthtree.hpp:73
LocalIterator< false > local_iterator
Definition LooseOrthtree.hpp:214
EntityID * pointer
Definition LooseOrthtree.hpp:72
Iterator< true > const_iterator
Const iterator type of the container.
Definition LooseOrthtree.hpp:213
ptrdiff_t difference_type
Definition LooseOrthtree.hpp:75
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition Subrange.hpp:88
View of entities in an EntityRegistry that have all of the required components and none of the exclud...
Definition EntityRegistry.hpp:815
Acceleration structure for spatial queries in a simulation.
Definition Broadphase.hpp:57
typename Tree::local_iterator local_iterator
Local iterator type for the nodes of the container.
Definition Broadphase.hpp:71
typename Tree::size_type size_type
Size type of the container.
Definition Broadphase.hpp:67
bool shapecast(ConvexShapeView< N > convexShape, CollisionFilter filter, const Transformation< N > &transformation, Direction< N > direction, Distance maxDistance, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, const CollisionAlgorithmOptions< N > &options, CollisionFilterTest filterTest={}, FunctionView< bool(const ShapecastResult &hit)> callback=[](const ShapecastResult &) -> bool { return true;}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
bool testShape(Length1D minPenetrationDepth, ShapeView< N > shape, CollisionFilter filter, const Transformation< N > &transformation, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, const CollisionAlgorithmOptions< N > &options, CollisionFilterTest filterTest={}, FunctionView< bool(EntityID objectID, CollisionFilterTestResult filterTestResult)> callback=[](EntityID) -> bool { return true;}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
auto traverseNodes(Callback &&callback, Predicate &&predicate) const
Execute a callback function for each active node of the broadphase, including empty internal nodes.
Definition Broadphase.hpp:239
Subrange< iterator, iterator, SubrangeKind::SIZED > ElementsView
Non-owning view over the elements in a broadphase.
Definition Broadphase.hpp:77
Distance getMinEntitySizeApproximation() const noexcept
Get the current minimum entity size approximation of the broadphase.
Definition Broadphase.hpp:130
Optional< RaycastResult > raycastClosestHit(const Ray< N > &ray, CollisionFilter filter, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, CollisionFilterTest filterTest={}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
Find the closest collision between a point and the objects in the broadphase when the point is cast l...
Definition Broadphase.hpp:640
Optional< RaycastResult > raycastAnyHit(const Ray< N > &ray, CollisionFilter filter, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, CollisionFilterTest filterTest={}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
Find any collision between a point and the objects in the broadphase when the point is cast like a ra...
Definition Broadphase.hpp:693
auto traverseEntities(Callback &&callback) const
Execute a callback function for each entity in the broadphase.
Definition Broadphase.hpp:365
bool raycast(const Ray< N > &ray, CollisionFilter filter, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, CollisionFilterTest filterTest={}, FunctionView< bool(const RaycastResult &hit)> callback=[](const RaycastResult &) -> bool { return true;}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
bool collide(ShapeView< N > shape, CollisionFilter filter, const Transformation< N > &transformation, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, const CollisionAlgorithmOptions< N > &options, CollisionFilterTest filterTest={}, FunctionView< bool(const CollisionResult &collision)> callback=[](const CollisionResult &) -> bool { return true;}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
typename Tree::iterator iterator
Iterator type of the container.
Definition Broadphase.hpp:69
Optional< ShapecastResult > shapecastAnyHit(ConvexShapeView< N > convexShape, CollisionFilter filter, const Transformation< N > &transformation, Direction< N > direction, Distance maxDistance, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, const CollisionAlgorithmOptions< N > &options, CollisionFilterTest filterTest={}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
Find any collision between an object of a convex shape and the objects in the broadphase when the obj...
Definition Broadphase.hpp:893
Subrange< const_iterator, const_iterator, SubrangeKind::SIZED > ConstElementsView
Non-owning read-only view over the elements in a broadphase.
Definition Broadphase.hpp:82
iterator insert(const Box< N > &boundingBox, EntityID entityID)
Insert an entity into the broadphase.
Definition Broadphase.hpp:155
auto traverseEntities(Callback &&callback, Predicate &&predicate) const
Execute a callback function for each entity in the broadphase.
Definition Broadphase.hpp:328
typename Tree::const_reference const_reference
Const reference type of the container.
Definition Broadphase.hpp:64
typename Tree::difference_type difference_type
Difference type of the container.
Definition Broadphase.hpp:68
ElementsView getElements() noexcept
Get a view over the elements in the broadphase.
Definition Broadphase.hpp:91
void clear() noexcept
Erase all elements from the broadphase and reset it to its empty state.
Definition Broadphase.hpp:109
Box< N > getRootBox() const noexcept
Get the current root box of the broadphase.
Definition Broadphase.hpp:139
typename Tree::const_pointer const_pointer
Const pointer type of the container.
Definition Broadphase.hpp:66
void reset(const Box< N > &worldBoundingBox, Distance minEntitySizeApproximation) noexcept
Reset the broadphase to an empty state with new world parameters.
Definition Broadphase.hpp:121
Optional< ShapecastResult > shapecastClosestHit(ConvexShapeView< N > convexShape, CollisionFilter filter, const Transformation< N > &transformation, Direction< N > direction, Distance maxDistance, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, const CollisionAlgorithmOptions< N > &options, CollisionFilterTest filterTest={}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
Find the closest collision between an object of a convex shape and the objects in the broadphase when...
Definition Broadphase.hpp:832
ConstElementsView getElements() const noexcept
Get a read-only view over the elements in the broadphase.
Definition Broadphase.hpp:102
typename Tree::reference reference
Reference type of the container.
Definition Broadphase.hpp:63
auto traverseNodes(Callback &&callback) const
Execute a callback function for each active node of the broadphase, including empty internal nodes.
Definition Broadphase.hpp:283
typename Tree::const_local_iterator const_local_iterator
Const local iterator type for the nodes of the container.
Definition Broadphase.hpp:72
typename Tree::value_type value_type
Value type of the container.
Definition Broadphase.hpp:62
bool testPoint(Position< N > point, CollisionFilter filter, execution::Entities< const Position< N >, const Orientation< N >, const Scale< N >, const Collider< N >, const ObjectBounds< N > > entities, CollisionFilterTest filterTest={}, FunctionView< bool(EntityID objectID, CollisionFilterTestResult filterTestResult)> callback=[](EntityID) -> bool { return true;}, FunctionView< bool(EntityID objectID)> predicate=[](EntityID) -> bool { return true;}) const
EntityID operator[](LooseOrthtreeID id) const
Get the entity with a specific handle.
Definition Broadphase.hpp:188
friend size_type erase_if(Broadphase &c, Predicate &&predicate)
Erase all entities that match a predicate from the broadphase.
Definition Broadphase.hpp:921
void erase(BroadphaseID id)
Remove an entity from the broadphase.
Definition Broadphase.hpp:177
typename Tree::const_iterator const_iterator
Const iterator type of the container.
Definition Broadphase.hpp:70
void erase(const_iterator pos)
Remove an entity from the broadphase.
Definition Broadphase.hpp:166
View of a generic convex shape.
Definition Shape.hpp:3147
View of a generic shape.
Definition Shape.hpp:2833
Affine transformation in world space.
Definition quantities.hpp:5104
Concept that checks if a type is a valid shape type.
Definition Shape.hpp:1227
Definition concepts.hpp:106
Definition Chunk.hpp:19
Definition Broadphase.hpp:24
Length1D Distance
Scalar length quantity.
Definition quantities.hpp:4962
Length< 1 > Length1D
Relative distance/length offset in 1-dimensional space. Unit: Meters.
Definition quantities.hpp:4925
execution::EntityID EntityID
Opaque handle to a specific entity in a Simulation's EntityRegistry.
Definition EntityID.hpp:16
Broadphase< 2 > Broadphase2D
Acceleration structure for spatial queries in a 2-dimensional simulation.
Definition Broadphase.hpp:929
Quantity< N, Meters > Length
Relative distance/length offset quantity.
Definition quantities.hpp:4924
Quantity< N, Absolute< Meters > > Position
Absolute position in world space.
Definition quantities.hpp:4949
Quantity< N, Unitless > Scale
Dimensionless scale.
Definition quantities.hpp:3984
Broadphase< 3 > Broadphase3D
Acceleration structure for spatial queries in a 3-dimensional simulation.
Definition Broadphase.hpp:930
Generic axis-aligned box shape with minimum and maximum extents in N-dimensional space.
Definition geometry.hpp:149
Definition Pair.hpp:53
Description of an axis-aligned box in world space.
Definition Shape.hpp:813
static constexpr Unit UNIT
Unit.
Definition Shape.hpp:817
GREM_ALWAYS_INLINE constexpr grem::Box< N, Scalar > in(OtherUnitT unit) const noexcept
Convert this physical box to a unitless box given a desired unit.
Definition Shape.hpp:844
Opaque handle to an entity in a Broadphase.
Definition Broadphase.hpp:29
constexpr BroadphaseID(LooseOrthtree< 3, Scalar, EntityID >::const_iterator pos) noexcept
Definition Broadphase.hpp:44
constexpr BroadphaseID() noexcept=default
constexpr BroadphaseID(LooseOrthtree< 2, Scalar, EntityID >::iterator pos) noexcept
Definition Broadphase.hpp:35
constexpr BroadphaseID(LooseOrthtree< 3, Scalar, EntityID >::iterator pos) noexcept
Definition Broadphase.hpp:41
constexpr BroadphaseID(LooseOrthtree< 2, Scalar, EntityID >::const_iterator pos) noexcept
Definition Broadphase.hpp:38
Result of a collision check of a shape against the entities in a broadphase.
Definition Broadphase.hpp:467
EntityID objectID
Handle to the object that the shape collided with.
Definition Broadphase.hpp:471
ContactManifold< N > manifold
Contact manifold of the collision, where the first object is the given shape and the second object is...
Definition Broadphase.hpp:477
Result of a raycast against the objects in a broadphase.
Definition Broadphase.hpp:532
CollisionFilterTestResult filterTestResult
Result of the collision filter test.
Definition Broadphase.hpp:559
Length< N > localOffset
Offset from the object's center of mass in shape-local space at which the collision occured.
Definition Broadphase.hpp:542
Direction< N > normal
Unit vector perpendicular to the separating plane of the collision, pointing away from the object's s...
Definition Broadphase.hpp:548
EntityID objectID
Handle to the object that the ray collided with.
Definition Broadphase.hpp:536
Distance distance
Distance from the raycast origin along the raycast direction at which which the ray collided.
Definition Broadphase.hpp:554
Result of a shapecast against the objects in a broadphase.
Definition Broadphase.hpp:710
Distance distance
Distance along the shapecast direction at which the collision occured.
Definition Broadphase.hpp:732
EntityID objectID
Handle to the object that the shape collided with.
Definition Broadphase.hpp:714
CollisionFilterTestResult filterTestResult
Result of the collision filter test.
Definition Broadphase.hpp:737
Pair< Length< N > > localOffsets
Offsets from the objects' centers of mass in shape-local space at which the collision occured.
Definition Broadphase.hpp:720
Direction< N > normal
Unit vector perpendicular to the separating plane of the collision, pointing away from the second obj...
Definition Broadphase.hpp:726
Generic collider.
Definition Shape.hpp:2812
Configuration options for a collision algorithm.
Definition collision.hpp:373
Result of a CollisionFilterTest.
Definition Shape.hpp:283
Test that checks if a pair of colliders should collide.
Definition Shape.hpp:337
Collision filtering options for a collider.
Definition Shape.hpp:272
Set of points on the contact surface between two potentially colliding objects.
Definition collision.hpp:287
Normalized unit vector.
Definition quantities.hpp:4105
Bounds of an object.
Definition objects.hpp:232
Absolute orientation in world space.
Definition quantities.hpp:4431
Description of a ray.
Definition Shape.hpp:392
Result of a raycast.
Definition Shape.hpp:654
Result of a shapecast.
Definition collision.hpp:1057