CollideSoftBodyVerticesVsTriangles.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/SoftBody/SoftBodyVertex.h>
  6. #include <Jolt/Geometry/ClosestPoint.h>
  7. JPH_NAMESPACE_BEGIN
  8. /// Collision detection helper that collides soft body vertices vs triangles
  9. class JPH_EXPORT CollideSoftBodyVerticesVsTriangles
  10. {
  11. public:
  12. CollideSoftBodyVerticesVsTriangles(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale) :
  13. mTransform(inCenterOfMassTransform * Mat44::sScale(inScale)),
  14. mInvTransform(mTransform.Inversed()),
  15. mNormalSign(ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f)
  16. {
  17. }
  18. JPH_INLINE void StartVertex(const SoftBodyVertex &inVertex)
  19. {
  20. mLocalPosition = mInvTransform * inVertex.mPosition;
  21. mClosestDistanceSq = FLT_MAX;
  22. }
  23. JPH_INLINE void ProcessTriangle(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  24. {
  25. // Get the closest point from the vertex to the triangle
  26. uint32 set;
  27. Vec3 closest_point = ClosestPoint::GetClosestPointOnTriangle(inV0 - mLocalPosition, inV1 - mLocalPosition, inV2 - mLocalPosition, set);
  28. float dist_sq = closest_point.LengthSq();
  29. if (dist_sq < mClosestDistanceSq)
  30. {
  31. mV0 = inV0;
  32. mV1 = inV1;
  33. mV2 = inV2;
  34. mClosestPoint = closest_point;
  35. mClosestDistanceSq = dist_sq;
  36. mSet = set;
  37. }
  38. }
  39. JPH_INLINE void FinishVertex(SoftBodyVertex &ioVertex, int inCollidingShapeIndex) const
  40. {
  41. if (mClosestDistanceSq < FLT_MAX)
  42. {
  43. // Convert triangle to world space
  44. Vec3 v0 = mTransform * mV0;
  45. Vec3 v1 = mTransform * mV1;
  46. Vec3 v2 = mTransform * mV2;
  47. Vec3 triangle_normal = mNormalSign * (v1 - v0).Cross(v2 - v0).NormalizedOr(Vec3::sAxisY());
  48. if (mSet == 0b111)
  49. {
  50. // Closest is interior to the triangle, use plane as collision plane but don't allow more than 0.1 m penetration
  51. // because otherwise a triangle half a level a way will have a huge penetration if it is back facing
  52. float penetration = min(triangle_normal.Dot(v0 - ioVertex.mPosition), 0.1f);
  53. if (penetration > ioVertex.mLargestPenetration)
  54. {
  55. ioVertex.mLargestPenetration = penetration;
  56. ioVertex.mCollisionPlane = Plane::sFromPointAndNormal(v0, triangle_normal);
  57. ioVertex.mCollidingShapeIndex = inCollidingShapeIndex;
  58. }
  59. }
  60. else
  61. {
  62. // Closest point is on an edge or vertex, use closest point as collision plane
  63. Vec3 closest_point = mTransform * (mLocalPosition + mClosestPoint);
  64. Vec3 normal = ioVertex.mPosition - closest_point;
  65. if (normal.Dot(triangle_normal) > 0.0f) // Ignore back facing edges
  66. {
  67. float normal_length = normal.Length();
  68. float penetration = -normal_length;
  69. if (penetration > ioVertex.mLargestPenetration)
  70. {
  71. ioVertex.mLargestPenetration = penetration;
  72. ioVertex.mCollisionPlane = Plane::sFromPointAndNormal(closest_point, normal_length > 0.0f? normal / normal_length : triangle_normal);
  73. ioVertex.mCollidingShapeIndex = inCollidingShapeIndex;
  74. }
  75. }
  76. }
  77. }
  78. }
  79. Mat44 mTransform;
  80. Mat44 mInvTransform;
  81. Vec3 mLocalPosition;
  82. Vec3 mV0, mV1, mV2;
  83. Vec3 mClosestPoint;
  84. float mNormalSign;
  85. float mClosestDistanceSq;
  86. uint32 mSet;
  87. };
  88. JPH_NAMESPACE_END