ScaledMeshShapeTest.cpp 4.3 KB

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