Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/BroadPhase/BroadPhase.h
9918 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseQuery.h>7#include <Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.h>89JPH_NAMESPACE_BEGIN1011// Shorthand function to ifdef out code if broadphase stats tracking is off12#ifdef JPH_TRACK_BROADPHASE_STATS13#define JPH_IF_TRACK_BROADPHASE_STATS(...) __VA_ARGS__14#else15#define JPH_IF_TRACK_BROADPHASE_STATS(...)16#endif // JPH_TRACK_BROADPHASE_STATS1718class BodyManager;19struct BodyPair;2021using BodyPairCollector = CollisionCollector<BodyPair, CollisionCollectorTraitsCollideShape>;2223/// Used to do coarse collision detection operations to quickly prune out bodies that will not collide.24class JPH_EXPORT BroadPhase : public BroadPhaseQuery25{26public:27/// Initialize the broadphase.28/// @param inBodyManager The body manager singleton29/// @param inLayerInterface Interface that maps object layers to broadphase layers.30/// Note that the broadphase takes a pointer to the data inside inObjectToBroadPhaseLayer so this object should remain static.31virtual void Init(BodyManager *inBodyManager, const BroadPhaseLayerInterface &inLayerInterface);3233/// Should be called after many objects have been inserted to make the broadphase more efficient, usually done on startup only34virtual void Optimize() { /* Optionally overridden by implementation */ }3536/// Must be called just before updating the broadphase when none of the body mutexes are locked37virtual void FrameSync() { /* Optionally overridden by implementation */ }3839/// Must be called before UpdatePrepare to prevent modifications from being made to the tree40virtual void LockModifications() { /* Optionally overridden by implementation */ }4142/// Context used during broadphase update43struct UpdateState { void *mData[4]; };4445/// Update the broadphase, needs to be called frequently to update the internal state when bodies have been modified.46/// The UpdatePrepare() function can run in a background thread without influencing the broadphase47virtual UpdateState UpdatePrepare() { return UpdateState(); }4849/// Finalizing the update will quickly apply the changes50virtual void UpdateFinalize([[maybe_unused]] const UpdateState &inUpdateState) { /* Optionally overridden by implementation */ }5152/// Must be called after UpdateFinalize to allow modifications to the broadphase53virtual void UnlockModifications() { /* Optionally overridden by implementation */ }5455/// Handle used during adding bodies to the broadphase56using AddState = void *;5758/// Prepare adding inNumber bodies at ioBodies to the broadphase, returns a handle that should be used in AddBodiesFinalize/Abort.59/// This can be done on a background thread without influencing the broadphase.60/// ioBodies may be shuffled around by this function and should be kept that way until AddBodiesFinalize/Abort is called.61virtual AddState AddBodiesPrepare([[maybe_unused]] BodyID *ioBodies, [[maybe_unused]] int inNumber) { return nullptr; } // By default the broadphase doesn't support this6263/// Finalize adding bodies to the broadphase, supply the return value of AddBodiesPrepare in inAddState.64/// Please ensure that the ioBodies array passed to AddBodiesPrepare is unmodified and passed again to this function.65virtual void AddBodiesFinalize(BodyID *ioBodies, int inNumber, AddState inAddState) = 0;6667/// Abort adding bodies to the broadphase, supply the return value of AddBodiesPrepare in inAddState.68/// This can be done on a background thread without influencing the broadphase.69/// Please ensure that the ioBodies array passed to AddBodiesPrepare is unmodified and passed again to this function.70virtual void AddBodiesAbort([[maybe_unused]] BodyID *ioBodies, [[maybe_unused]] int inNumber, [[maybe_unused]] AddState inAddState) { /* By default nothing needs to be done */ }7172/// Remove inNumber bodies in ioBodies from the broadphase.73/// ioBodies may be shuffled around by this function.74virtual void RemoveBodies(BodyID *ioBodies, int inNumber) = 0;7576/// Call whenever the aabb of a body changes (can change order of ioBodies array)77/// inTakeLock should be false if we're between LockModifications/UnlockModifications, in which case care needs to be taken to not call this between UpdatePrepare/UpdateFinalize78virtual void NotifyBodiesAABBChanged(BodyID *ioBodies, int inNumber, bool inTakeLock = true) = 0;7980/// Call whenever the layer (and optionally the aabb as well) of a body changes (can change order of ioBodies array)81virtual void NotifyBodiesLayerChanged(BodyID *ioBodies, int inNumber) = 0;8283/// Find all colliding pairs between dynamic bodies84/// Note that this function is very specifically tailored for the PhysicsSystem::Update function, hence it is not part of the BroadPhaseQuery interface.85/// 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.86/// @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.87/// @param inNumActiveBodies is the size of the ioActiveBodies array.88/// @param inSpeculativeContactDistance Distance at which speculative contact points will be created.89/// @param inObjectVsBroadPhaseLayerFilter is the filter that determines if an object can collide with a broadphase layer.90/// @param inObjectLayerPairFilter is the filter that determines if two objects can collide.91/// @param ioPairCollector receives callbacks for every body pair found.92virtual void FindCollidingPairs(BodyID *ioActiveBodies, int inNumActiveBodies, float inSpeculativeContactDistance, const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, const ObjectLayerPairFilter &inObjectLayerPairFilter, BodyPairCollector &ioPairCollector) const = 0;9394/// Same as BroadPhaseQuery::CastAABox but can be implemented in a way to take no broad phase locks.95virtual void CastAABoxNoLock(const AABoxCast &inBox, CastShapeBodyCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter) const = 0;9697/// Get the bounding box of all objects in the broadphase98virtual AABox GetBounds() const = 0;99100#ifdef JPH_TRACK_BROADPHASE_STATS101/// Trace the collected broadphase stats in CSV form.102/// This report can be used to judge and tweak the efficiency of the broadphase.103virtual void ReportStats() { /* Can be implemented by derived classes */ }104#endif // JPH_TRACK_BROADPHASE_STATS105106protected:107/// Link to the body manager that manages the bodies in this broadphase108BodyManager * mBodyManager = nullptr;109};110111JPH_NAMESPACE_END112113114