SimShapeFilterWrapper.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Collision/SimShapeFilter.h>
  6. #include <Jolt/Physics/Body/Body.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Helper class to forward ShapeFilter calls to a SimShapeFilter
  9. /// INTERNAL CLASS DO NOT USE!
  10. class SimShapeFilterWrapper : private ShapeFilter
  11. {
  12. public:
  13. /// Constructor
  14. SimShapeFilterWrapper(const SimShapeFilter *inFilter, const Body *inBody1) :
  15. mFilter(inFilter),
  16. mBody1(inBody1)
  17. {
  18. // Fall back to an empty filter if no simulation shape filter is set, this reduces the virtual call to 'return true'
  19. mFinalFilter = inFilter != nullptr? this : &mDefault;
  20. }
  21. /// Forward to the simulation shape filter
  22. virtual bool ShouldCollide(const Shape *inShape1, const SubShapeID &inSubShapeIDOfShape1, const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  23. {
  24. return mFilter->ShouldCollide(*mBody1, inShape1, inSubShapeIDOfShape1, *mBody2, inShape2, inSubShapeIDOfShape2);
  25. }
  26. /// Forward to the simulation shape filter
  27. virtual bool ShouldCollide(const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  28. {
  29. return mFilter->ShouldCollide(*mBody1, mBody1->GetShape(), SubShapeID(), *mBody2, inShape2, inSubShapeIDOfShape2);
  30. }
  31. /// Set the body we're colliding against
  32. void SetBody2(const Body *inBody2)
  33. {
  34. mBody2 = inBody2;
  35. }
  36. /// Returns the actual filter to use for collision detection
  37. const ShapeFilter & GetFilter() const
  38. {
  39. return *mFinalFilter;
  40. }
  41. private:
  42. const ShapeFilter * mFinalFilter;
  43. const SimShapeFilter * mFilter;
  44. const Body * mBody1;
  45. const Body * mBody2 = nullptr;
  46. const ShapeFilter mDefault;
  47. };
  48. JPH_NAMESPACE_END