BroadPhaseLayer.h 4.2 KB

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