Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Collision/BroadPhase/BroadPhase.h

raw symbols SCIP
1 // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2 // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3 // SPDX-License-Identifier: MIT
4
5 #pragma once
6
7 #include <Jolt/Physics/Collision/BroadPhase/BroadPhaseQuery.h>
8 #include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>
9
10 JPH_NAMESPACE_BEGIN
11
12 // Shorthand function to ifdef out code if broadphase stats tracking is off
13 #ifdef JPH_TRACK_BROADPHASE_STATS
14 #define JPH_IF_TRACK_BROADPHASE_STATS(...) __VA_ARGS__
15 #else
16 #define JPH_IF_TRACK_BROADPHASE_STATS(...)
17 #endif // JPH_TRACK_BROADPHASE_STATS
18
19 class BodyManager;
20 struct BodyPair;
21
22 using BodyPairCollector = CollisionCollector<BodyPair, CollisionCollectorTraitsCollideShape>;
23
24 /// Used to do coarse collision detection operations to quickly prune out bodies that will not collide.
25 class JPH_EXPORT BroadPhase : public BroadPhaseQuery
26 {
27 public:
28 /// Initialize the broadphase.
29 /// @param inBodyManager The body manager singleton
30 /// @param inLayerInterface Interface that maps object layers to broadphase layers.
31 /// Note that the broadphase takes a pointer to the data inside inObjectToBroadPhaseLayer so this object should remain static.
32 virtual void Init(BodyManager *inBodyManager, const BroadPhaseLayerInterface &inLayerInterface);
33
34 /// Should be called after many objects have been inserted to make the broadphase more efficient, usually done on startup only
35 virtual void Optimize() { /* Optionally overridden by implementation */ }
36
37 /// Must be called just before updating the broadphase when none of the body mutexes are locked
38 virtual void FrameSync() { /* Optionally overridden by implementation */ }
39
40 /// Must be called before UpdatePrepare to prevent modifications from being made to the tree
41 virtual void LockModifications() { /* Optionally overridden by implementation */ }
42
43 /// Context used during broadphase update
44 struct UpdateState { void *mData[4]; };
45
46 /// Update the broadphase, needs to be called frequently to update the internal state when bodies have been modified.
47 /// The UpdatePrepare() function can run in a background thread without influencing the broadphase
48 virtual UpdateState UpdatePrepare() { return UpdateState(); }
49
50 /// Finalizing the update will quickly apply the changes
51 virtual void UpdateFinalize([[maybe_unused]] const UpdateState &inUpdateState) { /* Optionally overridden by implementation */ }
52
53 /// Must be called after UpdateFinalize to allow modifications to the broadphase
54 virtual void UnlockModifications() { /* Optionally overridden by implementation */ }
55
56 /// Handle used during adding bodies to the broadphase
57 using AddState = void *;
58
59 /// Prepare adding inNumber bodies at ioBodies to the broadphase, returns a handle that should be used in AddBodiesFinalize/Abort.
60 /// This can be done on a background thread without influencing the broadphase.
61 /// ioBodies may be shuffled around by this function and should be kept that way until AddBodiesFinalize/Abort is called.
62 virtual AddState AddBodiesPrepare([[maybe_unused]] BodyID *ioBodies, [[maybe_unused]] int inNumber) { return nullptr; } // By default the broadphase doesn't support this
63
64 /// Finalize adding bodies to the broadphase, supply the return value of AddBodiesPrepare in inAddState.
65 /// Please ensure that the ioBodies array passed to AddBodiesPrepare is unmodified and passed again to this function.
66 virtual void AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState) = 0;
67
68 /// Abort adding bodies to the broadphase, supply the return value of AddBodiesPrepare in inAddState.
69 /// This can be done on a background thread without influencing the broadphase.
70 /// Please ensure that the ioBodies array passed to AddBodiesPrepare is unmodified and passed again to this function.
71 virtual void AddBodiesAbort([[maybe_unused]] BodyID *ioBodies, [[maybe_unused]] int inNumber, [[maybe_unused]] AddState inAddState) { /* By default nothing needs to be done */ }
72
73 /// Remove inNumber bodies in ioBodies from the broadphase.
74 /// ioBodies may be shuffled around by this function.
75 virtual void RemoveBodies(BodyID *ioBodies, int inNumber) = 0;
76
77 /// Call whenever the aabb of a body changes (can change order of ioBodies array)
78 /// inTakeLock should be false if we're between LockModifications/UnlockModifications, in which case care needs to be taken to not call this between UpdatePrepare/UpdateFinalize
79 virtual void NotifyBodiesAABBChanged(BodyID *ioBodies, int inNumber, bool inTakeLock = true) = 0;
80
81 /// Call whenever the layer (and optionally the aabb as well) of a body changes (can change order of ioBodies array)
82 virtual void NotifyBodiesLayerChanged(BodyID *ioBodies, int inNumber) = 0;
83
84 /// Find all colliding pairs between dynamic bodies
85 /// Note that this function is very specifically tailored for the PhysicsSystem::Update function, hence it is not part of the BroadPhaseQuery interface.
86 /// One of the assumptions it can make is that no locking is needed during the query as it will only be called during a very particular part of the update.
87 /// @param ioActiveBodies is a list of bodies for which we need to find colliding pairs (this function can change the order of the ioActiveBodies array). This can be a subset of the set of active bodies in the system.
88 /// @param inNumActiveBodies is the size of the ioActiveBodies array.
89 /// @param inSpeculativeContactDistance Distance at which speculative contact points will be created.
90 /// @param inObjectVsBroadPhaseLayerFilter is the filter that determines if an object can collide with a broadphase layer.
91 /// @param inObjectLayerPairFilter is the filter that determines if two objects can collide.
92 /// @param ioPairCollector receives callbacks for every body pair found.
93 virtual void FindCollidingPairs(BodyID *ioActiveBodies, int inNumActiveBodies, float inSpeculativeContactDistance, const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, const ObjectLayerPairFilter &inObjectLayerPairFilter, BodyPairCollector &ioPairCollector) const = 0;
94
95 /// Same as BroadPhaseQuery::CastAABox but can be implemented in a way to take no broad phase locks.
96 virtual void CastAABoxNoLock(const AABoxCast &inBox, CastShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const = 0;
97
98 /// Get the bounding box of all objects in the broadphase
99 virtual AABox GetBounds() const = 0;
100
101 #ifdef JPH_TRACK_BROADPHASE_STATS
102 /// Trace the collected broadphase stats in CSV form.
103 /// This report can be used to judge and tweak the efficiency of the broadphase.
104 virtual void ReportStats() { /* Can be implemented by derived classes */ }
105 #endif // JPH_TRACK_BROADPHASE_STATS
106
107 protected:
108 /// Link to the body manager that manages the bodies in this broadphase
109 BodyManager * mBodyManager = nullptr;
110 };
111
112 JPH_NAMESPACE_END