ShapeCast.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Geometry/AABox.h>
  5. #include <Jolt/Physics/Collision/CollideShape.h>
  6. #include <Jolt/Physics/Collision/Shape/Shape.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Structure that holds a single shape cast (a shape moving along a linear path in 3d space with no rotation)
  9. struct ShapeCast
  10. {
  11. JPH_OVERRIDE_NEW_DELETE
  12. /// Constructor
  13. ShapeCast(const Shape *inShape, Vec3Arg inScale, Mat44Arg inCenterOfMassStart, Vec3Arg inDirection, const AABox &inWorldSpaceBounds) :
  14. mShape(inShape),
  15. mScale(inScale),
  16. mCenterOfMassStart(inCenterOfMassStart),
  17. mDirection(inDirection),
  18. mShapeWorldBounds(inWorldSpaceBounds)
  19. {
  20. }
  21. /// Constructor
  22. ShapeCast(const Shape *inShape, Vec3Arg inScale, Mat44Arg inCenterOfMassStart, Vec3Arg inDirection) :
  23. ShapeCast(inShape, inScale, inCenterOfMassStart, inDirection, inShape->GetWorldSpaceBounds(inCenterOfMassStart, inScale))
  24. {
  25. }
  26. /// Construct a shape cast using a world transform for a shape instead of a center of mass transform
  27. static inline ShapeCast sFromWorldTransform(const Shape *inShape, Vec3Arg inScale, Mat44Arg inWorldTransform, Vec3Arg inDirection)
  28. {
  29. return ShapeCast(inShape, inScale, inWorldTransform.PreTranslated(inShape->GetCenterOfMass()), inDirection);
  30. }
  31. /// Transform this shape cast using inTransform. Multiply transform on the left left hand side.
  32. ShapeCast PostTransformed(Mat44Arg inTransform) const
  33. {
  34. Mat44 start = inTransform * mCenterOfMassStart;
  35. Vec3 direction = inTransform.Multiply3x3(mDirection);
  36. return { mShape, mScale, start, direction };
  37. }
  38. const Shape * mShape; ///< Shape that's being cast (cannot be mesh shape). Note that this structure does not assume ownership over the shape for performance reasons.
  39. const Vec3 mScale; ///< Scale in local space of the shape being cast
  40. const Mat44 mCenterOfMassStart; ///< Start position and orientation of the center of mass of the shape (construct using sFromWorldTransform if you have a world transform for your shape)
  41. const Vec3 mDirection; ///< Direction and length of the cast (anything beyond this length will not be reported as a hit)
  42. const AABox mShapeWorldBounds; ///< Cached shape's world bounds, calculated in constructor
  43. };
  44. /// Settings to be passed with a shape cast
  45. class ShapeCastSettings : public CollideSettingsBase
  46. {
  47. public:
  48. JPH_OVERRIDE_NEW_DELETE
  49. /// How backfacing triangles should be treated (should we report moving out of a triangle?)
  50. EBackFaceMode mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  51. /// How backfacing convex objects should be treated (should we report starting inside an object and moving out?)
  52. EBackFaceMode mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  53. /// Indicates if we want to shrink the shape by the convex radius and then expand it again. This speeds up collision detection and gives a more accurate normal at the cost of a more 'rounded' shape.
  54. bool mUseShrunkenShapeAndConvexRadius = false;
  55. /// When true, and the shape is intersecting at the beginning of the cast (fraction = 0) then this will calculate the deepest penetration point (costing additional CPU time)
  56. bool mReturnDeepestPoint = false;
  57. };
  58. /// Result of a shape cast test
  59. class ShapeCastResult : public CollideShapeResult
  60. {
  61. public:
  62. JPH_OVERRIDE_NEW_DELETE
  63. /// Default constructor
  64. ShapeCastResult() = default;
  65. /// Constructor
  66. /// @param inFraction Fraction at which the cast hit
  67. /// @param inContactPoint1 Contact point on shape 1
  68. /// @param inContactPoint2 Contact point on shape 2
  69. /// @param inContactNormalOrPenetrationDepth Contact normal pointing from shape 1 to 2 or penetration depth vector when the objects are penetrating (also from 1 to 2)
  70. /// @param inBackFaceHit If this hit was a back face hit
  71. /// @param inSubShapeID1 Sub shape id for shape 1
  72. /// @param inSubShapeID2 Sub shape id for shape 2
  73. /// @param inBodyID2 BodyID that was hit
  74. ShapeCastResult(float inFraction, Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inContactNormalOrPenetrationDepth, bool inBackFaceHit, const SubShapeID &inSubShapeID1, const SubShapeID &inSubShapeID2, const BodyID &inBodyID2) :
  75. CollideShapeResult(inContactPoint1, inContactPoint2, inContactNormalOrPenetrationDepth, (inContactPoint2 - inContactPoint1).Length(), inSubShapeID1, inSubShapeID2, inBodyID2),
  76. mFraction(inFraction),
  77. mIsBackFaceHit(inBackFaceHit)
  78. {
  79. }
  80. /// Function required by the CollisionCollector. A smaller fraction is considered to be a 'better hit'. For rays/cast shapes we can just use the collision fraction. The fraction and penetration depth are combined in such a way that deeper hits at fraction 0 go first.
  81. inline float GetEarlyOutFraction() const { return mFraction > 0.0f? mFraction : -mPenetrationDepth; }
  82. /// Reverses the hit result, swapping contact point 1 with contact point 2 etc.
  83. /// @param inWorldSpaceCastDirection Direction of the shape cast in world space
  84. ShapeCastResult Reversed(Vec3Arg inWorldSpaceCastDirection) const
  85. {
  86. // Calculate by how much to shift the contact points
  87. Vec3 delta = mFraction * inWorldSpaceCastDirection;
  88. ShapeCastResult result;
  89. result.mContactPointOn2 = mContactPointOn1 - delta;
  90. result.mContactPointOn1 = mContactPointOn2 - delta;
  91. result.mPenetrationAxis = -mPenetrationAxis;
  92. result.mPenetrationDepth = mPenetrationDepth;
  93. result.mSubShapeID2 = mSubShapeID1;
  94. result.mSubShapeID1 = mSubShapeID2;
  95. result.mBodyID2 = mBodyID2;
  96. result.mFraction = mFraction;
  97. result.mIsBackFaceHit = mIsBackFaceHit;
  98. result.mShape2Face.resize(mShape1Face.size());
  99. for (Face::size_type i = 0; i < mShape1Face.size(); ++i)
  100. result.mShape2Face[i] = mShape1Face[i] - delta;
  101. result.mShape1Face.resize(mShape2Face.size());
  102. for (Face::size_type i = 0; i < mShape2Face.size(); ++i)
  103. result.mShape1Face[i] = mShape2Face[i] - delta;
  104. return result;
  105. }
  106. float mFraction; ///< This is the fraction where the shape hit the other shape: CenterOfMassOnHit = Start + value * (End - Start)
  107. bool mIsBackFaceHit; ///< True if the shape was hit from the back side
  108. };
  109. JPH_NAMESPACE_END