Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/Collision/BroadPhase/BroadPhaseLayer.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/Core/NonCopyable.h>7#include <Jolt/Physics/Collision/ObjectLayer.h>89JPH_NAMESPACE_BEGIN1011/// An object layer can be mapped to a broadphase layer. Objects with the same broadphase layer will end up in the same sub structure (usually a tree) of the broadphase.12/// When there are many layers, this reduces the total amount of sub structures the broad phase needs to manage. Usually you want objects that don't collide with each other13/// in different broad phase layers, but there could be exceptions if objects layers only contain a minor amount of objects so it is not beneficial to give each layer its14/// own sub structure in the broadphase.15/// Note: This class requires explicit casting from and to Type to avoid confusion with ObjectLayer16class BroadPhaseLayer17{18public:19using Type = uint8;2021JPH_INLINE BroadPhaseLayer() = default;22JPH_INLINE explicit constexpr BroadPhaseLayer(Type inValue) : mValue(inValue) { }23JPH_INLINE constexpr BroadPhaseLayer(const BroadPhaseLayer &) = default;24JPH_INLINE BroadPhaseLayer & operator = (const BroadPhaseLayer &) = default;2526JPH_INLINE constexpr bool operator == (const BroadPhaseLayer &inRHS) const27{28return mValue == inRHS.mValue;29}3031JPH_INLINE constexpr bool operator != (const BroadPhaseLayer &inRHS) const32{33return mValue != inRHS.mValue;34}3536JPH_INLINE constexpr bool operator < (const BroadPhaseLayer &inRHS) const37{38return mValue < inRHS.mValue;39}4041JPH_INLINE explicit constexpr operator Type() const42{43return mValue;44}4546JPH_INLINE Type GetValue() const47{48return mValue;49}5051private:52Type mValue;53};5455/// Constant value used to indicate an invalid broad phase layer56static constexpr BroadPhaseLayer cBroadPhaseLayerInvalid(0xff);5758/// Interface that the application should implement to allow mapping object layers to broadphase layers59class JPH_EXPORT BroadPhaseLayerInterface : public NonCopyable60{61public:62/// Destructor63virtual ~BroadPhaseLayerInterface() = default;6465/// Return the number of broadphase layers there are66virtual uint GetNumBroadPhaseLayers() const = 0;6768/// Convert an object layer to the corresponding broadphase layer69virtual BroadPhaseLayer GetBroadPhaseLayer(ObjectLayer inLayer) const = 0;7071#if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)72/// Get the user readable name of a broadphase layer (debugging purposes)73virtual const char * GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const = 0;74#endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED75};7677/// Class to test if an object can collide with a broadphase layer. Used while finding collision pairs.78class JPH_EXPORT ObjectVsBroadPhaseLayerFilter : public NonCopyable79{80public:81/// Destructor82virtual ~ObjectVsBroadPhaseLayerFilter() = default;8384/// Returns true if an object layer should collide with a broadphase layer85virtual bool ShouldCollide([[maybe_unused]] ObjectLayer inLayer1, [[maybe_unused]] BroadPhaseLayer inLayer2) const86{87return true;88}89};9091/// Filter class for broadphase layers92class JPH_EXPORT BroadPhaseLayerFilter : public NonCopyable93{94public:95/// Destructor96virtual ~BroadPhaseLayerFilter() = default;9798/// Function to filter out broadphase layers when doing collision query test (return true to allow testing against objects with this layer)99virtual bool ShouldCollide([[maybe_unused]] BroadPhaseLayer inLayer) const100{101return true;102}103};104105/// Default filter class that uses the pair filter in combination with a specified layer to filter layers106class JPH_EXPORT DefaultBroadPhaseLayerFilter : public BroadPhaseLayerFilter107{108public:109/// Constructor110DefaultBroadPhaseLayerFilter(const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, ObjectLayer inLayer) :111mObjectVsBroadPhaseLayerFilter(inObjectVsBroadPhaseLayerFilter),112mLayer(inLayer)113{114}115116// See BroadPhaseLayerFilter::ShouldCollide117virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override118{119return mObjectVsBroadPhaseLayerFilter.ShouldCollide(mLayer, inLayer);120}121122private:123const ObjectVsBroadPhaseLayerFilter &mObjectVsBroadPhaseLayerFilter;124ObjectLayer mLayer;125};126127/// Allows objects from a specific broad phase layer only128class JPH_EXPORT SpecifiedBroadPhaseLayerFilter : public BroadPhaseLayerFilter129{130public:131/// Constructor132explicit SpecifiedBroadPhaseLayerFilter(BroadPhaseLayer inLayer) :133mLayer(inLayer)134{135}136137// See BroadPhaseLayerFilter::ShouldCollide138virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override139{140return mLayer == inLayer;141}142143private:144BroadPhaseLayer mLayer;145};146147JPH_NAMESPACE_END148149150