ConvexVsMeshScene.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. // Jolt includes
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  8. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  9. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  10. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  11. // Local includes
  12. #include "PerformanceTestScene.h"
  13. #include "Layers.h"
  14. // A scene that drops a number of convex shapes on a sloping terrain made out of a mesh shape
  15. class ConvexVsMeshScene : public PerformanceTestScene
  16. {
  17. public:
  18. virtual const char * GetName() const override
  19. {
  20. return "ConvexVsMesh";
  21. }
  22. virtual bool Load() override
  23. {
  24. const int n = 100;
  25. const float cell_size = 3.0f;
  26. const float max_height = 5.0f;
  27. float center = n * cell_size / 2;
  28. // Create vertices
  29. const int num_vertices = (n + 1) * (n + 1);
  30. VertexList vertices;
  31. vertices.resize(num_vertices);
  32. for (int x = 0; x <= n; ++x)
  33. for (int z = 0; z <= n; ++z)
  34. {
  35. float height = Sin(float(x) * 50.0f / n) * Cos(float(z) * 50.0f / n);
  36. vertices[z * (n + 1) + x] = Float3(cell_size * x, max_height * height, cell_size * z);
  37. }
  38. // Create regular grid of triangles
  39. const int num_triangles = n * n * 2;
  40. IndexedTriangleList indices;
  41. indices.resize(num_triangles);
  42. IndexedTriangle *next = indices.data();
  43. for (int x = 0; x < n; ++x)
  44. for (int z = 0; z < n; ++z)
  45. {
  46. int start = (n + 1) * z + x;
  47. next->mIdx[0] = start;
  48. next->mIdx[1] = start + n + 1;
  49. next->mIdx[2] = start + 1;
  50. next++;
  51. next->mIdx[0] = start + 1;
  52. next->mIdx[1] = start + n + 1;
  53. next->mIdx[2] = start + n + 2;
  54. next++;
  55. }
  56. // Create mesh shape settings
  57. Ref<MeshShapeSettings> mesh_shape_settings = new MeshShapeSettings(vertices, indices);
  58. mesh_shape_settings->mMaxTrianglesPerLeaf = 4;
  59. // Create mesh shape creation settings
  60. mMeshSettings.mMotionType = EMotionType::Static;
  61. mMeshSettings.mObjectLayer = Layers::NON_MOVING;
  62. mMeshSettings.mPosition = Vec3(-center, max_height, -center);
  63. mMeshSettings.mFriction = 0.5f;
  64. mMeshSettings.mRestitution = 0.6f;
  65. mMeshSettings.SetShapeSettings(mesh_shape_settings);
  66. // Create other shapes
  67. mShapes = {
  68. new BoxShape(Vec3(0.5f, 0.75f, 1.0f)),
  69. new SphereShape(0.5f),
  70. new CapsuleShape(0.75f, 0.5f),
  71. ConvexHullShapeSettings({ Vec3(0, 1, 0), Vec3(1, 0, 0), Vec3(-1, 0, 0), Vec3(0, 0, 1), Vec3(0, 0, -1) }).Create().Get(),
  72. };
  73. return true;
  74. }
  75. virtual void StartTest(PhysicsSystem &inPhysicsSystem, EMotionQuality inMotionQuality) override
  76. {
  77. // Reduce the solver iteration count, the scene doesn't have any constraints so we don't need the default amount of iterations
  78. PhysicsSettings settings = inPhysicsSystem.GetPhysicsSettings();
  79. settings.mNumVelocitySteps = 4;
  80. settings.mNumPositionSteps = 1;
  81. inPhysicsSystem.SetPhysicsSettings(settings);
  82. // Create background
  83. BodyInterface &bi = inPhysicsSystem.GetBodyInterface();
  84. bi.CreateAndAddBody(mMeshSettings, EActivation::DontActivate);
  85. // Construct bodies
  86. for (int x = -10; x <= 10; ++x)
  87. for (int y = 0; y < (int)mShapes.size(); ++y)
  88. for (int z = -10; z <= 10; ++z)
  89. {
  90. BodyCreationSettings creation_settings;
  91. creation_settings.mMotionType = EMotionType::Dynamic;
  92. creation_settings.mMotionQuality = inMotionQuality;
  93. creation_settings.mObjectLayer = Layers::MOVING;
  94. creation_settings.mPosition = Vec3(7.5f * x, 15.0f + 2.0f * y, 7.5f * z);
  95. creation_settings.mFriction = 0.5f;
  96. creation_settings.mRestitution = 0.6f;
  97. creation_settings.SetShape(mShapes[y]);
  98. bi.CreateAndAddBody(creation_settings, EActivation::Activate);
  99. }
  100. }
  101. private:
  102. BodyCreationSettings mMeshSettings;
  103. Array<Ref<Shape>> mShapes;
  104. };