EnhancedInternalEdgeRemovalTest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/General/EnhancedInternalEdgeRemovalTest.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  9. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  10. #include <Jolt/Geometry/Triangle.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Layers.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(EnhancedInternalEdgeRemovalTest)
  14. {
  15. JPH_ADD_BASE_CLASS(EnhancedInternalEdgeRemovalTest, Test)
  16. }
  17. void EnhancedInternalEdgeRemovalTest::CreateSlidingObjects(RVec3Arg inStart)
  18. {
  19. // Slide the shapes over the grid of boxes
  20. RVec3 pos = inStart - RVec3(0, 0, 12.0_r);
  21. static const char *labels[] = { "Normal", "Enhanced edge removal" };
  22. for (int enhanced_removal = 0; enhanced_removal < 2; ++enhanced_removal)
  23. {
  24. // A box
  25. BodyCreationSettings box_bcs(new BoxShape(Vec3::sReplicate(2.0f)), pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  26. box_bcs.mLinearVelocity = Vec3(20, 0, 0);
  27. box_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  28. BodyID id = mBodyInterface->CreateAndAddBody(box_bcs, EActivation::Activate);
  29. SetBodyLabel(id, labels[enhanced_removal]);
  30. pos += RVec3(0, 0, 5.0_r);
  31. // A sphere
  32. BodyCreationSettings sphere_bcs(new SphereShape(2.0f), pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  33. sphere_bcs.mLinearVelocity = Vec3(20, 0, 0);
  34. sphere_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  35. id = mBodyInterface->CreateAndAddBody(sphere_bcs, EActivation::Activate);
  36. SetBodyLabel(id, labels[enhanced_removal]);
  37. pos += RVec3(0, 0, 5.0_r);
  38. // Compound
  39. RefConst<Shape> box = new BoxShape(Vec3::sReplicate(0.1f));
  40. StaticCompoundShapeSettings compound;
  41. compound.SetEmbedded();
  42. for (int x = 0; x < 2; ++x)
  43. for (int y = 0; y < 2; ++y)
  44. for (int z = 0; z < 2; ++z)
  45. compound.AddShape(Vec3(x == 0? -1.9f : 1.9f, y == 0? -1.9f : 1.9f, z == 0? -1.9f : 1.9f), Quat::sIdentity(), box);
  46. BodyCreationSettings compound_bcs(&compound, pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  47. compound_bcs.mLinearVelocity = Vec3(20, 0, 0);
  48. compound_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  49. id = mBodyInterface->CreateAndAddBody(compound_bcs, EActivation::Activate);
  50. SetBodyLabel(id, labels[enhanced_removal]);
  51. pos += RVec3(0, 0, 7.0_r);
  52. }
  53. }
  54. void EnhancedInternalEdgeRemovalTest::Initialize()
  55. {
  56. // This test creates a grid of connected boxes and tests that objects don't hit the internal edges
  57. {
  58. StaticCompoundShapeSettings compound_settings;
  59. compound_settings.SetEmbedded();
  60. constexpr float size = 2.0f;
  61. RefConst<Shape> box_shape = new BoxShape(Vec3::sReplicate(0.5f * size));
  62. for (int x = -10; x < 10; ++x)
  63. for (int z = -10; z < 10; ++z)
  64. compound_settings.AddShape(Vec3(size * x, 0, size * z), Quat::sIdentity(), box_shape);
  65. BodyID id = mBodyInterface->CreateAndAddBody(BodyCreationSettings(&compound_settings, RVec3(0, -1, -40), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  66. SetBodyLabel(id, "Dense grid of boxes");
  67. CreateSlidingObjects(RVec3(-18, 1.9_r, -40.0_r));
  68. }
  69. // This tests if objects do not collide with internal edges
  70. {
  71. // Create a dense grid of triangles so that we have a large chance of hitting an internal edge
  72. constexpr float size = 2.0f;
  73. TriangleList triangles;
  74. for (int x = -10; x < 10; ++x)
  75. for (int z = -10; z < 10; ++z)
  76. {
  77. float x1 = size * x;
  78. float z1 = size * z;
  79. float x2 = x1 + size;
  80. float z2 = z1 + size;
  81. Float3 v1 = Float3(x1, 0, z1);
  82. Float3 v2 = Float3(x2, 0, z1);
  83. Float3 v3 = Float3(x1, 0, z2);
  84. Float3 v4 = Float3(x2, 0, z2);
  85. triangles.push_back(Triangle(v1, v3, v4));
  86. triangles.push_back(Triangle(v1, v4, v2));
  87. }
  88. MeshShapeSettings mesh_settings(triangles);
  89. mesh_settings.mActiveEdgeCosThresholdAngle = -1.0f; // Turn off regular active edge determination so that we only rely on the mEnhancedInternalEdgeRemoval flag
  90. mesh_settings.SetEmbedded();
  91. BodyID id = mBodyInterface->CreateAndAddBody(BodyCreationSettings(&mesh_settings, RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  92. SetBodyLabel(id, "Dense triangle mesh");
  93. CreateSlidingObjects(RVec3(-18, 1.9_r, 0));
  94. }
  95. // This test tests that we only ignore edges that are shared with voided triangles
  96. {
  97. // Create an L shape mesh lying on its back
  98. TriangleList triangles;
  99. constexpr float height = 0.5f;
  100. constexpr float half_width = 5.0f;
  101. constexpr float half_length = 2.0f;
  102. triangles.push_back(Triangle(Float3(-half_length, 0, half_width), Float3(half_length, 0, -half_width), Float3(-half_length, 0, -half_width)));
  103. triangles.push_back(Triangle(Float3(-half_length, 0, half_width), Float3(half_length, 0, half_width), Float3(half_length, 0, -half_width)));
  104. triangles.push_back(Triangle(Float3(half_length, height, half_width), Float3(half_length, height, -half_width), Float3(half_length, 0, half_width)));
  105. triangles.push_back(Triangle(Float3(half_length, 0, half_width), Float3(half_length, height, -half_width), Float3(half_length, 0, -half_width)));
  106. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new MeshShapeSettings(triangles), RVec3(0, 0, 30), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  107. // Roll a sphere towards the edge pointing upwards
  108. float z = 28.0f;
  109. for (int enhanced_removal = 0; enhanced_removal < 2; ++enhanced_removal)
  110. {
  111. // A sphere
  112. BodyCreationSettings sphere_bcs(new SphereShape(1.0f), RVec3(0, 1, z), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  113. sphere_bcs.mLinearVelocity = Vec3(20, 0, 0);
  114. sphere_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  115. mBodyInterface->CreateAndAddBody(sphere_bcs, EActivation::Activate);
  116. z += 4.0f;
  117. }
  118. }
  119. // This tests that fast moving spheres rolling over a triangle will not be affected by internal edges
  120. {
  121. // Create a flat plane
  122. MeshShapeSettings plane_mesh({
  123. {
  124. Float3(-10, 0, -10),
  125. Float3(-10, 0, 10),
  126. Float3(10, 0, 10)
  127. },
  128. {
  129. Float3(-10, 0, -10),
  130. Float3(10, 0, 10),
  131. Float3(10, 0, -10)
  132. },
  133. });
  134. plane_mesh.SetEmbedded();
  135. BodyCreationSettings level_plane(&plane_mesh, RVec3(-10, 0, 50), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  136. level_plane.mFriction = 1;
  137. BodyID id = mBodyInterface->CreateAndAddBody(level_plane, EActivation::DontActivate);
  138. SetBodyLabel(id, "Dense triangle mesh");
  139. // Roll a ball over it
  140. BodyCreationSettings level_ball(new SphereShape(0.5f), RVec3(-10, 1, 41), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  141. level_ball.mEnhancedInternalEdgeRemoval = true;
  142. level_ball.mFriction = 1;
  143. level_ball.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  144. level_ball.mMassPropertiesOverride.mMass = 1;
  145. mLevelBall = mBodyInterface->CreateAndAddBody(level_ball, EActivation::Activate);
  146. // Create a sloped plane
  147. BodyCreationSettings slope_plane(&plane_mesh, RVec3(10, 0, 50), Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(45)), EMotionType::Static, Layers::NON_MOVING);
  148. slope_plane.mFriction = 1;
  149. id = mBodyInterface->CreateAndAddBody(slope_plane, EActivation::DontActivate);
  150. SetBodyLabel(id, "Dense triangle mesh");
  151. // Roll a ball over it
  152. BodyCreationSettings slope_ball(new SphereShape(0.5f), RVec3(10, 8, 44), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  153. slope_ball.mEnhancedInternalEdgeRemoval = true;
  154. slope_ball.mFriction = 1;
  155. slope_ball.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  156. slope_ball.mMassPropertiesOverride.mMass = 1;
  157. mBodyInterface->CreateAndAddBody(slope_ball, EActivation::Activate);
  158. }
  159. // This tests a previous bug where a compound shape will fall through a box because features are voided by accident.
  160. // This is because both boxes of the compound shape collide with the top face of the static box. The big box will have a normal
  161. // that is aligned with the face so will be processed immediately. This will void the top face of the static box. The small box,
  162. // which collides with an edge of the top face will not be processed. This will cause the small box to penetrate the face.
  163. {
  164. // A box
  165. BodyCreationSettings box_bcs(new BoxShape(Vec3::sReplicate(2.5f)), RVec3(0, 0, 70), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  166. mBodyInterface->CreateAndAddBody(box_bcs, EActivation::DontActivate);
  167. // Compound
  168. StaticCompoundShapeSettings compound;
  169. compound.SetEmbedded();
  170. compound.AddShape(Vec3(-2.5f, 0, 0), Quat::sIdentity(), new BoxShape(Vec3(2.5f, 0.1f, 0.1f)));
  171. compound.AddShape(Vec3(0.1f, 0, 0), Quat::sIdentity(), new BoxShape(Vec3(0.1f, 1, 1)));
  172. BodyCreationSettings compound_bcs(&compound, RVec3(2, 5, 70), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  173. compound_bcs.mEnhancedInternalEdgeRemoval = true;
  174. mBodyInterface->CreateAndAddBody(compound_bcs, EActivation::Activate);
  175. }
  176. // Create a super dense grid of triangles
  177. {
  178. constexpr float size = 0.25f;
  179. TriangleList triangles;
  180. for (int x = -100; x < 100; ++x)
  181. for (int z = -5; z < 5; ++z)
  182. {
  183. float x1 = size * x;
  184. float z1 = size * z;
  185. float x2 = x1 + size;
  186. float z2 = z1 + size;
  187. Float3 v1 = Float3(x1, 0, z1);
  188. Float3 v2 = Float3(x2, 0, z1);
  189. Float3 v3 = Float3(x1, 0, z2);
  190. Float3 v4 = Float3(x2, 0, z2);
  191. triangles.push_back(Triangle(v1, v3, v4));
  192. triangles.push_back(Triangle(v1, v4, v2));
  193. }
  194. MeshShapeSettings mesh_settings(triangles);
  195. mesh_settings.mActiveEdgeCosThresholdAngle = -1.0f; // Turn off regular active edge determination so that we only rely on the mEnhancedInternalEdgeRemoval flag
  196. mesh_settings.SetEmbedded();
  197. BodyID id = mBodyInterface->CreateAndAddBody(BodyCreationSettings(&mesh_settings, RVec3(0, 0, 80), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  198. SetBodyLabel(id, "Dense triangle mesh");
  199. BodyCreationSettings box_bcs(new BoxShape(Vec3::sReplicate(1.0f)), RVec3(-24, 0.9_r, 80), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  200. box_bcs.mLinearVelocity = Vec3(20, 0, 0);
  201. box_bcs.mEnhancedInternalEdgeRemoval = true;
  202. mBodyInterface->CreateAndAddBody(box_bcs, EActivation::Activate);
  203. }
  204. }
  205. void EnhancedInternalEdgeRemovalTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  206. {
  207. // Increase rotation speed of the ball on the flat plane
  208. mBodyInterface->AddTorque(mLevelBall, Vec3(JPH_PI * 4, 0, 0));
  209. }