ShapeFilter.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /// Used during NarrowPhase queries and TransformedShape queries. Set to the body ID of inShape2 before calling ShouldCollide.
  37. /// Provides context to the filter to indicate which body is colliding.
  38. mutable BodyID mBodyID2;
  39. };
  40. /// Helper class to reverse the order of the shapes in the ShouldCollide function
  41. class ReversedShapeFilter : public ShapeFilter
  42. {
  43. public:
  44. /// Constructor
  45. explicit ReversedShapeFilter(const ShapeFilter &inFilter) : mFilter(inFilter)
  46. {
  47. mBodyID2 = inFilter.mBodyID2;
  48. }
  49. virtual bool ShouldCollide(const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  50. {
  51. return mFilter.ShouldCollide(inShape2, inSubShapeIDOfShape2);
  52. }
  53. virtual bool ShouldCollide(const Shape *inShape1, const SubShapeID &inSubShapeIDOfShape1, const Shape *inShape2, const SubShapeID &inSubShapeIDOfShape2) const override
  54. {
  55. return mFilter.ShouldCollide(inShape2, inSubShapeIDOfShape2, inShape1, inSubShapeIDOfShape1);
  56. }
  57. private:
  58. const ShapeFilter & mFilter;
  59. };
  60. JPH_NAMESPACE_END