LoadSaveSceneTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/General/LoadSaveSceneTest.h>
  6. #include <Math/Perlin.h>
  7. #include <Jolt/ObjectStream/ObjectStreamOut.h>
  8. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  9. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  10. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  11. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  12. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  13. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  14. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  15. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  16. #include <Jolt/Physics/Collision/Shape/TaperedCylinderShape.h>
  17. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  18. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  19. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  20. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  21. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  22. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  23. #include <Jolt/Physics/Collision/Shape/EmptyShape.h>
  24. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  25. #include <Jolt/Physics/Constraints/DistanceConstraint.h>
  26. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  27. #include <Layers.h>
  28. #include <Utils/Log.h>
  29. #include <Utils/SoftBodyCreator.h>
  30. JPH_IMPLEMENT_RTTI_VIRTUAL(LoadSaveSceneTest)
  31. {
  32. JPH_ADD_BASE_CLASS(LoadSaveSceneTest, Test)
  33. }
  34. static const float cMaxHeight = 4.0f;
  35. static MeshShapeSettings *sCreateMesh()
  36. {
  37. const int n = 10;
  38. const float cell_size = 2.0f;
  39. // Create heights
  40. float heights[n + 1][n + 1];
  41. for (int x = 0; x <= n; ++x)
  42. for (int z = 0; z <= n; ++z)
  43. heights[x][z] = cMaxHeight * PerlinNoise3(float(x) / n, 0, float(z) / n, 256, 256, 256);
  44. // Create 'wall' around grid
  45. for (int x = 0; x <= n; ++x)
  46. {
  47. heights[x][0] += 2.0f;
  48. heights[x][n] += 2.0f;
  49. }
  50. for (int y = 1; y < n; ++y)
  51. {
  52. heights[0][y] += 2.0f;
  53. heights[n][y] += 2.0f;
  54. }
  55. // Create regular grid of triangles
  56. uint32 max_material_index = 0;
  57. TriangleList triangles;
  58. for (int x = 0; x < n; ++x)
  59. for (int z = 0; z < n; ++z)
  60. {
  61. float center = n * cell_size / 2;
  62. float x1 = cell_size * x - center;
  63. float z1 = cell_size * z - center;
  64. float x2 = x1 + cell_size;
  65. float z2 = z1 + cell_size;
  66. Float3 v1 = Float3(x1, heights[x][z], z1);
  67. Float3 v2 = Float3(x2, heights[x + 1][z], z1);
  68. Float3 v3 = Float3(x1, heights[x][z + 1], z2);
  69. Float3 v4 = Float3(x2, heights[x + 1][z + 1], z2);
  70. uint32 material_index = uint32((Vec3(v1) + Vec3(v2) + Vec3(v3) + Vec3(v4)).Length() / 4.0f / cell_size);
  71. max_material_index = max(max_material_index, material_index);
  72. triangles.push_back(Triangle(v1, v3, v4, material_index));
  73. triangles.push_back(Triangle(v1, v4, v2, material_index));
  74. }
  75. // Create materials
  76. PhysicsMaterialList materials;
  77. for (uint i = 0; i <= max_material_index; ++i)
  78. materials.push_back(new PhysicsMaterialSimple("Mesh Material " + ConvertToString(i), Color::sGetDistinctColor(i)));
  79. return new MeshShapeSettings(triangles, std::move(materials));
  80. }
  81. static HeightFieldShapeSettings *sCreateHeightField()
  82. {
  83. const int n = 32;
  84. const float cell_size = 1.0f;
  85. // Create height samples
  86. float heights[n * n];
  87. for (int y = 0; y < n; ++y)
  88. for (int x = 0; x < n; ++x)
  89. heights[y * n + x] = cMaxHeight * PerlinNoise3(float(x) / n, 0, float(y) / n, 256, 256, 256);
  90. // Make a hole
  91. heights[2 * n + 2] = HeightFieldShapeConstants::cNoCollisionValue;
  92. // Make material indices
  93. uint8 max_material_index = 0;
  94. uint8 material_indices[Square(n - 1)];
  95. for (int y = 0; y < n - 1; ++y)
  96. for (int x = 0; x < n - 1; ++x)
  97. {
  98. uint8 material_index = uint8(round((Vec3(x * cell_size, 0, y * cell_size) - Vec3(n * cell_size / 2, 0, n * cell_size / 2)).Length() / 10.0f));
  99. max_material_index = max(max_material_index, material_index);
  100. material_indices[y * (n - 1) + x] = material_index;
  101. }
  102. // Create materials
  103. PhysicsMaterialList materials;
  104. for (uint8 i = 0; i <= max_material_index; ++i)
  105. materials.push_back(new PhysicsMaterialSimple("HeightField Material " + ConvertToString(uint(i)), Color::sGetDistinctColor(i)));
  106. // Create height field
  107. return new HeightFieldShapeSettings(heights, Vec3(-0.5f * cell_size * n, 0.0f, -0.5f * cell_size * n), Vec3(cell_size, 1.0f, cell_size), n, material_indices, materials);
  108. }
  109. Ref<PhysicsScene> LoadSaveSceneTest::sCreateScene()
  110. {
  111. int color = 0;
  112. auto next_color = [&color]() { return Color::sGetDistinctColor(color++); };
  113. RVec3 pos(0, cMaxHeight, 0);
  114. auto next_pos = [&pos]() { pos += RVec3(0, 1.0f, 0); return pos; };
  115. // Create scene
  116. Ref<PhysicsScene> scene = new PhysicsScene();
  117. // A scaled mesh floor
  118. scene->AddBody(BodyCreationSettings(new ScaledShapeSettings(sCreateMesh(), Vec3(2.5f, 1.0f, 1.5f)), RVec3(0, 0, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  119. // A heightfield floor
  120. scene->AddBody(BodyCreationSettings(sCreateHeightField(), RVec3(50, 0, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  121. // Some simple primitives
  122. scene->AddBody(BodyCreationSettings(new TriangleShapeSettings(Vec3(-2, 0, 0), Vec3(0, 1, 0), Vec3(2, 0, 0), 0.0f, new PhysicsMaterialSimple("Triangle Material", next_color())), next_pos(), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Static, Layers::NON_MOVING));
  123. scene->AddBody(BodyCreationSettings(new SphereShapeSettings(0.2f, new PhysicsMaterialSimple("Sphere Material", next_color())), next_pos(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  124. scene->AddBody(BodyCreationSettings(new BoxShapeSettings(Vec3(0.2f, 0.2f, 0.4f), 0.01f, new PhysicsMaterialSimple("Box Material", next_color())), next_pos(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  125. scene->AddBody(BodyCreationSettings(new CapsuleShapeSettings(1.5f, 0.2f, new PhysicsMaterialSimple("Capsule Material", next_color())), next_pos(), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  126. scene->AddBody(BodyCreationSettings(new TaperedCapsuleShapeSettings(0.5f, 0.1f, 0.2f, new PhysicsMaterialSimple("Tapered Capsule Material", next_color())), next_pos(), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  127. scene->AddBody(BodyCreationSettings(new CylinderShapeSettings(0.5f, 0.2f, cDefaultConvexRadius, new PhysicsMaterialSimple("Cylinder Material", next_color())), next_pos(), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  128. scene->AddBody(BodyCreationSettings(new TaperedCylinderShapeSettings(0.5f, 0.2f, 0.4f, cDefaultConvexRadius, new PhysicsMaterialSimple("Tapered Cylinder Material", next_color())), next_pos(), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  129. scene->AddBody(BodyCreationSettings(new TaperedCylinderShapeSettings(0.5f, 0.4f, 0.0f, 0.0f, new PhysicsMaterialSimple("Cone Material", next_color())), next_pos(), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  130. scene->AddBody(BodyCreationSettings(new EmptyShapeSettings(), next_pos(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  131. // Compound with sub compound and rotation
  132. StaticCompoundShapeSettings *sub_compound = new StaticCompoundShapeSettings();
  133. sub_compound->AddShape(Vec3(0, 0.5f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShapeSettings(Vec3(0.5f, 0.1f, 0.2f), cDefaultConvexRadius, new PhysicsMaterialSimple("Compound Box Material", next_color())));
  134. sub_compound->AddShape(Vec3(0.5f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CylinderShapeSettings(0.5f, 0.2f, cDefaultConvexRadius, new PhysicsMaterialSimple("Compound Cylinder Material", next_color())));
  135. sub_compound->AddShape(Vec3(0, 0, 0.5f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(0.5f, 0.1f, 0.2f, new PhysicsMaterialSimple("Compound Tapered Capsule Material", next_color())));
  136. StaticCompoundShapeSettings *compound_shape = new StaticCompoundShapeSettings();
  137. compound_shape->AddShape(Vec3(0, 0, 0), Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), sub_compound);
  138. compound_shape->AddShape(Vec3(0, -0.1f, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), -0.75f * JPH_PI), sub_compound);
  139. scene->AddBody(BodyCreationSettings(compound_shape, next_pos(), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  140. // Convex hull shape
  141. Array<Vec3> tetrahedron;
  142. tetrahedron.push_back(Vec3(-0.5f, 0, -0.5f));
  143. tetrahedron.push_back(Vec3(0, 0, 0.5f));
  144. tetrahedron.push_back(Vec3(0.5f, 0, -0.5f));
  145. tetrahedron.push_back(Vec3(0, -0.5f, 0));
  146. Ref<ConvexHullShapeSettings> convex_hull = new ConvexHullShapeSettings(tetrahedron, cDefaultConvexRadius, new PhysicsMaterialSimple("Convex Hull Material", next_color()));
  147. scene->AddBody(BodyCreationSettings(convex_hull, next_pos(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  148. // Rotated convex hull
  149. scene->AddBody(BodyCreationSettings(new RotatedTranslatedShapeSettings(Vec3::sReplicate(0.5f), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), convex_hull), next_pos(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  150. // Mutable compound
  151. MutableCompoundShapeSettings *mutable_compound = new MutableCompoundShapeSettings();
  152. mutable_compound->AddShape(Vec3(0, 0.5f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShapeSettings(Vec3(0.5f, 0.1f, 0.2f), cDefaultConvexRadius, new PhysicsMaterialSimple("MutableCompound Box Material", next_color())));
  153. mutable_compound->AddShape(Vec3(0.5f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CapsuleShapeSettings(0.5f, 0.1f, new PhysicsMaterialSimple("MutableCompound Capsule Material", next_color())));
  154. mutable_compound->AddShape(Vec3(0, 0, 0.5f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(0.5f, 0.2f, 0.1f, new PhysicsMaterialSimple("MutableCompound Tapered Capsule Material", next_color())));
  155. scene->AddBody(BodyCreationSettings(mutable_compound, next_pos(), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  156. // Connect the first two dynamic bodies with a distance constraint
  157. DistanceConstraintSettings *dist_constraint = new DistanceConstraintSettings();
  158. dist_constraint->mSpace = EConstraintSpace::LocalToBodyCOM;
  159. scene->AddConstraint(dist_constraint, 3, 4);
  160. // Add soft body cube
  161. Ref<SoftBodySharedSettings> sb_cube_settings = SoftBodySharedSettings::sCreateCube(5, 0.2f);
  162. sb_cube_settings->mMaterials = { new PhysicsMaterialSimple("Soft Body Cube Material", next_color()) };
  163. SoftBodyCreationSettings sb_cube(sb_cube_settings, next_pos(), Quat::sIdentity(), Layers::MOVING);
  164. scene->AddSoftBody(sb_cube);
  165. // Add the same shape again to test sharing
  166. sb_cube.mPosition = next_pos();
  167. scene->AddSoftBody(sb_cube);
  168. // Add soft body sphere
  169. Ref<SoftBodySharedSettings> sb_sphere_settings = SoftBodyCreator::CreateSphere(0.5f);
  170. sb_sphere_settings->mMaterials = { new PhysicsMaterialSimple("Soft Body Sphere Material", next_color()) };
  171. SoftBodyCreationSettings sb_sphere(sb_sphere_settings, next_pos(), Quat::sIdentity(), Layers::MOVING);
  172. sb_sphere.mPressure = 2000.0f;
  173. scene->AddSoftBody(sb_sphere);
  174. return scene;
  175. }
  176. void LoadSaveSceneTest::Initialize()
  177. {
  178. #ifdef JPH_OBJECT_STREAM
  179. Ref<PhysicsScene> scene = sCreateScene();
  180. stringstream data;
  181. // Write scene
  182. if (!ObjectStreamOut::sWriteObject(data, ObjectStream::EStreamType::Text, *scene))
  183. FatalError("Failed to save scene");
  184. // Clear scene
  185. scene = nullptr;
  186. // Read scene back in
  187. if (!ObjectStreamIn::sReadObject(data, scene))
  188. FatalError("Failed to load scene");
  189. // Ensure that the soft body shared settings have been optimized (this is not saved to a text file)
  190. for (SoftBodyCreationSettings &soft_body : scene->GetSoftBodies())
  191. const_cast<SoftBodySharedSettings *>(soft_body.mSettings.GetPtr())->Optimize();
  192. // Instantiate scene
  193. scene->CreateBodies(mPhysicsSystem);
  194. #endif // JPH_OBJECT_STREAM
  195. }