ActiveEdgesTests.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include "Layers.h"
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  8. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  9. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  10. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  11. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  12. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  13. #include <Jolt/Physics/Collision/CollideShape.h>
  14. #include <Jolt/Physics/Collision/ShapeCast.h>
  15. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  16. TEST_SUITE("ActiveEdgesTest")
  17. {
  18. static const float cCapsuleProbeOffset = 0.1f; // How much to offset the probe from y = 0 in order to avoid hitting a back instead of a front face
  19. static const float cCapsuleRadius = 0.1f;
  20. // Create a capsule as our probe
  21. static Ref<Shape> sCreateProbeCapsule()
  22. {
  23. // Ensure capsule is long enough so that when active edges mode is on, we will always get a horizontal penetration axis rather than a vertical one
  24. CapsuleShapeSettings capsule(1.0f, cCapsuleRadius);
  25. capsule.SetEmbedded();
  26. return capsule.Create().Get();
  27. }
  28. // Create a flat mesh shape consting of 7 x 7 quads, we know that only the outer edges of this shape are active
  29. static Ref<ShapeSettings> sCreateMeshShape()
  30. {
  31. TriangleList triangles;
  32. for (int z = 0; z < 7; ++z)
  33. for (int x = 0; x < 7; ++x)
  34. {
  35. float fx = (float)x - 3.5f, fz = (float)z - 3.5f;
  36. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx, 0, fz + 1), Vec3(fx + 1, 0, fz + 1)));
  37. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx + 1, 0, fz + 1), Vec3(fx + 1, 0, fz)));
  38. }
  39. return new MeshShapeSettings(triangles);
  40. }
  41. // Create a flat height field shape that has the same properties as the mesh shape
  42. static Ref<ShapeSettings> sCreateHeightFieldShape()
  43. {
  44. float samples[8*8];
  45. memset(samples, 0, sizeof(samples));
  46. return new HeightFieldShapeSettings(samples, Vec3(-3.5f, 0, -3.5f), Vec3::sReplicate(1.0f), 8);
  47. }
  48. // This struct indicates what we hope to find as hit
  49. struct ExpectedHit
  50. {
  51. Vec3 mPosition;
  52. Vec3 mPenetrationAxis;
  53. };
  54. // Compare expected hits with returned hits
  55. template <class ResultType>
  56. static void sCheckMatch(const Array<ResultType> &inResult, const Array<ExpectedHit> &inExpectedHits, float inAccuracySq)
  57. {
  58. CHECK(inResult.size() == inExpectedHits.size());
  59. for (const ExpectedHit &hit : inExpectedHits)
  60. {
  61. bool found = false;
  62. for (const ResultType &result : inResult)
  63. if (result.mContactPointOn2.IsClose(hit.mPosition, inAccuracySq)
  64. && result.mPenetrationAxis.Normalized().IsClose(hit.mPenetrationAxis, inAccuracySq))
  65. {
  66. found = true;
  67. break;
  68. }
  69. CHECK(found);
  70. }
  71. }
  72. // Collide our probe against the test shape and validate the hit results
  73. static void sTestCollideShape(Shape *inProbeShape, Shape *inTestShape, Vec3Arg inTestShapeScale, const CollideShapeSettings &inSettings, Vec3Arg inProbeShapePos, const Array<ExpectedHit> &inExpectedHits)
  74. {
  75. AllHitCollisionCollector<CollideShapeCollector> collector;
  76. CollisionDispatch::sCollideShapeVsShape(inProbeShape, inTestShape, Vec3::sReplicate(1.0f), inTestShapeScale, Mat44::sTranslation(inProbeShapePos), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), inSettings, collector);
  77. sCheckMatch(collector.mHits, inExpectedHits, 1.0e-8f);
  78. }
  79. // Collide a probe shape against our test shape in various locations to verify active edge behavior
  80. static void sTestCollideShape(const ShapeSettings *inTestShape, Vec3Arg inTestShapeScale, bool inActiveEdgesOnly)
  81. {
  82. CollideShapeSettings settings;
  83. settings.mActiveEdgeMode = inActiveEdgesOnly? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
  84. Ref<Shape> test_shape = inTestShape->Create().Get();
  85. Ref<Shape> capsule = sCreateProbeCapsule();
  86. // Test hitting all active edges
  87. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(-3.5f, cCapsuleProbeOffset, 0), { { Vec3(-3.5f, 0, 0), Vec3(1, 0, 0) } });
  88. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(3.5f, cCapsuleProbeOffset, 0), { { Vec3(3.5f, 0, 0), Vec3(-1, 0, 0) } });
  89. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(0, cCapsuleProbeOffset, -3.5f), { { Vec3(0, 0, -3.5f), Vec3(0, 0, 1) } });
  90. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(0, cCapsuleProbeOffset, 3.5f), { { Vec3(0, 0, 3.5f), Vec3(0, 0, -1) } });
  91. // Test hitting internal edges, this should return two hits
  92. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(-2.5f, cCapsuleProbeOffset, 0), { { Vec3(-2.5f, 0, 0), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(-1, 0, 0) }, { Vec3(-2.5f, 0, 0), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(1, 0, 0) } });
  93. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(0, cCapsuleProbeOffset, -2.5f), { { Vec3(0, 0, -2.5f), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(0, 0, -1) }, { Vec3(0, 0, -2.5f), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(0, 0, -1) } });
  94. // Test hitting an interior diagonal, this should return two hits
  95. sTestCollideShape(capsule, test_shape, inTestShapeScale, settings, Vec3(-3.0f, cCapsuleProbeOffset, 0), { { Vec3(-3.0f, 0, 0), inActiveEdgesOnly? Vec3(0, -1, 0) : (inTestShapeScale * Vec3(1, 0, -1)).Normalized() }, { Vec3(-3.0f, 0, 0), inActiveEdgesOnly? Vec3(0, -1, 0) : (inTestShapeScale * Vec3(-1, 0, 1)).Normalized() } });
  96. }
  97. TEST_CASE("CollideShapeMesh")
  98. {
  99. Ref<ShapeSettings> shape = sCreateMeshShape();
  100. sTestCollideShape(shape, Vec3::sReplicate(1.0f), false);
  101. sTestCollideShape(shape, Vec3::sReplicate(1.0f), true);
  102. sTestCollideShape(shape, Vec3(-1, 1, 1), false);
  103. sTestCollideShape(shape, Vec3(-1, 1, 1), true);
  104. }
  105. TEST_CASE("CollideShapeHeightField")
  106. {
  107. Ref<ShapeSettings> shape = sCreateHeightFieldShape();
  108. sTestCollideShape(shape, Vec3::sReplicate(1.0f), false);
  109. sTestCollideShape(shape, Vec3::sReplicate(1.0f), true);
  110. sTestCollideShape(shape, Vec3(-1, 1, 1), false);
  111. sTestCollideShape(shape, Vec3(-1, 1, 1), true);
  112. }
  113. // Cast our probe against the test shape and validate the hit results
  114. static void sTestCastShape(Shape *inProbeShape, Shape *inTestShape, Vec3Arg inTestShapeScale, const ShapeCastSettings &inSettings, Vec3Arg inProbeShapePos, Vec3Arg inProbeShapeDirection, const Array<ExpectedHit> &inExpectedHits)
  115. {
  116. AllHitCollisionCollector<CastShapeCollector> collector;
  117. ShapeCast shape_cast(inProbeShape, Vec3::sReplicate(1.0f), Mat44::sTranslation(inProbeShapePos), inProbeShapeDirection);
  118. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, inSettings, inTestShape, inTestShapeScale, ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  119. sCheckMatch(collector.mHits, inExpectedHits, 1.0e-6f);
  120. }
  121. // Cast a probe shape against our test shape in various locations to verify active edge behavior
  122. static void sTestCastShape(const ShapeSettings *inTestShape, Vec3Arg inTestShapeScale, bool inActiveEdgesOnly)
  123. {
  124. ShapeCastSettings settings;
  125. settings.mActiveEdgeMode = inActiveEdgesOnly? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
  126. settings.mReturnDeepestPoint = true;
  127. Ref<Shape> test_shape = inTestShape->Create().Get();
  128. Ref<Shape> capsule = sCreateProbeCapsule();
  129. // Test hitting all active edges
  130. sTestCastShape(capsule, test_shape, inTestShapeScale, settings, Vec3(-4, cCapsuleProbeOffset, 0), Vec3(0.5f, 0, 0), { { Vec3(-3.5f, 0, 0), Vec3(1, 0, 0) } });
  131. sTestCastShape(capsule, test_shape, inTestShapeScale, settings, Vec3(4, cCapsuleProbeOffset, 0), Vec3(-0.5f, 0, 0), { { Vec3(3.5f, 0, 0), Vec3(-1, 0, 0) } });
  132. sTestCastShape(capsule, test_shape, inTestShapeScale, settings, Vec3(0, cCapsuleProbeOffset, -4), Vec3(0, 0, 0.5f), { { Vec3(0, 0, -3.5f), Vec3(0, 0, 1) } });
  133. sTestCastShape(capsule, test_shape, inTestShapeScale, settings, Vec3(0, cCapsuleProbeOffset, 4), Vec3(0, 0, -0.5f), { { Vec3(0, 0, 3.5f), Vec3(0, 0, -1) } });
  134. // Test hitting internal edges, this should return two hits
  135. sTestCastShape(capsule, test_shape, inTestShapeScale, settings, Vec3(-2.5f - 1.1f * cCapsuleRadius, cCapsuleProbeOffset, 0), Vec3(0.2f * cCapsuleRadius, 0, 0), { { Vec3(-2.5f, 0, 0), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(-1, 0, 0) }, { Vec3(-2.5f, 0, 0), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(1, 0, 0) } });
  136. sTestCastShape(capsule, test_shape, inTestShapeScale, settings, Vec3(0, cCapsuleProbeOffset, -2.5f - 1.1f * cCapsuleRadius), Vec3(0, 0, 0.2f * cCapsuleRadius), { { Vec3(0, 0, -2.5f), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(0, 0, -1) }, { Vec3(0, 0, -2.5f), inActiveEdgesOnly? Vec3(0, -1, 0) : Vec3(0, 0, -1) } });
  137. }
  138. TEST_CASE("CastShapeMesh")
  139. {
  140. Ref<ShapeSettings> shape = sCreateMeshShape();
  141. sTestCastShape(shape, Vec3::sReplicate(1.0f), false);
  142. sTestCastShape(shape, Vec3::sReplicate(1.0f), true);
  143. sTestCastShape(shape, Vec3(-1, 1, 1), false);
  144. sTestCastShape(shape, Vec3(-1, 1, 1), true);
  145. }
  146. TEST_CASE("CastShapeHeightField")
  147. {
  148. Ref<ShapeSettings> shape = sCreateHeightFieldShape();
  149. sTestCastShape(shape, Vec3::sReplicate(1.0f), false);
  150. sTestCastShape(shape, Vec3::sReplicate(1.0f), true);
  151. sTestCastShape(shape, Vec3(-1, 1, 1), false);
  152. sTestCastShape(shape, Vec3(-1, 1, 1), true);
  153. }
  154. // Tests a discrete cube sliding over a mesh / heightfield shape
  155. static void sDiscreteCubeSlide(Ref<ShapeSettings> inShape, bool inCheckActiveEdges)
  156. {
  157. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  158. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  159. // Set simulation settings
  160. PhysicsSettings settings;
  161. settings.mCheckActiveEdges = inCheckActiveEdges;
  162. c.GetSystem()->SetPhysicsSettings(settings);
  163. // Create frictionless floor
  164. Body &floor = c.CreateBody(inShape, Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  165. floor.SetFriction(0.0f);
  166. // Create box sliding over the floor
  167. Vec3 initial_position(-3, 0.1f - cPenetrationSlop, 0);
  168. Vec3 initial_velocity(3, 0, 0);
  169. Body &box = c.CreateBox(initial_position, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.1f));
  170. box.SetLinearVelocity(initial_velocity);
  171. box.SetFriction(0.0f);
  172. box.GetMotionProperties()->SetLinearDamping(0.0f);
  173. const float cSimulationTime = 2.0f;
  174. c.Simulate(cSimulationTime);
  175. Vec3 expected_position = initial_position + cSimulationTime * initial_velocity;
  176. if (inCheckActiveEdges)
  177. {
  178. // Box should have slided frictionless over the plane without encountering any collisions
  179. CHECK_APPROX_EQUAL(box.GetPosition(), expected_position, 1.0e-4f);
  180. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), initial_velocity, 1.0e-4f);
  181. }
  182. else
  183. {
  184. // Box should have bumped into an internal edge and not reached its target
  185. CHECK(box.GetPosition().GetX() < expected_position.GetX() - 1.0f);
  186. }
  187. }
  188. TEST_CASE("DiscreteCubeSlideMesh")
  189. {
  190. Ref<ShapeSettings> shape = sCreateMeshShape();
  191. sDiscreteCubeSlide(shape, false);
  192. sDiscreteCubeSlide(shape, true);
  193. Ref<ShapeSettings> scaled_shape = new ScaledShapeSettings(shape, Vec3(-1, 1, 1));
  194. sDiscreteCubeSlide(scaled_shape, false);
  195. sDiscreteCubeSlide(scaled_shape, true);
  196. }
  197. TEST_CASE("DiscreteCubeSlideHeightField")
  198. {
  199. Ref<ShapeSettings> shape = sCreateHeightFieldShape();
  200. sDiscreteCubeSlide(shape, false);
  201. sDiscreteCubeSlide(shape, true);
  202. Ref<ShapeSettings> scaled_shape = new ScaledShapeSettings(shape, Vec3(-1, 1, 1));
  203. sDiscreteCubeSlide(scaled_shape, false);
  204. sDiscreteCubeSlide(scaled_shape, true);
  205. }
  206. // Tests a linear cast cube sliding over a mesh / heightfield shape
  207. static void sLinearCastCubeSlide(Ref<ShapeSettings> inShape, bool inCheckActiveEdges)
  208. {
  209. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  210. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  211. // Set simulation settings
  212. PhysicsSettings settings;
  213. settings.mCheckActiveEdges = inCheckActiveEdges;
  214. c.GetSystem()->SetPhysicsSettings(settings);
  215. // Create frictionless floor
  216. Body &floor = c.CreateBody(inShape, Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  217. floor.SetFriction(0.0f);
  218. // Create box starting a little bit above the floor and ending 0.5 * cPenetrationSlop below the floor so that if no internal edges are hit the motion should not be stopped
  219. // Note that we need the vertical velocity or else back face culling will ignore the face
  220. Vec3 initial_position(-3, 0.1f + cPenetrationSlop, 0);
  221. Vec3 initial_velocity(6 * 60, -1.5f * cPenetrationSlop * 60, 0);
  222. Body &box = c.CreateBox(initial_position, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(0.1f));
  223. box.SetLinearVelocity(initial_velocity);
  224. box.SetFriction(0.0f);
  225. box.GetMotionProperties()->SetLinearDamping(0.0f);
  226. // To avoid extra vertical velocity being picked up in 1 step, zero gravity
  227. c.ZeroGravity();
  228. c.SimulateSingleStep();
  229. Vec3 expected_position = initial_position + initial_velocity / 60.0f;
  230. if (inCheckActiveEdges)
  231. {
  232. // Box should stepped in one frame over the plane without encountering any linear cast collisions
  233. CHECK_APPROX_EQUAL(box.GetPosition(), expected_position, 1.0e-4f);
  234. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), initial_velocity, 1.0e-4f);
  235. }
  236. else
  237. {
  238. // Box should have bumped into an internal edge and not reached its target
  239. CHECK(box.GetPosition().GetX() < expected_position.GetX() - 1.0f);
  240. }
  241. }
  242. TEST_CASE("LinearCastCubeSlideMesh")
  243. {
  244. Ref<ShapeSettings> shape = sCreateMeshShape();
  245. sLinearCastCubeSlide(shape, false);
  246. sLinearCastCubeSlide(shape, true);
  247. Ref<ShapeSettings> scaled_shape = new ScaledShapeSettings(shape, Vec3(-1, 1, 1));
  248. sLinearCastCubeSlide(scaled_shape, false);
  249. sLinearCastCubeSlide(scaled_shape, true);
  250. }
  251. TEST_CASE("LinearCastCubeSlideHeightField")
  252. {
  253. Ref<ShapeSettings> shape = sCreateHeightFieldShape();
  254. sLinearCastCubeSlide(shape, false);
  255. sLinearCastCubeSlide(shape, true);
  256. Ref<ShapeSettings> scaled_shape = new ScaledShapeSettings(shape, Vec3(-1, 1, 1));
  257. sLinearCastCubeSlide(scaled_shape, false);
  258. sLinearCastCubeSlide(scaled_shape, true);
  259. }
  260. }