CollideSphereVsTriangles.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/CollideSphereVsTriangles.h>
  6. #include <Jolt/Physics/Collision/Shape/ScaleHelpers.h>
  7. #include <Jolt/Physics/Collision/CollideShape.h>
  8. #include <Jolt/Physics/Collision/TransformedShape.h>
  9. #include <Jolt/Physics/Collision/ActiveEdges.h>
  10. #include <Jolt/Physics/Collision/NarrowPhaseStats.h>
  11. #include <Jolt/Core/Profiler.h>
  12. JPH_NAMESPACE_BEGIN
  13. static constexpr uint8 sClosestFeatureToActiveEdgesMask[] = {
  14. 0b000, // 0b000: Invalid, guarded by an assert
  15. 0b101, // 0b001: Vertex 1 -> edge 1 or 3
  16. 0b011, // 0b010: Vertex 2 -> edge 1 or 2
  17. 0b001, // 0b011: Vertex 1 & 2 -> edge 1
  18. 0b110, // 0b100: Vertex 3 -> edge 2 or 3
  19. 0b100, // 0b101: Vertex 1 & 3 -> edge 3
  20. 0b010, // 0b110: Vertex 2 & 3 -> edge 2
  21. // 0b111: Vertex 1, 2 & 3 -> interior, guarded by an if
  22. };
  23. CollideSphereVsTriangles::CollideSphereVsTriangles(const SphereShape *inShape1, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeID &inSubShapeID1, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector) :
  24. mCollideShapeSettings(inCollideShapeSettings),
  25. mCollector(ioCollector),
  26. mShape1(inShape1),
  27. mScale2(inScale2),
  28. mTransform2(inCenterOfMassTransform2),
  29. mSubShapeID1(inSubShapeID1)
  30. {
  31. // Calculate the center of the sphere in the space of 2
  32. mSphereCenterIn2 = inCenterOfMassTransform2.Multiply3x3Transposed(inCenterOfMassTransform1.GetTranslation() - inCenterOfMassTransform2.GetTranslation());
  33. // Determine if shape 2 is inside out or not
  34. mScaleSign2 = ScaleHelpers::IsInsideOut(inScale2)? -1.0f : 1.0f;
  35. // Check that the sphere is uniformly scaled
  36. JPH_ASSERT(ScaleHelpers::IsUniformScale(inScale1.Abs()));
  37. mRadius = abs(inScale1.GetX()) * inShape1->GetRadius();
  38. mRadiusPlusMaxSeparationSq = Square(mRadius + inCollideShapeSettings.mMaxSeparationDistance);
  39. }
  40. void CollideSphereVsTriangles::Collide(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, uint8 inActiveEdges, const SubShapeID &inSubShapeID2)
  41. {
  42. // Scale triangle and make it relative to the center of the sphere
  43. Vec3 v0 = mScale2 * inV0 - mSphereCenterIn2;
  44. Vec3 v1 = mScale2 * inV1 - mSphereCenterIn2;
  45. Vec3 v2 = mScale2 * inV2 - mSphereCenterIn2;
  46. // Calculate triangle normal
  47. Vec3 triangle_normal = mScaleSign2 * (v1 - v0).Cross(v2 - v0);
  48. // Backface check
  49. bool back_facing = triangle_normal.Dot(v0) > 0.0f;
  50. if (mCollideShapeSettings.mBackFaceMode == EBackFaceMode::IgnoreBackFaces && back_facing)
  51. return;
  52. // Check if we collide with the sphere
  53. uint32 closest_feature;
  54. Vec3 point2 = ClosestPoint::GetClosestPointOnTriangle(v0, v1, v2, closest_feature);
  55. float point2_len_sq = point2.LengthSq();
  56. if (point2_len_sq > mRadiusPlusMaxSeparationSq)
  57. return;
  58. // Calculate penetration depth
  59. float penetration_depth = mRadius - sqrt(point2_len_sq);
  60. if (-penetration_depth >= mCollector.GetEarlyOutFraction())
  61. return;
  62. // Calculate penetration axis, direction along which to push 2 to move it out of collision (this is always away from the sphere center)
  63. Vec3 penetration_axis = point2.NormalizedOr(Vec3::sAxisY());
  64. // Calculate the point on the sphere
  65. Vec3 point1 = mRadius * penetration_axis;
  66. // Check if we have enabled active edge detection
  67. JPH_ASSERT(closest_feature != 0);
  68. if (mCollideShapeSettings.mActiveEdgeMode == EActiveEdgeMode::CollideOnlyWithActive
  69. && closest_feature != 0b111 // For an interior hit we should already have the right normal
  70. && (inActiveEdges & sClosestFeatureToActiveEdgesMask[closest_feature]) == 0) // If we didn't hit an active edge we should take the triangle normal
  71. {
  72. // Convert the active edge velocity hint to local space
  73. Vec3 active_edge_movement_direction = mTransform2.Multiply3x3Transposed(mCollideShapeSettings.mActiveEdgeMovementDirection);
  74. // See ActiveEdges::FixNormal. If penetration_axis affects the movement less than the triangle normal we keep penetration_axis.
  75. Vec3 new_penetration_axis = back_facing? triangle_normal : -triangle_normal;
  76. if (active_edge_movement_direction.Dot(penetration_axis) * new_penetration_axis.Length() >= active_edge_movement_direction.Dot(new_penetration_axis))
  77. penetration_axis = new_penetration_axis;
  78. }
  79. // Convert to world space
  80. point1 = mTransform2 * (mSphereCenterIn2 + point1);
  81. point2 = mTransform2 * (mSphereCenterIn2 + point2);
  82. Vec3 penetration_axis_world = mTransform2.Multiply3x3(penetration_axis);
  83. // Create collision result
  84. CollideShapeResult result(point1, point2, penetration_axis_world, penetration_depth, mSubShapeID1, inSubShapeID2, TransformedShape::sGetBodyID(mCollector.GetContext()));
  85. // Gather faces
  86. if (mCollideShapeSettings.mCollectFacesMode == ECollectFacesMode::CollectFaces)
  87. {
  88. // The sphere doesn't have a supporting face
  89. // Get face of triangle 2
  90. result.mShape2Face.resize(3);
  91. result.mShape2Face[0] = mTransform2 * (mSphereCenterIn2 + v0);
  92. result.mShape2Face[1] = mTransform2 * (mSphereCenterIn2 + v1);
  93. result.mShape2Face[2] = mTransform2 * (mSphereCenterIn2 + v2);
  94. }
  95. // Notify the collector
  96. JPH_IF_TRACK_NARROWPHASE_STATS(TrackNarrowPhaseCollector track;)
  97. mCollector.AddHit(result);
  98. }
  99. JPH_NAMESPACE_END