CollisionDispatch.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Collision/Shape/Shape.h>
  5. #include <Physics/Collision/Shape/SubShapeID.h>
  6. #include <Physics/Collision/ShapeCast.h>
  7. #include <Physics/Collision/ShapeFilter.h>
  8. #include <Physics/Collision/NarrowPhaseStats.h>
  9. namespace JPH {
  10. class CollideShapeSettings;
  11. /// Dispatch function, main function to handle collisions between shapes
  12. class CollisionDispatch
  13. {
  14. public:
  15. /// Collide 2 shapes and pass any collision on to ioCollector
  16. /// @param inShape1 The first shape
  17. /// @param inShape2 The second shape
  18. /// @param inScale1 Local space scale of shape 1
  19. /// @param inScale2 Local space scale of shape 2
  20. /// @param inCenterOfMassTransform1 Transform to transform center of mass of shape 1 into world space
  21. /// @param inCenterOfMassTransform2 Transform to transform center of mass of shape 2 into world space
  22. /// @param inSubShapeIDCreator1 Class that tracks the current sub shape ID for shape 1
  23. /// @param inSubShapeIDCreator2 Class that tracks the current sub shape ID for shape 2
  24. /// @param inCollideShapeSettings Options for the CollideShape test
  25. /// @param ioCollector The collector that receives the results.
  26. static inline void sCollideShapeVsShape(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector)
  27. {
  28. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseStat track(NarrowPhaseStat::sCollideShape[(int)inShape1->GetSubType()][(int)inShape2->GetSubType()]);)
  29. sCollideShape[(int)inShape1->GetSubType()][(int)inShape2->GetSubType()](inShape1, inShape2, inScale1, inScale2, inCenterOfMassTransform1, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, inCollideShapeSettings, ioCollector);
  30. }
  31. /// Cast a shape againt this shape, passes any hits found to ioCollector.
  32. /// Note that the shape cast should be relative to the center of mass of this shape (i.e. inShapeCast.mCenterOfMassStart = Start * Mat44::sTranslation(mShape->GetCenterOfMass()) if you want to cast the shape in the space it was created).
  33. /// @param inShapeCast The shape to cast against the other shape and its start and direction
  34. /// @param inShapeCastSettings Settings for performing the cast
  35. /// @param inShape The shape to cast against.
  36. /// @param inScale Local space scale for the shape to cast against.
  37. /// @param inShapeFilter allows selectively disabling collisions between pairs of (sub) shapes.
  38. /// @param inCenterOfMassTransform2 Is the center of mass transform of shape 2 (excluding scale), this is used to provide a transform to the shape cast result so that local quantities can be transformed into world space.
  39. /// @param inSubShapeIDCreator1 Class that tracks the current sub shape ID for the casting shape
  40. /// @param inSubShapeIDCreator2 Class that tracks the current sub shape ID for the shape we're casting against
  41. /// @param ioCollector The collector that receives the results.
  42. static inline void sCastShapeVsShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector)
  43. {
  44. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseStat track(NarrowPhaseStat::sCastShape[(int)inShapeCast.mShape->GetSubType()][(int)inShape->GetSubType()]);)
  45. // Only test shape if it passes the shape filter
  46. if (inShapeFilter.ShouldCollide(inSubShapeIDCreator1.GetID(), inSubShapeIDCreator2.GetID()))
  47. sCastShape[(int)inShapeCast.mShape->GetSubType()][(int)inShape->GetSubType()](inShapeCast, inShapeCastSettings, inShape, inScale, inShapeFilter, inCenterOfMassTransform2, inSubShapeIDCreator1, inSubShapeIDCreator2, ioCollector);
  48. }
  49. /// Function that collides 2 shapes (see sCollideShapeVsShape)
  50. using CollideShape = void (*)(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector);
  51. /// Function that casts a shape vs another shape (see sCastShapeVsShape)
  52. using CastShape = void (*)(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  53. /// Register a collide shape function in the collision table
  54. static void sRegisterCollideShape(EShapeSubType inType1, EShapeSubType inType2, CollideShape inFunction) { sCollideShape[(int)inType1][(int)inType2] = inFunction; }
  55. /// Register a cast shape function in the collision table
  56. static void sRegisterCastShape(EShapeSubType inType1, EShapeSubType inType2, CastShape inFunction) { sCastShape[(int)inType1][(int)inType2] = inFunction; }
  57. private:
  58. static CollideShape sCollideShape[NumSubShapeTypes][NumSubShapeTypes];
  59. static CastShape sCastShape[NumSubShapeTypes][NumSubShapeTypes];
  60. };
  61. } // JPH