NarrowPhaseQuery.h 5.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Body/BodyFilter.h>
  5. #include <Jolt/Physics/Body/BodyLock.h>
  6. #include <Jolt/Physics/Body/BodyLockInterface.h>
  7. #include <Jolt/Physics/Collision/ShapeFilter.h>
  8. #include <Jolt/Physics/Collision/BroadPhase/BroadPhase.h>
  9. #include <Jolt/Physics/Collision/BackFaceMode.h>
  10. JPH_NAMESPACE_BEGIN
  11. class Shape;
  12. class CollideShapeSettings;
  13. class RayCastResult;
  14. /// Class that provides an interface for doing precise collision detection against the broad and then the narrow phase
  15. class NarrowPhaseQuery : public NonCopyable
  16. {
  17. public:
  18. /// Initialize the interface (should only be called by PhysicsSystem)
  19. void Init(BodyLockInterface &inBodyLockInterface, BroadPhase &inBroadPhase) { mBodyLockInterface = &inBodyLockInterface; mBroadPhase = &inBroadPhase; }
  20. /// Cast a ray and find the closest hit. Returns true if it finds a hit. Hits further than ioHit.mFraction will not be considered and in this case ioHit will remain unmodified (and the function will return false).
  21. /// Convex objects will be treated as solid (meaning if the ray starts inside, you'll get a hit fraction of 0) and back face hits against triangles are returned.
  22. /// If you want the surface normal of the hit use Body::GetWorldSpaceSurfaceNormal(ioHit.mSubShapeID2, inRay.GetPointOnRay(ioHit.mFraction)) on body with ID ioHit.mBodyID.
  23. bool CastRay(const RRayCast &inRay, RayCastResult &ioHit, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter = { }, const ObjectLayerFilter &inObjectLayerFilter = { }, const BodyFilter &inBodyFilter = { }) const;
  24. /// Cast a ray, allows collecting multiple hits. Note that this version is more flexible but also slightly slower than the CastRay function that returns only a single hit.
  25. /// If you want the surface normal of the hit use Body::GetWorldSpaceSurfaceNormal(collected sub shape ID, inRay.GetPointOnRay(collected fraction)) on body with collected body ID.
  26. void CastRay(const RRayCast &inRay, const RayCastSettings &inRayCastSettings, CastRayCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter = { }, const ObjectLayerFilter &inObjectLayerFilter = { }, const BodyFilter &inBodyFilter = { }, const ShapeFilter &inShapeFilter = { }) const;
  27. /// Check if inPoint is inside any shapes. For this tests all shapes are treated as if they were solid.
  28. /// For a mesh shape, this test will only provide sensible information if the mesh is a closed manifold.
  29. /// For each shape that collides, ioCollector will receive a hit
  30. void CollidePoint(RVec3Arg inPoint, CollidePointCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter = { }, const ObjectLayerFilter &inObjectLayerFilter = { }, const BodyFilter &inBodyFilter = { }, const ShapeFilter &inShapeFilter = { }) const;
  31. /// Collide a shape with the system
  32. /// @param inShape Shape to test
  33. /// @param inShapeScale Scale in local space of shape
  34. /// @param inCenterOfMassTransform Center of mass transform for the shape
  35. /// @param inCollideShapeSettings Settings
  36. /// @param inBaseOffset All hit results will be returned relative to this offset, can be zero to get results in world position, but when you're testing far from the origin you get better precision by picking a position that's closer e.g. inCenterOfMassTransform.GetTranslation() since floats are most accurate near the origin
  37. /// @param ioCollector Collector that receives the hits
  38. /// @param inBroadPhaseLayerFilter Filter that filters at broadphase level
  39. /// @param inObjectLayerFilter Filter that filters at layer level
  40. /// @param inBodyFilter Filter that filters at body level
  41. /// @param inShapeFilter Filter that filters at shape level
  42. void CollideShape(const Shape *inShape, Vec3Arg inShapeScale, RMat44Arg inCenterOfMassTransform, const CollideShapeSettings &inCollideShapeSettings, RVec3Arg inBaseOffset, CollideShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter = { }, const ObjectLayerFilter &inObjectLayerFilter = { }, const BodyFilter &inBodyFilter = { }, const ShapeFilter &inShapeFilter = { }) const;
  43. /// Cast a shape and report any hits to ioCollector
  44. /// @param inShapeCast The shape cast and its position and direction
  45. /// @param inShapeCastSettings Settings for the shape cast
  46. /// @param inBaseOffset All hit results will be returned relative to this offset, can be zero to get results in world position, but when you're testing far from the origin you get better precision by picking a position that's closer e.g. inShapeCast.mCenterOfMassStart.GetTranslation() since floats are most accurate near the origin
  47. /// @param ioCollector Collector that receives the hits
  48. /// @param inBroadPhaseLayerFilter Filter that filters at broadphase level
  49. /// @param inObjectLayerFilter Filter that filters at layer level
  50. /// @param inBodyFilter Filter that filters at body level
  51. /// @param inShapeFilter Filter that filters at shape level
  52. void CastShape(const RShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, RVec3Arg inBaseOffset, CastShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter = { }, const ObjectLayerFilter &inObjectLayerFilter = { }, const BodyFilter &inBodyFilter = { }, const ShapeFilter &inShapeFilter = { }) const;
  53. /// Collect all leaf transformed shapes that fall inside world space box inBox
  54. void CollectTransformedShapes(const AABox &inBox, TransformedShapeCollector &ioCollector, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter = { }, const ObjectLayerFilter &inObjectLayerFilter = { }, const BodyFilter &inBodyFilter = { }, const ShapeFilter &inShapeFilter = { }) const;
  55. private:
  56. BodyLockInterface * mBodyLockInterface = nullptr;
  57. BroadPhase * mBroadPhase = nullptr;
  58. };
  59. JPH_NAMESPACE_END