ActiveEdgesTests.cpp 14 KB

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