CollideSoftBodyVerticesVsTriangles.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/Collision/CollideSoftBodyVertexIterator.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),
  14. mInvTransform(mTransform.InversedRotationTranslation()),
  15. mScale(inScale),
  16. mNormalSign(ScaleHelpers::IsInsideOut(inScale)? -1.0f : 1.0f)
  17. {
  18. }
  19. JPH_INLINE void StartVertex(const CollideSoftBodyVertexIterator &inVertex)
  20. {
  21. mLocalPosition = mInvTransform * inVertex.GetPosition();
  22. mClosestDistanceSq = FLT_MAX;
  23. }
  24. JPH_INLINE void ProcessTriangle(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2)
  25. {
  26. // Apply the scale to the triangle
  27. Vec3 v0 = mScale * inV0;
  28. Vec3 v1 = mScale * inV1;
  29. Vec3 v2 = mScale * inV2;
  30. // Get the closest point from the vertex to the triangle
  31. uint32 set;
  32. Vec3 closest_point = ClosestPoint::GetClosestPointOnTriangle(v0 - mLocalPosition, v1 - mLocalPosition, v2 - mLocalPosition, set);
  33. float dist_sq = closest_point.LengthSq();
  34. if (dist_sq < mClosestDistanceSq)
  35. {
  36. mV0 = v0;
  37. mV1 = v1;
  38. mV2 = v2;
  39. mClosestPoint = closest_point;
  40. mClosestDistanceSq = dist_sq;
  41. mSet = set;
  42. }
  43. }
  44. JPH_INLINE void FinishVertex(const CollideSoftBodyVertexIterator &ioVertex, int inCollidingShapeIndex) const
  45. {
  46. if (mClosestDistanceSq < FLT_MAX)
  47. {
  48. // Convert triangle to world space
  49. Vec3 v0 = mTransform * mV0;
  50. Vec3 v1 = mTransform * mV1;
  51. Vec3 v2 = mTransform * mV2;
  52. Vec3 triangle_normal = mNormalSign * (v1 - v0).Cross(v2 - v0).NormalizedOr(Vec3::sAxisY());
  53. if (mSet == 0b111)
  54. {
  55. // Closest is interior to the triangle, use plane as collision plane but don't allow more than sTriangleThickness penetration
  56. // because otherwise a triangle half a level a way will have a huge penetration if it is back facing
  57. float penetration = triangle_normal.Dot(v0 - ioVertex.GetPosition());
  58. if (penetration < sTriangleThickness && ioVertex.UpdatePenetration(penetration))
  59. ioVertex.SetCollision(Plane::sFromPointAndNormal(v0, triangle_normal), inCollidingShapeIndex);
  60. }
  61. else
  62. {
  63. // Closest point is on an edge or vertex, use closest point as collision plane
  64. Vec3 closest_point = mTransform * (mLocalPosition + mClosestPoint);
  65. Vec3 normal = ioVertex.GetPosition() - closest_point;
  66. if (normal.Dot(triangle_normal) > 0.0f) // Ignore back facing edges
  67. {
  68. float normal_length = normal.Length();
  69. float penetration = -normal_length;
  70. if (ioVertex.UpdatePenetration(penetration))
  71. ioVertex.SetCollision(Plane::sFromPointAndNormal(closest_point, normal_length > 0.0f? normal / normal_length : triangle_normal), inCollidingShapeIndex);
  72. }
  73. }
  74. }
  75. }
  76. /// Triangles are considered to have some thickness. This thickness extends backwards along the negative triangle normal.
  77. /// Make this value smaller than the smallest 'wall thickness' so that the back side of the triangle doesn't protrude through the other side.
  78. /// Make this value too small and tunneling is more likely to occur.
  79. static inline float sTriangleThickness = 0.1f;
  80. Mat44 mTransform;
  81. Mat44 mInvTransform;
  82. Vec3 mScale;
  83. Vec3 mLocalPosition;
  84. Vec3 mV0, mV1, mV2;
  85. Vec3 mClosestPoint;
  86. float mNormalSign;
  87. float mClosestDistanceSq;
  88. uint32 mSet;
  89. };
  90. JPH_NAMESPACE_END