ShapeFilter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/Physics/Body/BodyID.h>
  6. #include <Jolt/Core/NonCopyable.h>
  7. JPH_NAMESPACE_BEGIN
  8. class Shape;
  9. class SubShapeID;
  10. /// Filter class
  11. class ShapeFilter : public NonCopyable
  12. {
  13. public:
  14. /// Destructor
  15. virtual ~ShapeFilter() = default;
  16. /// Filter function to determine if we should collide with a shape. Returns true if the filter passes.
  17. /// This overload is called when the query doesn't have a source shape (e.g. ray cast / collide point)
  18. /// @param inShape2 Shape we're colliding against
  19. /// @param inSubShapeIDOfShape2 The sub shape ID that will lead from the root shape to inShape2 (i.e. the shape of mBodyID2)
  20. virtual bool ShouldCollide([[maybe_unused]] const Shape *inShape2, [[maybe_unused]] const SubShapeID &inSubShapeIDOfShape2) const
  21. {
  22. return true;
  23. }
  24. /// Filter function to determine if two shapes should collide. Returns true if the filter passes.
  25. /// This overload is called when querying a shape vs a shape (e.g. collide object / cast object).
  26. /// It is called at each level of the shape hierarchy, so if you have a compound shape with a box, this function will be called twice.
  27. /// It will not be called on triangles that are part of another shape, i.e a mesh shape will not trigger a callback per triangle. You can filter out individual triangles in the CollisionCollector::AddHit function by their sub shape ID.
  28. /// @param inShape1 1st shape that is colliding
  29. /// @param inSubShapeIDOfShape1 The sub shape ID that will lead from the root shape to inShape1 (i.e. the shape that is used to collide or cast against shape 2)
  30. /// @param inShape2 2nd shape that is colliding
  31. /// @param inSubShapeIDOfShape2 The sub shape ID that will lead from the root shape to inShape2 (i.e. the shape of mBodyID2)
  32. virtual bool ShouldCollide([[maybe_unused]] const Shape *inShape1, [[maybe_unused]] const SubShapeID &inSubShapeIDOfShape1, [[maybe_unused]] const Shape *inShape2, [[maybe_unused]] const SubShapeID &inSubShapeIDOfShape2) const
  33. {
  34. return true;
  35. }
  36. /// Set by the collision detection functions to the body ID of the body that we're colliding against before calling the ShouldCollide function
  37. mutable BodyID mBodyID2;
  38. };
  39. /// Helper class to reverse the order of the shapes in the ShouldCollide function
  40. class ReversedShapeFilter : public ShapeFilter
  41. {
  42. public:
  43. /// Constructor
  44. explicit ReversedShapeFilter(const ShapeFilter &inFilter) : mFilter(inFilter)
  45. {
  46. mBodyID2 = inFilter.mBodyID2;
  47. }
  48. virtual bool ShouldCollide(const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  49. {
  50. return mFilter.ShouldCollide(inShape2, inSubShapeIDOfShape2);
  51. }
  52. virtual bool ShouldCollide(const Shape *inShape1, const SubShapeID &inSubShapeIDOfShape1, const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  53. {
  54. return mFilter.ShouldCollide(inShape2, inSubShapeIDOfShape2, inShape1, inSubShapeIDOfShape1);
  55. }
  56. private:
  57. const ShapeFilter & mFilter;
  58. };
  59. JPH_NAMESPACE_END