ObjectLayer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. JPH_NAMESPACE_BEGIN
  7. /// Layer that objects can be in, determines which other objects it can collide with
  8. #ifndef JPH_OBJECT_LAYER_BITS
  9. #define JPH_OBJECT_LAYER_BITS 16
  10. #endif // JPH_OBJECT_LAYER_BITS
  11. #if JPH_OBJECT_LAYER_BITS == 16
  12. using ObjectLayer = uint16;
  13. #elif JPH_OBJECT_LAYER_BITS == 32
  14. using ObjectLayer = uint32;
  15. #else
  16. #error "JPH_OBJECT_LAYER_BITS must be 16 or 32"
  17. #endif
  18. /// Constant value used to indicate an invalid object layer
  19. static constexpr ObjectLayer cObjectLayerInvalid = ObjectLayer(~ObjectLayer(0U));
  20. /// Filter class for object layers
  21. class ObjectLayerFilter : public NonCopyable
  22. {
  23. public:
  24. /// Destructor
  25. virtual ~ObjectLayerFilter() = default;
  26. /// Function to filter out object layers when doing collision query test (return true to allow testing against objects with this layer)
  27. virtual bool ShouldCollide([[maybe_unused]] ObjectLayer inLayer) const
  28. {
  29. return true;
  30. }
  31. #ifdef JPH_TRACK_BROADPHASE_STATS
  32. /// Get a string that describes this filter for stat tracking purposes
  33. virtual String GetDescription() const
  34. {
  35. return "No Description";
  36. }
  37. #endif // JPH_TRACK_BROADPHASE_STATS
  38. };
  39. /// Filter class to test if two objects can collide based on their object layer. Used while finding collision pairs.
  40. class ObjectLayerPairFilter : public NonCopyable
  41. {
  42. public:
  43. /// Destructor
  44. virtual ~ObjectLayerPairFilter() = default;
  45. /// Returns true if two layers can collide
  46. virtual bool ShouldCollide([[maybe_unused]] ObjectLayer inLayer1, [[maybe_unused]] ObjectLayer inLayer2) const
  47. {
  48. return true;
  49. }
  50. };
  51. /// Default filter class that uses the pair filter in combination with a specified layer to filter layers
  52. class DefaultObjectLayerFilter : public ObjectLayerFilter
  53. {
  54. public:
  55. /// Constructor
  56. DefaultObjectLayerFilter(const ObjectLayerPairFilter &inObjectLayerPairFilter, ObjectLayer inLayer) :
  57. mObjectLayerPairFilter(inObjectLayerPairFilter),
  58. mLayer(inLayer)
  59. {
  60. }
  61. /// Copy constructor
  62. DefaultObjectLayerFilter(const DefaultObjectLayerFilter &inRHS) :
  63. mObjectLayerPairFilter(inRHS.mObjectLayerPairFilter),
  64. mLayer(inRHS.mLayer)
  65. {
  66. }
  67. // See ObjectLayerFilter::ShouldCollide
  68. virtual bool ShouldCollide(ObjectLayer inLayer) const override
  69. {
  70. return mObjectLayerPairFilter.ShouldCollide(mLayer, inLayer);
  71. }
  72. private:
  73. const ObjectLayerPairFilter & mObjectLayerPairFilter;
  74. ObjectLayer mLayer;
  75. };
  76. /// Allows objects from a specific layer only
  77. class SpecifiedObjectLayerFilter : public ObjectLayerFilter
  78. {
  79. public:
  80. /// Constructor
  81. explicit SpecifiedObjectLayerFilter(ObjectLayer inLayer) :
  82. mLayer(inLayer)
  83. {
  84. }
  85. // See ObjectLayerFilter::ShouldCollide
  86. virtual bool ShouldCollide(ObjectLayer inLayer) const override
  87. {
  88. return mLayer == inLayer;
  89. }
  90. private:
  91. ObjectLayer mLayer;
  92. };
  93. JPH_NAMESPACE_END