CastConvexVsTriangles.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. JPH_PROFILE_FUNCTION();
  27. // Scale triangle
  28. Vec3 v0 = mScale * inV0;
  29. Vec3 v1 = mScale * inV1;
  30. Vec3 v2 = mScale * inV2;
  31. // Calculate triangle normal
  32. Vec3 triangle_normal = mScaleSign * (v1 - v0).Cross(v2 - v0);
  33. // Backface check
  34. bool back_facing = triangle_normal.Dot(mShapeCast.mDirection) > 0.0f;
  35. if (mShapeCastSettings.mBackFaceModeTriangles == EBackFaceMode::IgnoreBackFaces && back_facing)
  36. return;
  37. // Create triangle support function
  38. TriangleConvexSupport triangle { v0, v1, v2 };
  39. // Check if we already created the cast shape support function
  40. if (mSupport == nullptr)
  41. {
  42. // Determine if we want to use the actual shape or a shrunken shape with convex radius
  43. ConvexShape::ESupportMode support_mode = mShapeCastSettings.mUseShrunkenShapeAndConvexRadius? ConvexShape::ESupportMode::ExcludeConvexRadius : ConvexShape::ESupportMode::IncludeConvexRadius;
  44. // Create support function
  45. mSupport = static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportFunction(support_mode, mSupportBuffer, mShapeCast.mScale);
  46. }
  47. EPAPenetrationDepth epa;
  48. float fraction = mCollector.GetEarlyOutFraction();
  49. Vec3 contact_point_a, contact_point_b, contact_normal;
  50. 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))
  51. {
  52. // Check if we have enabled active edge detection
  53. if (mShapeCastSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive && inActiveEdges != 0b111)
  54. {
  55. // Convert the active edge velocity hint to local space
  56. Vec3 active_edge_movement_direction = mCenterOfMassTransform2.Multiply3x3Transposed(mShapeCastSettings.mActiveEdgeMovementDirection);
  57. // Update the contact normal to account for active edges
  58. // Note that we flip the triangle normal as the penetration axis is pointing towards the triangle instead of away
  59. contact_normal = ActiveEdges::FixNormal(v0, v1, v2, back_facing? triangle_normal : -triangle_normal, inActiveEdges, contact_point_b, contact_normal, active_edge_movement_direction);
  60. }
  61. // Convert to world space
  62. contact_point_a = mCenterOfMassTransform2 * contact_point_a;
  63. contact_point_b = mCenterOfMassTransform2 * contact_point_b;
  64. Vec3 contact_normal_world = mCenterOfMassTransform2.Multiply3x3(contact_normal);
  65. // Its a hit, store the sub shape id's
  66. ShapeCastResult result(fraction, contact_point_a, contact_point_b, contact_normal_world, back_facing, mSubShapeIDCreator1.GetID(), inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));
  67. // Gather faces
  68. if (mShapeCastSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
  69. {
  70. // Get supporting face of shape 1
  71. Mat44 transform_1_to_2 = mShapeCast.mCenterOfMassStart;
  72. transform_1_to_2.SetTranslation(transform_1_to_2.GetTranslation() + fraction * mShapeCast.mDirection);
  73. static_cast<const ConvexShape *>(mShapeCast.mShape)->GetSupportingFace(SubShapeID(), transform_1_to_2.Multiply3x3Transposed(-contact_normal), mShapeCast.mScale, mCenterOfMassTransform2 * transform_1_to_2, result.mShape1Face);
  74. // Get face of the triangle
  75. triangle.GetSupportingFace(contact_normal, result.mShape2Face);
  76. // Convert to world space
  77. for (Vec3 &p : result.mShape2Face)
  78. p = mCenterOfMassTransform2 * p;
  79. }
  80. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
  81. mCollector.AddHit(result);
  82. }
  83. }
  84. JPH_NAMESPACE_END