ScaledMeshShapeTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/ScaledShapes/ScaledMeshShapeTest.h>
  6. #include <Math/Perlin.h>
  7. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  8. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  9. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  10. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Layers.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(ScaledMeshShapeTest)
  14. {
  15. JPH_ADD_BASE_CLASS(ScaledMeshShapeTest, Test)
  16. }
  17. void ScaledMeshShapeTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. const int n = 10;
  22. const float cell_size = 2.0f;
  23. const float max_height = 4.0f;
  24. // Create heights
  25. float heights[n + 1][n + 1];
  26. for (int x = 0; x <= n; ++x)
  27. for (int z = 0; z <= n; ++z)
  28. heights[x][z] = max_height * PerlinNoise3(float(x) / n, 0, float(z) / n, 256, 256, 256);
  29. // Create 'wall' around grid
  30. for (int x = 0; x <= n; ++x)
  31. {
  32. heights[x][0] += 2.0f;
  33. heights[x][n] += 2.0f;
  34. }
  35. for (int y = 1; y < n; ++y)
  36. {
  37. heights[0][y] += 2.0f;
  38. heights[n][y] += 2.0f;
  39. }
  40. // Create regular grid of triangles
  41. TriangleList triangles;
  42. for (int x = 0; x < n; ++x)
  43. for (int z = 0; z < n; ++z)
  44. {
  45. float center = n * cell_size / 2;
  46. float x1 = cell_size * x - center;
  47. float z1 = cell_size * z - center;
  48. float x2 = x1 + cell_size;
  49. float z2 = z1 + cell_size;
  50. Float3 v1 = Float3(x1, heights[x][z], z1);
  51. Float3 v2 = Float3(x2, heights[x + 1][z], z1);
  52. Float3 v3 = Float3(x1, heights[x][z + 1], z2);
  53. Float3 v4 = Float3(x2, heights[x + 1][z + 1], z2);
  54. triangles.push_back(Triangle(v1, v3, v4));
  55. triangles.push_back(Triangle(v1, v4, v2));
  56. }
  57. RefConst<ShapeSettings> mesh_shape = new MeshShapeSettings(triangles);
  58. // Original shape
  59. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(mesh_shape, RVec3(-60, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  60. mBodyInterface->AddBody(body1.GetID(), EActivation::DontActivate);
  61. // Uniformly scaled shape < 1
  62. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3::sReplicate(0.5f)), RVec3(-40, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  63. mBodyInterface->AddBody(body2.GetID(), EActivation::DontActivate);
  64. // Uniformly scaled shape > 1
  65. Body &body3 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3::sReplicate(1.5f)), RVec3(-20, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  66. mBodyInterface->AddBody(body3.GetID(), EActivation::DontActivate);
  67. // Non-uniform scaled shape
  68. Body &body4 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3(0.5f, 1.0f, 1.5f)), RVec3(0, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  69. mBodyInterface->AddBody(body4.GetID(), EActivation::DontActivate);
  70. // Flipped in 2 axis
  71. Body &body5 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3(-0.5f, 1.0f, -1.5f)), RVec3(20, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  72. mBodyInterface->AddBody(body5.GetID(), EActivation::DontActivate);
  73. // Inside out
  74. Body &body6 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3(-0.5f, 1.0f, 1.5f)), RVec3(40, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  75. mBodyInterface->AddBody(body6.GetID(), EActivation::DontActivate);
  76. // Upside down
  77. Body &body7 = *mBodyInterface->CreateBody(BodyCreationSettings(new ScaledShapeSettings(mesh_shape, Vec3(0.5f, -1.0f, 1.5f)), RVec3(60, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  78. mBodyInterface->AddBody(body7.GetID(), EActivation::DontActivate);
  79. // Create a number of balls above the meshes
  80. RefConst<Shape> sphere_shape = new SphereShape(0.2f);
  81. RefConst<Shape> box_shape = new BoxShape(Vec3(0.2f, 0.2f, 0.4f), 0.01f);
  82. for (int i = 0; i < 7; ++i)
  83. for (int j = 0; j < 5; ++j)
  84. {
  85. Body &dynamic = *mBodyInterface->CreateBody(BodyCreationSettings((j & 1)? box_shape : sphere_shape, RVec3(-60.0f + 20.0f * i, 10.0f + max_height + 0.5f * j, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  86. mBodyInterface->AddBody(dynamic.GetID(), EActivation::Activate);
  87. }
  88. }