BodyFilter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/Body/BodyID.h>
  7. JPH_NAMESPACE_BEGIN
  8. class Body;
  9. /// Class function to filter out bodies, returns true if test should collide with body
  10. class JPH_EXPORT BodyFilter : public NonCopyable
  11. {
  12. public:
  13. /// Destructor
  14. virtual ~BodyFilter() = default;
  15. /// Filter function. Returns true if we should collide with inBodyID
  16. virtual bool ShouldCollide([[maybe_unused]] const BodyID &inBodyID) const
  17. {
  18. return true;
  19. }
  20. /// Filter function. Returns true if we should collide with inBody (this is called after the body is locked and makes it possible to filter based on body members)
  21. virtual bool ShouldCollideLocked([[maybe_unused]] const Body &inBody) const
  22. {
  23. return true;
  24. }
  25. };
  26. /// A simple body filter implementation that ignores a single, specified body
  27. class JPH_EXPORT IgnoreSingleBodyFilter : public BodyFilter
  28. {
  29. public:
  30. /// Constructor, pass the body you want to ignore
  31. explicit IgnoreSingleBodyFilter(const BodyID &inBodyID) :
  32. mBodyID(inBodyID)
  33. {
  34. }
  35. /// Filter function. Returns true if we should collide with inBodyID
  36. virtual bool ShouldCollide(const BodyID &inBodyID) const override
  37. {
  38. return mBodyID != inBodyID;
  39. }
  40. private:
  41. BodyID mBodyID;
  42. };
  43. /// A simple body filter implementation that ignores multiple, specified bodies
  44. class JPH_EXPORT IgnoreMultipleBodiesFilter : public BodyFilter
  45. {
  46. public:
  47. /// Remove all bodies from the filter
  48. void Clear()
  49. {
  50. mBodyIDs.clear();
  51. }
  52. /// Reserve space for inSize body ID's
  53. void Reserve(uint inSize)
  54. {
  55. mBodyIDs.reserve(inSize);
  56. }
  57. /// Add a body to be ignored
  58. void IgnoreBody(const BodyID &inBodyID)
  59. {
  60. mBodyIDs.push_back(inBodyID);
  61. }
  62. /// Filter function. Returns true if we should collide with inBodyID
  63. virtual bool ShouldCollide(const BodyID &inBodyID) const override
  64. {
  65. return std::find(mBodyIDs.begin(), mBodyIDs.end(), inBodyID) == mBodyIDs.end();
  66. }
  67. private:
  68. Array<BodyID> mBodyIDs;
  69. };
  70. /// Ignores a single body and chains the filter to another filter
  71. class JPH_EXPORT IgnoreSingleBodyFilterChained : public BodyFilter
  72. {
  73. public:
  74. /// Constructor
  75. explicit IgnoreSingleBodyFilterChained(const BodyID inBodyID, const BodyFilter &inFilter) :
  76. mBodyID(inBodyID),
  77. mFilter(inFilter)
  78. {
  79. }
  80. /// Filter function. Returns true if we should collide with inBodyID
  81. virtual bool ShouldCollide(const BodyID &inBodyID) const override
  82. {
  83. return inBodyID != mBodyID && mFilter.ShouldCollide(inBodyID);
  84. }
  85. /// Filter function. Returns true if we should collide with inBody (this is called after the body is locked and makes it possible to filter based on body members)
  86. virtual bool ShouldCollideLocked(const Body &inBody) const override
  87. {
  88. return mFilter.ShouldCollideLocked(inBody);
  89. }
  90. private:
  91. BodyID mBodyID;
  92. const BodyFilter & mFilter;
  93. };
  94. #ifdef JPH_DEBUG_RENDERER
  95. /// Class function to filter out bodies for debug rendering, returns true if body should be rendered
  96. class JPH_EXPORT BodyDrawFilter : public NonCopyable
  97. {
  98. public:
  99. /// Destructor
  100. virtual ~BodyDrawFilter() = default;
  101. /// Filter function. Returns true if inBody should be rendered
  102. virtual bool ShouldDraw([[maybe_unused]] const Body& inBody) const
  103. {
  104. return true;
  105. }
  106. };
  107. #endif // JPH_DEBUG_RENDERER
  108. JPH_NAMESPACE_END