BroadPhaseLayer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Core/NonCopyable.h>
  5. #include <Physics/Collision/ObjectLayer.h>
  6. namespace JPH {
  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 &inRHS) : mValue(inRHS.mValue) { }
  19. JPH_INLINE BroadPhaseLayer & operator = (const BroadPhaseLayer &inRHS)
  20. {
  21. mValue = inRHS.mValue;
  22. return *this;
  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 constexpr bool operator < (const BroadPhaseLayer &inRHS) const
  33. {
  34. return mValue < inRHS.mValue;
  35. }
  36. JPH_INLINE explicit constexpr operator Type() const
  37. {
  38. return mValue;
  39. }
  40. private:
  41. Type mValue;
  42. };
  43. /// Constant value used to indicate an invalid broad phase layer
  44. static constexpr BroadPhaseLayer cBroadPhaseLayerInvalid(0xff);
  45. /// An array whose length corresponds to the max amount of object layers that should be supported.
  46. /// To map these to a broadphase layer you'd do vector[BroadPhaseLayer]. The broadphase layers should be tightly
  47. /// packed, i.e. the lowest value should be 0 and the amount of sub structures that are created in the broadphase is max(inObjectToBroadPhaseLayer).
  48. using ObjectToBroadPhaseLayer = vector<BroadPhaseLayer>;
  49. /// Function to test if an object can collide with a broadphase layer. Used while finding collision pairs.
  50. using ObjectVsBroadPhaseLayerFilter = bool (*)(ObjectLayer inLayer1, BroadPhaseLayer inLayer2);
  51. /// Function to convert a broadphase layer to a string for debugging purposes
  52. using BroadPhaseLayerToString = const char * (*)(BroadPhaseLayer inLayer);
  53. /// Filter class for broadphase layers
  54. class BroadPhaseLayerFilter : public NonCopyable
  55. {
  56. public:
  57. /// Destructor
  58. virtual ~BroadPhaseLayerFilter() = default;
  59. /// Function to filter out broadphase layers when doing collision query test (return true to allow testing against objects with this layer)
  60. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const
  61. {
  62. return true;
  63. }
  64. };
  65. /// Default filter class that uses the pair filter in combination with a specified layer to filter layers
  66. class DefaultBroadPhaseLayerFilter : public BroadPhaseLayerFilter
  67. {
  68. public:
  69. /// Constructor
  70. DefaultBroadPhaseLayerFilter(ObjectVsBroadPhaseLayerFilter inObjectVsBroadPhaseLayerFilter, ObjectLayer inLayer) :
  71. mObjectVsBroadPhaseLayerFilter(inObjectVsBroadPhaseLayerFilter),
  72. mLayer(inLayer)
  73. {
  74. }
  75. /// Copy constructor
  76. DefaultBroadPhaseLayerFilter(const DefaultBroadPhaseLayerFilter &inRHS) :
  77. mObjectVsBroadPhaseLayerFilter(inRHS.mObjectVsBroadPhaseLayerFilter),
  78. mLayer(inRHS.mLayer)
  79. {
  80. }
  81. // See BroadPhaseLayerFilter::ShouldCollide
  82. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
  83. {
  84. return mObjectVsBroadPhaseLayerFilter(mLayer, inLayer);
  85. }
  86. private:
  87. ObjectVsBroadPhaseLayerFilter mObjectVsBroadPhaseLayerFilter;
  88. ObjectLayer mLayer;
  89. };
  90. /// Allows objects from a specific broad phase layer only
  91. class SpecifiedBroadPhaseLayerFilter : public BroadPhaseLayerFilter
  92. {
  93. public:
  94. /// Constructor
  95. explicit SpecifiedBroadPhaseLayerFilter(BroadPhaseLayer inLayer) :
  96. mLayer(inLayer)
  97. {
  98. }
  99. // See BroadPhaseLayerFilter::ShouldCollide
  100. virtual bool ShouldCollide(BroadPhaseLayer inLayer) const override
  101. {
  102. return mLayer == inLayer;
  103. }
  104. private:
  105. BroadPhaseLayer mLayer;
  106. };
  107. } // JPH