ConvexVsTrianglesTest.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  6. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  7. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  8. #include <Jolt/Physics/Collision/CollideShape.h>
  9. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  10. #include <Jolt/Physics/Collision/CollideConvexVsTriangles.h>
  11. #include <Jolt/Physics/Collision/CollideSphereVsTriangles.h>
  12. #include "Layers.h"
  13. #include "PhysicsTestContext.h"
  14. TEST_SUITE("ConvexVsTrianglesTest")
  15. {
  16. static constexpr float cEdgeLength = 4.0f;
  17. template <class Collider>
  18. static void sCheckCollisionNoHit(const CollideShapeSettings &inSettings, Vec3Arg inCenter, float inRadius, uint8 inActiveEdges)
  19. {
  20. // Our sphere
  21. Ref<SphereShape> sphere = new SphereShape(inRadius);
  22. // Our default triangle
  23. Vec3 v1(0, 0, 0);
  24. Vec3 v2(0, 0, cEdgeLength);
  25. Vec3 v3(cEdgeLength, 0, 0);
  26. {
  27. // Collide sphere
  28. AllHitCollisionCollector<CollideShapeCollector> collector;
  29. Collider collider(sphere, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), Mat44::sTranslation(inCenter), Mat44::sIdentity(), SubShapeID(), inSettings, collector);
  30. collider.Collide(v1, v2, v3, inActiveEdges, SubShapeID());
  31. CHECK(!collector.HadHit());
  32. }
  33. // A triangle shape has all edges active, so only test if all edges are active
  34. if (inActiveEdges == 0b111)
  35. {
  36. // Create the triangle shape
  37. PhysicsTestContext context;
  38. context.CreateBody(new TriangleShapeSettings(v1, v2, v3), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  39. // Collide sphere
  40. AllHitCollisionCollector<CollideShapeCollector> collector;
  41. context.GetSystem()->GetNarrowPhaseQuery().CollideShape(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(inCenter), inSettings, collector);
  42. CHECK(!collector.HadHit());
  43. }
  44. // A mesh shape with a single triangle has all edges active, so only test if all edges are active
  45. if (inActiveEdges == 0b111)
  46. {
  47. // Create a mesh with a single triangle
  48. TriangleList triangles;
  49. triangles.push_back(Triangle(v1, v2, v3));
  50. PhysicsTestContext context;
  51. context.CreateBody(new MeshShapeSettings(triangles), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  52. // Collide sphere
  53. AllHitCollisionCollector<CollideShapeCollector> collector;
  54. context.GetSystem()->GetNarrowPhaseQuery().CollideShape(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(inCenter), inSettings, collector);
  55. CHECK(!collector.HadHit());
  56. }
  57. }
  58. template <class Collider>
  59. static void sCheckCollision(const CollideShapeSettings &inSettings, Vec3Arg inCenter, float inRadius, uint8 inActiveEdges, Vec3Arg inExpectedContactOn1, Vec3Arg inExpectedContactOn2, Vec3Arg inExpectedPenetrationAxis, float inExpectedPenetrationDepth)
  60. {
  61. // Our sphere
  62. Ref<SphereShape> sphere = new SphereShape(inRadius);
  63. // Our default triangle
  64. Vec3 v1(0, 0, 0);
  65. Vec3 v2(0, 0, cEdgeLength);
  66. Vec3 v3(cEdgeLength, 0, 0);
  67. // A semi random transform for the triangle
  68. Vec3 translation = Vec3(1, 2, 3);
  69. Quat rotation = Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI);
  70. Mat44 transform = Mat44::sRotationTranslation(rotation, translation);
  71. Mat44 inv_transform = transform.InversedRotationTranslation();
  72. // The transform for the sphere
  73. Mat44 sphere_transform = transform * Mat44::sTranslation(inCenter);
  74. // Transform incoming settings
  75. CollideShapeSettings settings = inSettings;
  76. settings.mActiveEdgeMovementDirection = transform.Multiply3x3(inSettings.mActiveEdgeMovementDirection);
  77. // Test the specified collider
  78. {
  79. SubShapeID sub_shape_id1, sub_shape_id2;
  80. sub_shape_id1.SetValue(123);
  81. sub_shape_id2.SetValue(456);
  82. // Collide sphere
  83. AllHitCollisionCollector<CollideShapeCollector> collector;
  84. Collider collider(sphere, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), sphere_transform, transform, sub_shape_id1, settings, collector);
  85. collider.Collide(v1, v2, v3, inActiveEdges, sub_shape_id2);
  86. // Test result
  87. CHECK(collector.mHits.size() == 1);
  88. const CollideShapeResult &hit = collector.mHits[0];
  89. CHECK(hit.mBodyID2 == BodyID());
  90. CHECK(hit.mSubShapeID1.GetValue() == sub_shape_id1.GetValue());
  91. CHECK(hit.mSubShapeID2.GetValue() == sub_shape_id2.GetValue());
  92. Vec3 contact1 = inv_transform * hit.mContactPointOn1;
  93. Vec3 contact2 = inv_transform * hit.mContactPointOn2;
  94. Vec3 pen_axis = transform.Multiply3x3Transposed(hit.mPenetrationAxis).Normalized();
  95. Vec3 expected_pen_axis = inExpectedPenetrationAxis.Normalized();
  96. CHECK_APPROX_EQUAL(contact1, inExpectedContactOn1, 1.0e-4f);
  97. CHECK_APPROX_EQUAL(contact2, inExpectedContactOn2, 1.0e-4f);
  98. CHECK_APPROX_EQUAL(pen_axis, expected_pen_axis, 1.0e-4f);
  99. CHECK_APPROX_EQUAL(hit.mPenetrationDepth, inExpectedPenetrationDepth, 1.0e-4f);
  100. }
  101. // A triangle shape has all edges active, so only test if all edges are active
  102. if (inActiveEdges == 0b111)
  103. {
  104. // Create the triangle shape
  105. PhysicsTestContext context;
  106. Body &body = context.CreateBody(new TriangleShapeSettings(v1, v2, v3), translation, rotation, EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  107. // Collide sphere
  108. AllHitCollisionCollector<CollideShapeCollector> collector;
  109. context.GetSystem()->GetNarrowPhaseQuery().CollideShape(sphere, Vec3::sReplicate(1.0f), sphere_transform, settings, collector);
  110. // Test result
  111. CHECK(collector.mHits.size() == 1);
  112. const CollideShapeResult &hit = collector.mHits[0];
  113. CHECK(hit.mBodyID2 == body.GetID());
  114. CHECK(hit.mSubShapeID1.GetValue() == SubShapeID().GetValue());
  115. CHECK(hit.mSubShapeID2.GetValue() == SubShapeID().GetValue());
  116. Vec3 contact1 = inv_transform * hit.mContactPointOn1;
  117. Vec3 contact2 = inv_transform * hit.mContactPointOn2;
  118. Vec3 pen_axis = transform.Multiply3x3Transposed(hit.mPenetrationAxis).Normalized();
  119. Vec3 expected_pen_axis = inExpectedPenetrationAxis.Normalized();
  120. CHECK_APPROX_EQUAL(contact1, inExpectedContactOn1, 1.0e-4f);
  121. CHECK_APPROX_EQUAL(contact2, inExpectedContactOn2, 1.0e-4f);
  122. CHECK_APPROX_EQUAL(pen_axis, expected_pen_axis, 1.0e-4f);
  123. CHECK_APPROX_EQUAL(hit.mPenetrationDepth, inExpectedPenetrationDepth, 1.0e-4f);
  124. }
  125. // A mesh shape with a single triangle has all edges active, so only test if all edges are active
  126. if (inActiveEdges == 0b111)
  127. {
  128. // Create a mesh with a single triangle
  129. TriangleList triangles;
  130. triangles.push_back(Triangle(v1, v2, v3));
  131. PhysicsTestContext context;
  132. Body &body = context.CreateBody(new MeshShapeSettings(triangles), translation, rotation, EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  133. // Collide sphere
  134. AllHitCollisionCollector<CollideShapeCollector> collector;
  135. context.GetSystem()->GetNarrowPhaseQuery().CollideShape(sphere, Vec3::sReplicate(1.0f), sphere_transform, settings, collector);
  136. // Test result
  137. CHECK(collector.mHits.size() == 1);
  138. const CollideShapeResult &hit = collector.mHits[0];
  139. CHECK(hit.mBodyID2 == body.GetID());
  140. CHECK(hit.mSubShapeID1.GetValue() == SubShapeID().GetValue());
  141. CHECK(hit.mSubShapeID2.GetValue() != SubShapeID().GetValue()); // We don't really know what SubShapeID a triangle in the mesh will get, but it should not be invalid
  142. Vec3 contact1 = inv_transform * hit.mContactPointOn1;
  143. Vec3 contact2 = inv_transform * hit.mContactPointOn2;
  144. Vec3 pen_axis = transform.Multiply3x3Transposed(hit.mPenetrationAxis).Normalized();
  145. Vec3 expected_pen_axis = inExpectedPenetrationAxis.Normalized();
  146. CHECK_APPROX_EQUAL(contact1, inExpectedContactOn1, 1.0e-4f);
  147. CHECK_APPROX_EQUAL(contact2, inExpectedContactOn2, 1.0e-4f);
  148. CHECK_APPROX_EQUAL(pen_axis, expected_pen_axis, 1.0e-4f);
  149. CHECK_APPROX_EQUAL(hit.mPenetrationDepth, inExpectedPenetrationDepth, 1.0e-4f);
  150. }
  151. }
  152. // Compares CollideShapeResult for two spheres with given positions and radii
  153. template <class Collider>
  154. static void sTestConvexVsTriangles()
  155. {
  156. const float cRadius = 0.5f;
  157. const float cRadiusRS2 = cRadius / sqrt(2.0f);
  158. const float cDistanceToTriangle = 0.1f;
  159. const float cDistanceToTriangleRS2 = cDistanceToTriangle / sqrt(2.0f);
  160. const float cEpsilon = 1.0e-6f; // A small epsilon to ensure we hit the front side
  161. const float cMaxSeparationDistance = 0.5f;
  162. const float cSeparationDistance = 0.1f;
  163. // Loop over all possible active edge combinations
  164. for (uint8 active_edges = 0; active_edges <= 0b111; ++active_edges)
  165. {
  166. // Create settings
  167. CollideShapeSettings settings;
  168. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  169. // Settings with ignore back faces
  170. CollideShapeSettings settings_no_bf;
  171. settings_no_bf.mBackFaceMode = EBackFaceMode::IgnoreBackFaces;
  172. // Settings with max seperation distance
  173. CollideShapeSettings settings_max_distance;
  174. settings_max_distance.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  175. settings_max_distance.mMaxSeparationDistance = cMaxSeparationDistance;
  176. {
  177. // There should be no hit in front of the triangle
  178. Vec3 sphere_center(0.25f * cEdgeLength, cRadius + cSeparationDistance, 0.25f * cEdgeLength);
  179. sCheckCollisionNoHit<Collider>(settings, sphere_center, cRadius, active_edges);
  180. // But if there's a max separation distance there should be
  181. Vec3 expected1 = sphere_center + Vec3(0, -cRadius, 0);
  182. Vec3 expected2(0.25f * cEdgeLength, 0, 0.25f * cEdgeLength);
  183. Vec3 pen_axis(0, -1, 0);
  184. float pen_depth = -cSeparationDistance;
  185. sCheckCollision<Collider>(settings_max_distance, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  186. }
  187. {
  188. // But if we go beyond the separation distance we should again have no hit
  189. Vec3 sphere_center(0.25f * cEdgeLength, cRadius + cMaxSeparationDistance + cSeparationDistance, 0.25f * cEdgeLength);
  190. sCheckCollisionNoHit<Collider>(settings_max_distance, sphere_center, cRadius, active_edges);
  191. }
  192. {
  193. // There should be no hit in behind the triangle
  194. Vec3 sphere_center(0.25f * cEdgeLength, -cRadius - cSeparationDistance, 0.25f * cEdgeLength);
  195. sCheckCollisionNoHit<Collider>(settings, sphere_center, cRadius, active_edges);
  196. // But if there's a max separation distance there should be
  197. Vec3 expected1 = sphere_center + Vec3(0, cRadius, 0);
  198. Vec3 expected2(0.25f * cEdgeLength, 0, 0.25f * cEdgeLength);
  199. Vec3 pen_axis(0, 1, 0);
  200. float pen_depth = -cSeparationDistance;
  201. sCheckCollision<Collider>(settings_max_distance, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  202. }
  203. {
  204. // But if we go beyond the separation distance we should again have no hit
  205. Vec3 sphere_center(0.25f * cEdgeLength, -cRadius - cMaxSeparationDistance - cSeparationDistance, 0.25f * cEdgeLength);
  206. sCheckCollisionNoHit<Collider>(settings_max_distance, sphere_center, cRadius, active_edges);
  207. }
  208. {
  209. // Hit interior from front side
  210. Vec3 expected2(0.25f * cEdgeLength, 0, 0.25f * cEdgeLength);
  211. Vec3 sphere_center = expected2 + Vec3(0, cDistanceToTriangle, 0);
  212. Vec3 expected1 = sphere_center + Vec3(0, -cRadius, 0);
  213. Vec3 pen_axis(0, -1, 0);
  214. float pen_depth = cRadius - cDistanceToTriangle;
  215. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  216. // Ignore back faces should not matter
  217. sCheckCollision<Collider>(settings_no_bf, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  218. }
  219. {
  220. // Hit interior from back side
  221. Vec3 expected2(0.25f * cEdgeLength, 0, 0.25f * cEdgeLength);
  222. Vec3 sphere_center = expected2 + Vec3(0, -cDistanceToTriangle, 0);
  223. Vec3 expected1 = sphere_center + Vec3(0, cRadius, 0);
  224. Vec3 pen_axis(0, 1, 0);
  225. float pen_depth = cRadius - cDistanceToTriangle;
  226. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  227. // Back face hit should be filtered
  228. sCheckCollisionNoHit<Collider>(settings_no_bf, sphere_center, cRadius, active_edges);
  229. }
  230. // Loop over possibel active edge movement direction permutations
  231. for (int movement_direction = 0; movement_direction < 3; ++movement_direction)
  232. {
  233. switch (movement_direction)
  234. {
  235. case 0:
  236. // Disable the system
  237. settings.mActiveEdgeMovementDirection = Vec3::sZero();
  238. break;
  239. case 1:
  240. // Move into the triangle, this should always give us the normal from the edge
  241. settings.mActiveEdgeMovementDirection = Vec3(0, -1, 0);
  242. break;
  243. case 2:
  244. // Move out of the triangle, we should always get the normal of the triangle
  245. settings.mActiveEdgeMovementDirection = Vec3(0, 1, 0);
  246. break;
  247. }
  248. {
  249. // Hit edge 1
  250. Vec3 expected2(0, 0, 0.5f * cEdgeLength);
  251. Vec3 sphere_center = expected2 + Vec3(-cDistanceToTriangle, cEpsilon, 0);
  252. Vec3 expected1 = sphere_center + Vec3(cRadius, 0, 0);
  253. Vec3 pen_axis = (active_edges & 0b001) != 0 || movement_direction == 1? Vec3(1, 0, 0) : Vec3(0, -1, 0);
  254. float pen_depth = cRadius - cDistanceToTriangle;
  255. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  256. }
  257. {
  258. // Hit edge 2
  259. Vec3 expected2(0.5f * cEdgeLength, 0, 0.5f * cEdgeLength);
  260. Vec3 sphere_center = expected2 + Vec3(cDistanceToTriangleRS2, cEpsilon, cDistanceToTriangleRS2);
  261. Vec3 expected1 = sphere_center - Vec3(cRadiusRS2, 0, cRadiusRS2);
  262. Vec3 pen_axis = (active_edges & 0b010) != 0 || movement_direction == 1? Vec3(-1, 0, -1) : Vec3(0, -1, 0);
  263. float pen_depth = cRadius - cDistanceToTriangle;
  264. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  265. }
  266. {
  267. // Hit edge 3
  268. Vec3 expected2(0.5f * cEdgeLength, 0, 0);
  269. Vec3 sphere_center = expected2 + Vec3(0, cEpsilon, -cDistanceToTriangle);
  270. Vec3 expected1 = sphere_center + Vec3(0, 0, cRadius);
  271. Vec3 pen_axis = (active_edges & 0b100) != 0 || movement_direction == 1? Vec3(0, 0, 1) : Vec3(0, -1, 0);
  272. float pen_depth = cRadius - cDistanceToTriangle;
  273. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  274. }
  275. {
  276. // Hit vertex 1
  277. Vec3 expected2(0, 0, 0);
  278. Vec3 sphere_center = expected2 + Vec3(-cDistanceToTriangleRS2, cEpsilon, -cDistanceToTriangleRS2);
  279. Vec3 expected1 = sphere_center + Vec3(cRadiusRS2, 0, cRadiusRS2);
  280. Vec3 pen_axis = (active_edges & 0b101) != 0 || movement_direction == 1? Vec3(1, 0, 1) : Vec3(0, -1, 0);
  281. float pen_depth = cRadius - cDistanceToTriangle;
  282. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  283. }
  284. {
  285. // Hit vertex 2
  286. Vec3 expected2(0, 0, cEdgeLength);
  287. Vec3 sphere_center = expected2 + Vec3(-cDistanceToTriangleRS2, cEpsilon, cDistanceToTriangleRS2);
  288. Vec3 expected1 = sphere_center + Vec3(cRadiusRS2, 0, -cRadiusRS2);
  289. Vec3 pen_axis = (active_edges & 0b011) != 0 || movement_direction == 1? Vec3(1, 0, -1) : Vec3(0, -1, 0);
  290. float pen_depth = cRadius - cDistanceToTriangle;
  291. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  292. }
  293. {
  294. // Hit vertex 3
  295. Vec3 expected2(cEdgeLength, 0, 0);
  296. Vec3 sphere_center = expected2 + Vec3(cDistanceToTriangleRS2, cEpsilon, -cDistanceToTriangleRS2);
  297. Vec3 expected1 = sphere_center + Vec3(-cRadiusRS2, 0, cRadiusRS2);
  298. Vec3 pen_axis = (active_edges & 0b110) != 0 || movement_direction == 1? Vec3(-1, 0, 1) : Vec3(0, -1, 0);
  299. float pen_depth = cRadius - cDistanceToTriangle;
  300. sCheckCollision<Collider>(settings, sphere_center, cRadius, active_edges, expected1, expected2, pen_axis, pen_depth);
  301. }
  302. }
  303. }
  304. }
  305. TEST_CASE("TestConvexVsTriangles")
  306. {
  307. sTestConvexVsTriangles<CollideConvexVsTriangles>();
  308. }
  309. TEST_CASE("TestSphereVsTriangles")
  310. {
  311. sTestConvexVsTriangles<CollideSphereVsTriangles>();
  312. }
  313. }