BroadPhaseLayer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/NonCopyable.h>
  6. #include <Jolt/Physics/Collision/ObjectLayer.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// 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.
  9. /// 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 other
  10. /// 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 its
  11. /// own sub structure in the broadphase.
  12. /// Note: This class requires explicit casting from and to Type to avoid confusion with ObjectLayer
  13. class BroadPhaseLayer
  14. {
  15. public:
  16. using Type = uint8;
  17. JPH_INLINE BroadPhaseLayer() = default;
  18. JPH_INLINE explicit constexpr BroadPhaseLayer(Type inValue) : mValue(inValue) { }
  19. JPH_INLINE constexpr BroadPhaseLayer(const BroadPhaseLayer &) = default;
  20. JPH_INLINE BroadPhaseLayer & operator = (const BroadPhaseLayer &) = default;
  21. JPH_INLINE constexpr bool operator == (const BroadPhaseLayer &inRHS) const
  22. {
  23. return mValue == inRHS.mValue;
  24. }
  25. JPH_INLINE constexpr bool operator != (const BroadPhaseLayer &inRHS) const
  26. {
  27. return mValue != inRHS.mValue;
  28. }
  29. JPH_INLINE constexpr bool operator < (const BroadPhaseLayer &inRHS) const
  30. {
  31. return mValue < inRHS.mValue;
  32. }
  33. JPH_INLINE explicit constexpr operator Type() const
  34. {
  35. return mValue;
  36. }
  37. JPH_INLINE Type GetValue() const
  38. {
  39. return mValue;
  40. }
  41. private:
  42. Type mValue;
  43. };
  44. /// Constant value used to indicate an invalid broad phase layer
  45. static constexpr BroadPhaseLayer cBroadPhaseLayerInvalid(0xff);
  46. /// Interface that the application should implement to allow mapping object layers to broadphase layers
  47. class BroadPhaseLayerInterface : public NonCopyable
  48. {
  49. public:
  50. /// Destructor
  51. virtual ~BroadPhaseLayerInterface() = default;
  52. /// Return the number of broadphase layers there are
  53. virtual uint GetNumBroadPhaseLayers() const = 0;
  54. /// Convert an object layer to the corresponding broadphase layer
  55. virtual BroadPhaseLayer GetBroadPhaseLayer(ObjectLayer inLayer) const = 0;
  56. #if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
  57. /// Get the user readable name of a broadphase layer (debugging purposes)
  58. virtual const char * GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const = 0;
  59. #endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED
  60. };
  61. /// Class to test if an object can collide with a broadphase layer. Used while finding collision pairs.
  62. class ObjectVsBroadPhaseLayerFilter : public NonCopyable
  63. {
  64. public:
  65. /// Destructor
  66. virtual ~ObjectVsBroadPhaseLayerFilter() = default;
  67. /// Returns true if an object layer should collide with a broadphase layer
  68. virtual bool ShouldCollide([[maybe_unused]] ObjectLayer inLayer1, [[maybe_unused]] BroadPhaseLayer inLayer2) const
  69. {
  70. return true;
  71. }
  72. };
  73. /// Filter class for broadphase layers
  74. class BroadPhaseLayerFilter : public NonCopyable
  75. {
  76. public:
  77. /// Destructor
  78. virtual ~BroadPhaseLayerFilter() = default;
  79. /// Function to filter out broadphase layers when doing collision query test (return true to allow testing against objects with this layer)
  80. virtual bool ShouldCollide([[maybe_unused]] BroadPhaseLayer inLayer) const
  81. {
  82. return true;
  83. }
  84. };
  85. /// Default filter class that uses the pair filter in combination with a specified layer to filter layers
  86. class DefaultBroadPhaseLayerFilter : public BroadPhaseLayerFilter
  87. {
  88. public:
  89. /// Constructor
  90. DefaultBroadPhaseLayerFilter(const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, ObjectLayer inLayer) :
  91. mObjectVsBroadPhaseLayerFilter(inObjectVsBroadPhaseLayerFilter),
  92. mLayer(inLayer)
  93. {
  94. }
  95. // See BroadPhaseLayerFilter::ShouldCollide
  96. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
  97. {
  98. return mObjectVsBroadPhaseLayerFilter.ShouldCollide(mLayer, inLayer);
  99. }
  100. private:
  101. const ObjectVsBroadPhaseLayerFilter &mObjectVsBroadPhaseLayerFilter;
  102. ObjectLayer mLayer;
  103. };
  104. /// Allows objects from a specific broad phase layer only
  105. class SpecifiedBroadPhaseLayerFilter : public BroadPhaseLayerFilter
  106. {
  107. public:
  108. /// Constructor
  109. explicit SpecifiedBroadPhaseLayerFilter(BroadPhaseLayer inLayer) :
  110. mLayer(inLayer)
  111. {
  112. }
  113. // See BroadPhaseLayerFilter::ShouldCollide
  114. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
  115. {
  116. return mLayer == inLayer;
  117. }
  118. private:
  119. BroadPhaseLayer mLayer;
  120. };
  121. JPH_NAMESPACE_END