CastConvexVsTriangles.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/CastConvexVsTriangles.h>
  6. #include <Jolt/Physics/Collision/TransformedShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
  8. #include <Jolt/Physics/Collision/ActiveEdges.h>
  9. #include <Jolt/Physics/Collision/NarrowPhaseStats.h>
  10. #include <Jolt/Geometry/EPAPenetrationDepth.h>
  11. JPH_NAMESPACE_BEGIN
  12. CastConvexVsTriangles::CastConvexVsTriangles(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, CastShapeCollector &ioCollector) :
  13. mShapeCast(inShapeCast),
  14. mShapeCastSettings(inShapeCastSettings),
  15. mCenterOfMassTransform2(inCenterOfMassTransform2),
  16. mScale(inScale),
  17. mSubShapeIDCreator1(inSubShapeIDCreator1),
  18. mCollector(ioCollector)
  19. {
  20. JPH_ASSERT(inShapeCast.mShape->GetType() == EShapeType::Convex);
  21. // Determine if shape is inside out or not
  22. mScaleSign = ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f;
  23. }
  24. void CastConvexVsTriangles::Cast(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, uint8 inActiveEdges, const SubShapeID &inSubShapeID2)
  25. {
  26. // Scale triangle
  27. Vec3 v0 = mScale * inV0;
  28. Vec3 v1 = mScale * inV1;
  29. Vec3 v2 = mScale * inV2;
  30. // Calculate triangle normal
  31. Vec3 triangle_normal = mScaleSign * (v1 - v0).Cross(v2 - v0);
  32. // Backface check
  33. bool back_facing = triangle_normal.Dot(mShapeCast.mDirection) > 0.0f;
  34. if (mShapeCastSettings.mBackFaceModeTriangles == EBackFaceMode::IgnoreBackFaces && back_facing)
  35. return;
  36. // Create triangle support function
  37. TriangleConvexSupport triangle { v0, v1, v2 };
  38. // Check if we already created the cast shape support function
  39. if (mSupport == nullptr)
  40. {
  41. // Determine if we want to use the actual shape or a shrunken shape with convex radius
  42. ConvexShape::ESupportMode support_mode = mShapeCastSettings.mUseShrunkenShapeAndConvexRadius? ConvexShape::ESupportMode::ExcludeConvexRadius : ConvexShape::ESupportMode::Default;
  43. // Create support function
  44. mSupport = static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportFunction(support_mode, mSupportBuffer, mShapeCast.mScale);
  45. }
  46. EPAPenetrationDepth epa;
  47. float fraction = mCollector.GetEarlyOutFraction();
  48. Vec3 contact_point_a, contact_point_b, contact_normal;
  49. if (epa.CastShape(mShapeCast.mCenterOfMassStart, mShapeCast.mDirection, mShapeCastSettings.mCollisionTolerance, mShapeCastSettings.mPenetrationTolerance, *mSupport, triangle, mSupport->GetConvexRadius(), 0.0f, mShapeCastSettings.mReturnDeepestPoint, fraction, contact_point_a, contact_point_b, contact_normal))
  50. {
  51. // Check if we have enabled active edge detection
  52. if (mShapeCastSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive && inActiveEdges != 0b111)
  53. {
  54. // Convert the active edge velocity hint to local space
  55. Vec3 active_edge_movement_direction = mCenterOfMassTransform2.Multiply3x3Transposed(mShapeCastSettings.mActiveEdgeMovementDirection);
  56. // Update the contact normal to account for active edges
  57. // Note that we flip the triangle normal as the penetration axis is pointing towards the triangle instead of away
  58. contact_normal = ActiveEdges::FixNormal(v0, v1, v2, back_facing? triangle_normal : -triangle_normal, inActiveEdges, contact_point_b, contact_normal, active_edge_movement_direction);
  59. }
  60. // Convert to world space
  61. contact_point_a = mCenterOfMassTransform2 * contact_point_a;
  62. contact_point_b = mCenterOfMassTransform2 * contact_point_b;
  63. Vec3 contact_normal_world = mCenterOfMassTransform2.Multiply3x3(contact_normal);
  64. // Its a hit, store the sub shape id's
  65. ShapeCastResult result(fraction, contact_point_a, contact_point_b, contact_normal_world, back_facing, mSubShapeIDCreator1.GetID(), inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));
  66. // Early out if this hit is deeper than the collector's early out value
  67. if (fraction == 0.0f && -result.mPenetrationDepth >= mCollector.GetEarlyOutFraction())
  68. return;
  69. // Gather faces
  70. if (mShapeCastSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
  71. {
  72. // Get supporting face of shape 1
  73. Mat44 transform_1_to_2 = mShapeCast.mCenterOfMassStart;
  74. transform_1_to_2.SetTranslation(transform_1_to_2.GetTranslation() + fraction * mShapeCast.mDirection);
  75. static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportingFace(SubShapeID(), transform_1_to_2.Multiply3x3Transposed(-contact_normal), mShapeCast.mScale, mCenterOfMassTransform2 * transform_1_to_2, result.mShape1Face);
  76. // Get face of the triangle
  77. triangle.GetSupportingFace(contact_normal, result.mShape2Face);
  78. // Convert to world space
  79. for (Vec3 &p : result.mShape2Face)
  80. p = mCenterOfMassTransform2 * p;
  81. }
  82. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
  83. mCollector.AddHit(result);
  84. }
  85. }
  86. JPH_NAMESPACE_END