EnhancedInternalEdgeRemovalTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. for (int enhanced_removal = 0; enhanced_removal < 2; ++enhanced_removal)
  22. {
  23. // A box
  24. BodyCreationSettings box_bcs(new BoxShape(Vec3::sReplicate(2.0f)), pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  25. box_bcs.mLinearVelocity = Vec3(20, 0, 0);
  26. box_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  27. mBodyInterface->CreateAndAddBody(box_bcs, EActivation::Activate);
  28. pos += RVec3(0, 0, 5.0_r);
  29. // A sphere
  30. BodyCreationSettings sphere_bcs(new SphereShape(2.0f), pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  31. sphere_bcs.mLinearVelocity = Vec3(20, 0, 0);
  32. sphere_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  33. mBodyInterface->CreateAndAddBody(sphere_bcs, EActivation::Activate);
  34. pos += RVec3(0, 0, 5.0_r);
  35. // Compound
  36. RefConst<Shape> box = new BoxShape(Vec3::sReplicate(0.1f));
  37. StaticCompoundShapeSettings compound;
  38. compound.SetEmbedded();
  39. for (int x = 0; x < 2; ++x)
  40. for (int y = 0; y < 2; ++y)
  41. for (int z = 0; z < 2; ++z)
  42. compound.AddShape(Vec3(x == 0? -1.9f : 1.9f, y == 0? -1.9f : 1.9f, z == 0? -1.9f : 1.9f), Quat::sIdentity(), box);
  43. BodyCreationSettings compound_bcs(&compound, pos, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  44. compound_bcs.mLinearVelocity = Vec3(20, 0, 0);
  45. compound_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  46. mBodyInterface->CreateAndAddBody(compound_bcs, EActivation::Activate);
  47. pos += RVec3(0, 0, 7.0_r);
  48. }
  49. }
  50. void EnhancedInternalEdgeRemovalTest::Initialize()
  51. {
  52. // This test creates a grid of connected boxes and tests that objects don't hit the internal edges
  53. {
  54. StaticCompoundShapeSettings compound_settings;
  55. compound_settings.SetEmbedded();
  56. constexpr float size = 2.0f;
  57. RefConst<Shape> box_shape = new BoxShape(Vec3::sReplicate(0.5f * size));
  58. for (int x = -10; x < 10; ++x)
  59. for (int z = -10; z < 10; ++z)
  60. compound_settings.AddShape(Vec3(size * x, 0, size * z), Quat::sIdentity(), box_shape);
  61. mBodyInterface->CreateAndAddBody(BodyCreationSettings(&compound_settings, RVec3(0, -1, -40), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  62. CreateSlidingObjects(RVec3(-18, 1.9_r, -40.0_r));
  63. }
  64. // This tests if objects do not collide with internal edges
  65. {
  66. // Create a dense grid of triangles so that we have a large chance of hitting an internal edge
  67. constexpr float size = 2.0f;
  68. TriangleList triangles;
  69. for (int x = -10; x < 10; ++x)
  70. for (int z = -10; z < 10; ++z)
  71. {
  72. float x1 = size * x;
  73. float z1 = size * z;
  74. float x2 = x1 + size;
  75. float z2 = z1 + size;
  76. Float3 v1 = Float3(x1, 0, z1);
  77. Float3 v2 = Float3(x2, 0, z1);
  78. Float3 v3 = Float3(x1, 0, z2);
  79. Float3 v4 = Float3(x2, 0, z2);
  80. triangles.push_back(Triangle(v1, v3, v4));
  81. triangles.push_back(Triangle(v1, v4, v2));
  82. }
  83. MeshShapeSettings mesh_settings(triangles);
  84. mesh_settings.mActiveEdgeCosThresholdAngle = FLT_MAX; // Turn off regular active edge determination so that we only rely on the mEnhancedInternalEdgeRemoval flag
  85. mesh_settings.SetEmbedded();
  86. mBodyInterface->CreateAndAddBody(BodyCreationSettings(&mesh_settings, RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  87. CreateSlidingObjects(RVec3(-18, 1.9_r, 0));
  88. }
  89. // This test tests that we only ignore edges that are shared with voided triangles
  90. {
  91. // Create an L shape mesh lying on its back
  92. TriangleList triangles;
  93. constexpr float height = 0.5f;
  94. constexpr float half_width = 5.0f;
  95. constexpr float half_length = 2.0f;
  96. triangles.push_back(Triangle(Float3(-half_length, 0, half_width), Float3(half_length, 0, -half_width), Float3(-half_length, 0, -half_width)));
  97. triangles.push_back(Triangle(Float3(-half_length, 0, half_width), Float3(half_length, 0, half_width), Float3(half_length, 0, -half_width)));
  98. triangles.push_back(Triangle(Float3(half_length, height, half_width), Float3(half_length, height, -half_width), Float3(half_length, 0, half_width)));
  99. triangles.push_back(Triangle(Float3(half_length, 0, half_width), Float3(half_length, height, -half_width), Float3(half_length, 0, -half_width)));
  100. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new MeshShapeSettings(triangles), RVec3(0, 0, 30), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  101. // Roll a sphere towards the edge pointing upwards
  102. float z = 28.0f;
  103. for (int enhanced_removal = 0; enhanced_removal < 2; ++enhanced_removal)
  104. {
  105. // A sphere
  106. BodyCreationSettings sphere_bcs(new SphereShape(1.0f), RVec3(0, 1, z), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  107. sphere_bcs.mLinearVelocity = Vec3(20, 0, 0);
  108. sphere_bcs.mEnhancedInternalEdgeRemoval = enhanced_removal == 1;
  109. mBodyInterface->CreateAndAddBody(sphere_bcs, EActivation::Activate);
  110. z += 4.0f;
  111. }
  112. }
  113. // This tests that fast moving spheres rolling over a triangle will not be affected by internal edges
  114. {
  115. // Create a flat plane
  116. MeshShapeSettings plane_mesh({
  117. {
  118. Float3(-10, 0, -10),
  119. Float3(-10, 0, 10),
  120. Float3(10, 0, 10)
  121. },
  122. {
  123. Float3(-10, 0, -10),
  124. Float3(10, 0, 10),
  125. Float3(10, 0, -10)
  126. },
  127. });
  128. plane_mesh.SetEmbedded();
  129. BodyCreationSettings level_plane(&plane_mesh, RVec3(-10, 0, 50), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  130. level_plane.mFriction = 1;
  131. mBodyInterface->CreateAndAddBody(level_plane, EActivation::DontActivate);
  132. // Roll a ball over it
  133. BodyCreationSettings level_ball(new SphereShape(0.5f), RVec3(-10, 1, 41), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  134. level_ball.mEnhancedInternalEdgeRemoval = true;
  135. level_ball.mFriction = 1;
  136. level_ball.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  137. level_ball.mMassPropertiesOverride.mMass = 1;
  138. mLevelBall = mBodyInterface->CreateAndAddBody(level_ball, EActivation::Activate);
  139. // Create a sloped plane
  140. BodyCreationSettings slope_plane(&plane_mesh, RVec3(10, 0, 50), Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(45)), EMotionType::Static, Layers::NON_MOVING);
  141. slope_plane.mFriction = 1;
  142. mBodyInterface->CreateAndAddBody(slope_plane, EActivation::DontActivate);
  143. // Roll a ball over it
  144. BodyCreationSettings slope_ball(new SphereShape(0.5f), RVec3(10, 8, 44), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  145. slope_ball.mEnhancedInternalEdgeRemoval = true;
  146. slope_ball.mFriction = 1;
  147. slope_ball.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  148. slope_ball.mMassPropertiesOverride.mMass = 1;
  149. mBodyInterface->CreateAndAddBody(slope_ball, EActivation::Activate);
  150. }
  151. }
  152. void EnhancedInternalEdgeRemovalTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  153. {
  154. // Increase rotation speed of the ball on the flat plane
  155. mBodyInterface->AddTorque(mLevelBall, Vec3(JPH_PI * 4, 0, 0));
  156. }