ManifoldReductionTest.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/ManifoldReductionTest.h>
  5. #include <Math/Perlin.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  8. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  9. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  10. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Layers.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(ManifoldReductionTest)
  14. {
  15. JPH_ADD_BASE_CLASS(ManifoldReductionTest, Test)
  16. }
  17. void ManifoldReductionTest::Initialize()
  18. {
  19. constexpr float cPerturbance = 0.02f;
  20. // Create mesh of regular grid of triangles
  21. TriangleList triangles;
  22. for (int x = -10; x < 10; ++x)
  23. for (int z = -10; z < 10; ++z)
  24. {
  25. float x1 = 0.1f * x;
  26. float z1 = 0.1f * z;
  27. float x2 = x1 + 0.1f;
  28. float z2 = z1 + 0.1f;
  29. Float3 v1 = Float3(x1, cPerturbance * PerlinNoise3(x1, 0, z1, 256, 256, 256), z1);
  30. Float3 v2 = Float3(x2, cPerturbance * PerlinNoise3(x2, 0, z1, 256, 256, 256), z1);
  31. Float3 v3 = Float3(x1, cPerturbance * PerlinNoise3(x1, 0, z2, 256, 256, 256), z2);
  32. Float3 v4 = Float3(x2, cPerturbance * PerlinNoise3(x2, 0, z2, 256, 256, 256), z2);
  33. triangles.push_back(Triangle(v1, v3, v4, 0));
  34. triangles.push_back(Triangle(v1, v4, v2, 0));
  35. }
  36. PhysicsMaterialList materials;
  37. materials.push_back(new PhysicsMaterialSimple());
  38. Ref<ShapeSettings> mesh_shape = new MeshShapeSettings(triangles, materials);
  39. // Floor
  40. Body &floor = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3::sReplicate(20)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  41. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  42. // Create a box made of meshes
  43. Ref<StaticCompoundShapeSettings> mesh_box_shape = new StaticCompoundShapeSettings;
  44. mesh_box_shape->AddShape(Vec3(0, -1, 0), Quat::sRotation(Vec3::sAxisX(), JPH_PI), mesh_shape);
  45. mesh_box_shape->AddShape(Vec3(0, 1, 0), Quat::sIdentity(), mesh_shape);
  46. mesh_box_shape->AddShape(Vec3(-1, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), mesh_shape);
  47. mesh_box_shape->AddShape(Vec3(1, 0, 0), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), mesh_shape);
  48. mesh_box_shape->AddShape(Vec3(0, 0, -1), Quat::sRotation(Vec3::sAxisX(), -0.5f * JPH_PI), mesh_shape);
  49. mesh_box_shape->AddShape(Vec3(0, 0, 1), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), mesh_shape);
  50. // A convex box
  51. RefConst<ShapeSettings> box_shape = new BoxShapeSettings(Vec3(1, 1, 1), 0.0f);
  52. {
  53. // Create a compound of 3 mesh boxes
  54. Ref<StaticCompoundShapeSettings> three_mesh_box_shape = new StaticCompoundShapeSettings;
  55. three_mesh_box_shape->AddShape(Vec3(-2.1f, 0, 0), Quat::sIdentity(), mesh_box_shape);
  56. three_mesh_box_shape->AddShape(Vec3(0, -1, 0), Quat::sIdentity(), mesh_box_shape);
  57. three_mesh_box_shape->AddShape(Vec3(2.1f, 0, 0), Quat::sIdentity(), mesh_box_shape);
  58. // Create a compound of 3 convex boxes
  59. Ref<StaticCompoundShapeSettings> three_box_shape = new StaticCompoundShapeSettings;
  60. three_box_shape->AddShape(Vec3(-2.1f, 0, 0), Quat::sIdentity(), box_shape);
  61. three_box_shape->AddShape(Vec3(0, -1.1f, 0), Quat::sIdentity(), box_shape);
  62. three_box_shape->AddShape(Vec3(2.1f, 0, 0), Quat::sIdentity(), box_shape);
  63. // A set of 3 mesh boxes to rest on
  64. Body &three_mesh_box = *mBodyInterface->CreateBody(BodyCreationSettings(three_mesh_box_shape, Vec3(0, 1, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  65. mBodyInterface->AddBody(three_mesh_box.GetID(), EActivation::DontActivate);
  66. // A set of 3 boxes that are dynamic where the middle one penetrates more than the other two
  67. BodyCreationSettings box_settings(three_box_shape, Vec3(0, 2.95f, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  68. box_settings.mAllowSleeping = false;
  69. Body &box = *mBodyInterface->CreateBody(box_settings);
  70. mBodyInterface->AddBody(box.GetID(), EActivation::Activate);
  71. }
  72. {
  73. // Create a compound of 2 mesh boxes
  74. Ref<StaticCompoundShapeSettings> two_mesh_box_shape = new StaticCompoundShapeSettings;
  75. two_mesh_box_shape->AddShape(Vec3(-2.1f, 0, 0), Quat::sIdentity(), mesh_box_shape);
  76. two_mesh_box_shape->AddShape(Vec3(0, -1, 0), Quat::sIdentity(), mesh_box_shape);
  77. // Create a compound of 2 convex boxes
  78. Ref<StaticCompoundShapeSettings> two_box_shape = new StaticCompoundShapeSettings;
  79. two_box_shape->AddShape(Vec3(-2.1f, 0, 0), Quat::sIdentity(), box_shape);
  80. two_box_shape->AddShape(Vec3(0, -1, 0), Quat::sIdentity(), box_shape);
  81. // A set of 2 mesh boxes to rest on
  82. Body &two_mesh_box = *mBodyInterface->CreateBody(BodyCreationSettings(two_mesh_box_shape, Vec3(0, 1, 4), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  83. mBodyInterface->AddBody(two_mesh_box.GetID(), EActivation::DontActivate);
  84. // A set of 2 boxes that are dynamic, one is lower than the other
  85. BodyCreationSettings box_settings(two_box_shape, Vec3(0, 4, 4), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  86. box_settings.mAllowSleeping = false;
  87. Body &box = *mBodyInterface->CreateBody(box_settings);
  88. mBodyInterface->AddBody(box.GetID(), EActivation::Activate);
  89. }
  90. {
  91. // Create a compound of 2 meshes under small angle, small enough to combine the manifolds.
  92. Ref<StaticCompoundShapeSettings> two_mesh_shape = new StaticCompoundShapeSettings;
  93. two_mesh_shape->AddShape(Vec3(1, 0, 0), Quat::sRotation(Vec3::sAxisZ(), DegreesToRadians(2)), mesh_shape);
  94. two_mesh_shape->AddShape(Vec3(-1, 0, 0), Quat::sRotation(Vec3::sAxisZ(), DegreesToRadians(-2)), mesh_shape);
  95. // A set of 2 meshes to rest on
  96. Body &two_mesh_box = *mBodyInterface->CreateBody(BodyCreationSettings(two_mesh_shape, Vec3(0, 1, -4), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  97. mBodyInterface->AddBody(two_mesh_box.GetID(), EActivation::DontActivate);
  98. // A box that is dynamic, resting on the slightly sloped surface. The surface normals are close enough so that the manifold should be merged.
  99. BodyCreationSettings box_settings(box_shape, Vec3(0, 4, -4), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  100. box_settings.mAllowSleeping = false;
  101. Body &box = *mBodyInterface->CreateBody(box_settings);
  102. mBodyInterface->AddBody(box.GetID(), EActivation::Activate);
  103. }
  104. {
  105. // Create a compound of 2 meshes under small angle, but bigger than the limit to combine the manifolds.
  106. Ref<StaticCompoundShapeSettings> two_mesh_shape = new StaticCompoundShapeSettings();
  107. two_mesh_shape->AddShape(Vec3(1, 0, 0), Quat::sRotation(Vec3::sAxisZ(), DegreesToRadians(3)), mesh_shape);
  108. two_mesh_shape->AddShape(Vec3(-1, 0, 0), Quat::sRotation(Vec3::sAxisZ(), DegreesToRadians(-3)), mesh_shape);
  109. // A set of 2 meshes to rest on
  110. Body &two_mesh_box = *mBodyInterface->CreateBody(BodyCreationSettings(two_mesh_shape, Vec3(0, 1, -8), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  111. mBodyInterface->AddBody(two_mesh_box.GetID(), EActivation::DontActivate);
  112. // A box that is dynamic, resting on the slightly sloped surface. The surface normals are not close enough so that the manifold should be merged.
  113. BodyCreationSettings box_settings(box_shape, Vec3(0, 4, -8), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  114. box_settings.mAllowSleeping = false;
  115. Body &box = *mBodyInterface->CreateBody(box_settings);
  116. mBodyInterface->AddBody(box.GetID(), EActivation::Activate);
  117. }
  118. }