CollideConvexVsTriangles.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Collision/CollideConvexVsTriangles.h>
  5. #include <Physics/Collision/Shape/ScaleHelpers.h>
  6. #include <Physics/Collision/CollideShape.h>
  7. #include <Physics/Collision/TransformedShape.h>
  8. #include <Physics/Collision/ActiveEdges.h>
  9. #include <Core/StatCollector.h>
  10. #include <Geometry/EPAPenetrationDepth.h>
  11. #include <Geometry/Plane.h>
  12. namespace JPH {
  13. #ifdef JPH_STAT_COLLECTOR
  14. alignas(JPH_CACHE_LINE_SIZE) atomic<int> CollideConvexVsTriangles::sNumCollideChecks { 0 };
  15. alignas(JPH_CACHE_LINE_SIZE) atomic<int> CollideConvexVsTriangles::sNumGJKChecks { 0 };
  16. alignas(JPH_CACHE_LINE_SIZE) atomic<int> CollideConvexVsTriangles::sNumEPAChecks { 0 };
  17. alignas(JPH_CACHE_LINE_SIZE) atomic<int> CollideConvexVsTriangles::sNumCollisions { 0 };
  18. #endif // JPH_STAT_COLLECTOR
  19. CollideConvexVsTriangles::CollideConvexVsTriangles(const ConvexShape *inShape1, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeID &inSubShapeID1, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector) :
  20. mCollideShapeSettings(inCollideShapeSettings),
  21. mCollector(ioCollector),
  22. mShape1(inShape1),
  23. mScale1(inScale1),
  24. mScale2(inScale2),
  25. mTransform1(inCenterOfMassTransform1),
  26. mSubShapeID1(inSubShapeID1)
  27. {
  28. // Get transforms
  29. Mat44 inverse_transform2 = inCenterOfMassTransform2.InversedRotationTranslation();
  30. Mat44 transform1_to_2 = inverse_transform2 * inCenterOfMassTransform1;
  31. mTransform2To1 = transform1_to_2.InversedRotationTranslation();
  32. // Calculate bounds
  33. mBoundsOf1 = inShape1->GetLocalBounds().Scaled(inScale1);
  34. mBoundsOf1.ExpandBy(Vec3::sReplicate(inCollideShapeSettings.mMaxSeparationDistance));
  35. mBoundsOf1InSpaceOf2 = mBoundsOf1.Transformed(transform1_to_2); // Convert bounding box of 1 into space of 2
  36. // Determine if shape 2 is inside out or not
  37. mScaleSign2 = ScaleHelpers::IsInsideOut(inScale2)? -1.0f : 1.0f;
  38. }
  39. void CollideConvexVsTriangles::Collide(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, uint8 inActiveEdges, const SubShapeID &inSubShapeID2)
  40. {
  41. JPH_PROFILE_FUNCTION();
  42. JPH_IF_STAT_COLLECTOR(sNumCollideChecks++;)
  43. // Scale triangle and transform it to the space of 1
  44. Vec3 v0 = mTransform2To1 * (mScale2 * inV0);
  45. Vec3 v1 = mTransform2To1 * (mScale2 * inV1);
  46. Vec3 v2 = mTransform2To1 * (mScale2 * inV2);
  47. // Calculate triangle normal
  48. Vec3 triangle_normal = mScaleSign2 * (v1 - v0).Cross(v2 - v0);
  49. // Backface check
  50. bool back_facing = triangle_normal.Dot(v0) > 0.0f;
  51. if (mCollideShapeSettings.mBackFaceMode == EBackFaceMode::IgnoreBackFaces && back_facing)
  52. return;
  53. // Get bounding box for triangle
  54. AABox triangle_bbox = AABox::sFromTwoPoints(v0, v1);
  55. triangle_bbox.Encapsulate(v2);
  56. // Get intersection between triangle and shape box, if there is none, we're done
  57. if (!triangle_bbox.Overlaps(mBoundsOf1))
  58. return;
  59. // Create triangle support function
  60. TriangleConvexSupport triangle(v0, v1, v2);
  61. // Perform collision detection
  62. Vec3 penetration_axis = Vec3::sAxisX(), point1, point2;
  63. EPAPenetrationDepth pen_depth;
  64. EPAPenetrationDepth::EStatus status;
  65. JPH_IF_STAT_COLLECTOR(sNumGJKChecks++;)
  66. // Get the support function
  67. if (mShape1ExCvxRadius == nullptr)
  68. mShape1ExCvxRadius = mShape1->GetSupportFunction(ConvexShape::ESupportMode::ExcludeConvexRadius, mBufferExCvxRadius, mScale1);
  69. // Perform GJK step
  70. status = pen_depth.GetPenetrationDepthStepGJK(*mShape1ExCvxRadius, mShape1ExCvxRadius->GetConvexRadius() + mCollideShapeSettings.mMaxSeparationDistance, triangle, 0.0f, mCollideShapeSettings.mCollisionTolerance, penetration_axis, point1, point2);
  71. // Check result of collision detection
  72. if (status == EPAPenetrationDepth::EStatus::NotColliding)
  73. return;
  74. else if (status == EPAPenetrationDepth::EStatus::Indeterminate)
  75. {
  76. // Need to run expensive EPA algorithm
  77. JPH_IF_STAT_COLLECTOR(sNumEPAChecks++;)
  78. // Get the support function
  79. if (mShape1IncCvxRadius == nullptr)
  80. mShape1IncCvxRadius = mShape1->GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, mBufferIncCvxRadius, mScale1);
  81. // Add convex radius
  82. AddConvexRadius<ConvexShape::Support> shape1_add_max_separation_distance(*mShape1IncCvxRadius, mCollideShapeSettings.mMaxSeparationDistance);
  83. // Perform EPA step
  84. if (!pen_depth.GetPenetrationDepthStepEPA(shape1_add_max_separation_distance, triangle, mCollideShapeSettings.mPenetrationTolerance, penetration_axis, point1, point2))
  85. return;
  86. }
  87. // Check if the penetration is bigger than the early out fraction
  88. float penetration_depth = (point2 - point1).Length() - mCollideShapeSettings.mMaxSeparationDistance;
  89. if (-penetration_depth >= mCollector.GetEarlyOutFraction())
  90. return;
  91. // Correct point1 for the added separation distance
  92. float penetration_axis_len = penetration_axis.Length();
  93. if (penetration_axis_len > 0.0f)
  94. point1 -= penetration_axis * (mCollideShapeSettings.mMaxSeparationDistance / penetration_axis_len);
  95. // Check if we have enabled active edge detection
  96. if (mCollideShapeSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive)
  97. {
  98. // Convert the active edge velocity hint to local space
  99. Vec3 active_edge_movement_direction = mTransform1.Multiply3x3Transposed(mCollideShapeSettings.mActiveEdgeMovementDirection);
  100. // Update the penetration axis to account for active edges
  101. // Note that we flip the triangle normal as the penetration axis is pointing towards the triangle instead of away
  102. penetration_axis = ActiveEdges::FixNormal(v0, v1, v2, back_facing? triangle_normal : -triangle_normal, inActiveEdges, point2, penetration_axis, active_edge_movement_direction);
  103. }
  104. // Convert to world space
  105. point1 = mTransform1 * point1;
  106. point2 = mTransform1 * point2;
  107. Vec3 penetration_axis_world = mTransform1.Multiply3x3(penetration_axis);
  108. // Create collision result
  109. CollideShapeResult result(point1, point2, penetration_axis_world, penetration_depth, mSubShapeID1, inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));
  110. // Gather faces
  111. if (mCollideShapeSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
  112. {
  113. // Get supporting face of shape 1
  114. mShape1->GetSupportingFace(-penetration_axis, mScale1, result.mShape1Face);
  115. // Convert to world space
  116. for (Vec3 &p : result.mShape1Face)
  117. p = mTransform1 * p;
  118. // Get face of the triangle
  119. triangle.GetSupportingFace(penetration_axis, result.mShape2Face);
  120. // Convert to world space
  121. for (Vec3 &p : result.mShape2Face)
  122. p = mTransform1 * p;
  123. }
  124. JPH_IF_STAT_COLLECTOR(sNumCollisions++;)
  125. // Notify the collector
  126. mCollector.AddHit(result);
  127. }
  128. #ifdef JPH_STAT_COLLECTOR
  129. void CollideConvexVsTriangles::sResetStats()
  130. {
  131. sNumCollideChecks = 0;
  132. sNumGJKChecks = 0;
  133. sNumEPAChecks = 0;
  134. sNumCollisions = 0;
  135. }
  136. void CollideConvexVsTriangles::sCollectStats()
  137. {
  138. JPH_PROFILE_FUNCTION();
  139. JPH_STAT_COLLECTOR_ADD("ConvexVsTriangles.NumChecks", int(sNumCollideChecks));
  140. JPH_STAT_COLLECTOR_ADD("ConvexVsTriangles.NumCollisions", int(sNumCollisions));
  141. if (sNumCollideChecks > 0)
  142. {
  143. JPH_STAT_COLLECTOR_ADD("ConvexVsTriangles.GJKCheckPercentage", 100.0f * sNumGJKChecks / sNumCollideChecks);
  144. JPH_STAT_COLLECTOR_ADD("ConvexVsTriangles.EPACheckPercentage", 100.0f * sNumEPAChecks / sNumCollideChecks);
  145. }
  146. }
  147. #endif // JPH_STAT_COLLECTOR
  148. } // JPH