BodyFilter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/NonCopyable.h>
  5. #include <Jolt/Physics/Body/BodyID.h>
  6. JPH_NAMESPACE_BEGIN
  7. class Body;
  8. /// Class function to filter out bodies, returns true if test should collide with body
  9. class BodyFilter : public NonCopyable
  10. {
  11. public:
  12. /// Destructor
  13. virtual ~BodyFilter() = default;
  14. /// Filter function. Returns true if we should collide with inBodyID
  15. virtual bool ShouldCollide(const BodyID &inBodyID) const
  16. {
  17. return true;
  18. }
  19. /// 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)
  20. virtual bool ShouldCollideLocked(const Body &inBody) const
  21. {
  22. return true;
  23. }
  24. };
  25. /// A simple body filter implementation that ignores a single, specified body
  26. class IgnoreSingleBodyFilter : public BodyFilter
  27. {
  28. public:
  29. /// Constructor, pass the body you want to ignore
  30. explicit IgnoreSingleBodyFilter(const BodyID &inBodyID) :
  31. mBodyID(inBodyID)
  32. {
  33. }
  34. /// Filter function. Returns true if we should collide with inBodyID
  35. virtual bool ShouldCollide(const BodyID &inBodyID) const override
  36. {
  37. return mBodyID != inBodyID;
  38. }
  39. private:
  40. BodyID mBodyID;
  41. };
  42. /// A simple body filter implementation that ignores multiple, specified bodies
  43. class IgnoreMultipleBodiesFilter : public BodyFilter
  44. {
  45. public:
  46. /// Remove all bodies from the filter
  47. void Clear()
  48. {
  49. mBodyIDs.clear();
  50. }
  51. /// Reserve space for inSize body ID's
  52. void Reserve(uint inSize)
  53. {
  54. mBodyIDs.reserve(inSize);
  55. }
  56. /// Add a body to be ignored
  57. void IgnoreBody(const BodyID &inBodyID)
  58. {
  59. mBodyIDs.push_back(inBodyID);
  60. }
  61. /// Filter function. Returns true if we should collide with inBodyID
  62. virtual bool ShouldCollide(const BodyID &inBodyID) const override
  63. {
  64. return find(mBodyIDs.begin(), mBodyIDs.end(), inBodyID) == mBodyIDs.end();
  65. }
  66. private:
  67. Array<BodyID> mBodyIDs;
  68. };
  69. #ifdef JPH_DEBUG_RENDERER
  70. /// Class function to filter out bodies for debug rendering, returns true if body should be rendered
  71. class BodyDrawFilter : public NonCopyable
  72. {
  73. public:
  74. /// Destructor
  75. virtual ~BodyDrawFilter() = default;
  76. /// Filter function. Returns true if inBody should be rendered
  77. virtual bool ShouldDraw(const Body& inBody) const
  78. {
  79. return true;
  80. }
  81. };
  82. #endif // JPH_DEBUG_RENDERER
  83. JPH_NAMESPACE_END