2
0

ConvexVsMeshScene.h 3.8 KB

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