EnhancedInternalEdgeRemovalTest.cpp 6.7 KB

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