2
0

BroadPhaseLayer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. private:
  38. Type mValue;
  39. };
  40. /// Constant value used to indicate an invalid broad phase layer
  41. static constexpr BroadPhaseLayer cBroadPhaseLayerInvalid(0xff);
  42. /// Interface that the application should implement to allow mapping object layers to broadphase layers
  43. class BroadPhaseLayerInterface : public NonCopyable
  44. {
  45. public:
  46. /// Destructor
  47. virtual ~BroadPhaseLayerInterface() = default;
  48. /// Return the number of broadphase layers there are
  49. virtual uint GetNumBroadPhaseLayers() const = 0;
  50. /// Convert an object layer to the corresponding broadphase layer
  51. virtual BroadPhaseLayer GetBroadPhaseLayer(ObjectLayer inLayer) const = 0;
  52. #if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
  53. /// Get the user readable name of a broadphase layer (debugging purposes)
  54. virtual const char * GetBroadPhaseLayerName(BroadPhaseLayer inLayer) const = 0;
  55. #endif // JPH_EXTERNAL_PROFILE || JPH_PROFILE_ENABLED
  56. };
  57. /// Class to test if an object can collide with a broadphase layer. Used while finding collision pairs.
  58. class ObjectVsBroadPhaseLayerFilter : public NonCopyable
  59. {
  60. public:
  61. /// Destructor
  62. virtual ~ObjectVsBroadPhaseLayerFilter() = default;
  63. /// Returns true if an object layer should collide with a broadphase layer
  64. virtual bool ShouldCollide([[maybe_unused]] ObjectLayer inLayer1, [[maybe_unused]] BroadPhaseLayer inLayer2) const
  65. {
  66. return true;
  67. }
  68. };
  69. /// Filter class for broadphase layers
  70. class BroadPhaseLayerFilter : public NonCopyable
  71. {
  72. public:
  73. /// Destructor
  74. virtual ~BroadPhaseLayerFilter() = default;
  75. /// Function to filter out broadphase layers when doing collision query test (return true to allow testing against objects with this layer)
  76. virtual bool ShouldCollide([[maybe_unused]] BroadPhaseLayer inLayer) const
  77. {
  78. return true;
  79. }
  80. };
  81. /// Default filter class that uses the pair filter in combination with a specified layer to filter layers
  82. class DefaultBroadPhaseLayerFilter : public BroadPhaseLayerFilter
  83. {
  84. public:
  85. /// Constructor
  86. DefaultBroadPhaseLayerFilter(const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, ObjectLayer inLayer) :
  87. mObjectVsBroadPhaseLayerFilter(inObjectVsBroadPhaseLayerFilter),
  88. mLayer(inLayer)
  89. {
  90. }
  91. // See BroadPhaseLayerFilter::ShouldCollide
  92. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
  93. {
  94. return mObjectVsBroadPhaseLayerFilter.ShouldCollide(mLayer, inLayer);
  95. }
  96. private:
  97. const ObjectVsBroadPhaseLayerFilter &mObjectVsBroadPhaseLayerFilter;
  98. ObjectLayer mLayer;
  99. };
  100. /// Allows objects from a specific broad phase layer only
  101. class SpecifiedBroadPhaseLayerFilter : public BroadPhaseLayerFilter
  102. {
  103. public:
  104. /// Constructor
  105. explicit SpecifiedBroadPhaseLayerFilter(BroadPhaseLayer inLayer) :
  106. mLayer(inLayer)
  107. {
  108. }
  109. // See BroadPhaseLayerFilter::ShouldCollide
  110. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
  111. {
  112. return mLayer == inLayer;
  113. }
  114. private:
  115. BroadPhaseLayer mLayer;
  116. };
  117. JPH_NAMESPACE_END