BodyFilter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 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(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(const Body &inBody) const
  22. {
  23. return true;
  24. }
  25. };
  26. /// A simple body filter implementation that ignores a single, specified body
  27. class 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 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 find(mBodyIDs.begin(), mBodyIDs.end(), inBodyID) == mBodyIDs.end();
  66. }
  67. private:
  68. Array<BodyID> mBodyIDs;
  69. };
  70. #ifdef JPH_DEBUG_RENDERER
  71. /// Class function to filter out bodies for debug rendering, returns true if body should be rendered
  72. class BodyDrawFilter : public NonCopyable
  73. {
  74. public:
  75. /// Destructor
  76. virtual ~BodyDrawFilter() = default;
  77. /// Filter function. Returns true if inBody should be rendered
  78. virtual bool ShouldDraw(const Body& inBody) const
  79. {
  80. return true;
  81. }
  82. };
  83. #endif // JPH_DEBUG_RENDERER
  84. JPH_NAMESPACE_END