ActiveEdges.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Geometry/ClosestPoint.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// An active edge is an edge that either has no neighbouring edge or if the angle between the two connecting faces is too large.
  8. namespace ActiveEdges
  9. {
  10. /// Helper function to check if an edge is active or not
  11. /// @param inNormal1 Triangle normal of triangle on the left side of the edge (when looking along the edge from the top)
  12. /// @param inNormal2 Triangle normal of triangle on the right side of the edge
  13. /// @param inEdgeDirection Vector that points along the edge
  14. inline static bool IsEdgeActive(Vec3Arg inNormal1, Vec3Arg inNormal2, Vec3Arg inEdgeDirection)
  15. {
  16. // If normals are opposite the edges are active (the triangles are back to back)
  17. float cos_angle_normals = inNormal1.Dot(inNormal2);
  18. if (cos_angle_normals < -0.99984769515639123915701155881391f) // cos(179 degrees)
  19. return true;
  20. // Check if concave edge, if so we are not active
  21. if (inNormal1.Cross(inNormal2).Dot(inEdgeDirection) < 0.0f)
  22. return false;
  23. // Convex edge, active when angle bigger than threshold
  24. return cos_angle_normals < 0.99619469809174553229501040247389f; // cos(5 degrees)
  25. }
  26. /// Replace normal by triangle normal if a hit is hitting an inactive edge
  27. /// @param inV0 , inV1 , inV2 form the triangle
  28. /// @param inTriangleNormal is the normal of the provided triangle (does not need to be normalized)
  29. /// @param inActiveEdges bit 0 = edge v0..v1 is active, bit 1 = edge v1..v2 is active, bit 2 = edge v2..v0 is active
  30. /// @param inPoint Collision point on the triangle
  31. /// @param inNormal Collision normal on the triangle (does not need to be normalized)
  32. /// @param inMovementDirection Can be zero. This gives an indication of in which direction the motion is to determine if when we hit an inactive edge/triangle we should return the triangle normal.
  33. /// @return Returns inNormal if an active edge was hit, otherwise returns inTriangleNormal
  34. inline static Vec3 FixNormal(Vec3Arg inV0, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inTriangleNormal, uint8 inActiveEdges, Vec3Arg inPoint, Vec3Arg inNormal, Vec3Arg inMovementDirection)
  35. {
  36. // Check: All of the edges are active, we have the correct normal already. No need to call this function!
  37. JPH_ASSERT(inActiveEdges != 0b111);
  38. // If inNormal would affect movement less than inTriangleNormal use inNormal
  39. // This is done since it is really hard to make a distinction between sliding over a horizontal triangulated grid and hitting an edge (in this case you want to use the triangle normal)
  40. // and sliding over a triangulated grid and grazing a vertical triangle with an inactive edge (in this case using the triangle normal will cause the object to bounce back so we want to use the calculated normal).
  41. // To solve this we take a movement hint to give an indication of what direction our object is moving. If the edge normal results in less motion difference than the triangle normal we use the edge normal.
  42. float normal_length = inNormal.Length();
  43. float triangle_normal_length = inTriangleNormal.Length();
  44. if (inMovementDirection.Dot(inNormal) * triangle_normal_length < inMovementDirection.Dot(inTriangleNormal) * normal_length)
  45. return inNormal;
  46. // Check: None of the edges are active, we need to use the triangle normal
  47. if (inActiveEdges == 0)
  48. return inTriangleNormal;
  49. // Some edges are active.
  50. // If normal is parallel to the triangle normal we don't need to check the active edges.
  51. if (inTriangleNormal.Dot(inNormal) > 0.99619469809174553229501040247389f * normal_length * triangle_normal_length) // cos(5 degrees)
  52. return inNormal;
  53. const float cEpsilon = 1.0e-4f;
  54. const float cOneMinusEpsilon = 1.0f - cEpsilon;
  55. uint colliding_edge;
  56. // Test where the contact point is in the triangle
  57. float u, v, w;
  58. ClosestPoint::GetBaryCentricCoordinates(inV0 - inPoint, inV1 - inPoint, inV2 - inPoint, u, v, w);
  59. if (u > cOneMinusEpsilon)
  60. {
  61. // Colliding with v0, edge 0 or 2 needs to be active
  62. colliding_edge = 0b101;
  63. }
  64. else if (v > cOneMinusEpsilon)
  65. {
  66. // Colliding with v1, edge 0 or 1 needs to be active
  67. colliding_edge = 0b011;
  68. }
  69. else if (w > cOneMinusEpsilon)
  70. {
  71. // Colliding with v2, edge 1 or 2 needs to be active
  72. colliding_edge = 0b110;
  73. }
  74. else if (u < cEpsilon)
  75. {
  76. // Colliding with edge v1, v2, edge 1 needs to be active
  77. colliding_edge = 0b010;
  78. }
  79. else if (v < cEpsilon)
  80. {
  81. // Colliding with edge v0, v2, edge 2 needs to be active
  82. colliding_edge = 0b100;
  83. }
  84. else if (w < cEpsilon)
  85. {
  86. // Colliding with edge v0, v1, edge 0 needs to be active
  87. colliding_edge = 0b001;
  88. }
  89. else
  90. {
  91. // Interior hit
  92. return inTriangleNormal;
  93. }
  94. // If this edge is active, use the provided normal instead of the triangle normal
  95. return (inActiveEdges & colliding_edge) != 0? inNormal : inTriangleNormal;
  96. }
  97. }
  98. JPH_NAMESPACE_END