ScaledHeightFieldShapeTest.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/ScaledHeightFieldShapeTest.h>
  6. #include <External/Perlin.h>
  7. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  10. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  11. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  12. #include <Layers.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(ScaledHeightFieldShapeTest)
  14. {
  15. JPH_ADD_BASE_CLASS(ScaledHeightFieldShapeTest, Test)
  16. }
  17. void ScaledHeightFieldShapeTest::Initialize()
  18. {
  19. // Floor
  20. CreateFloor();
  21. const int n = 64;
  22. const float cell_size = 0.25f;
  23. const float max_height = 4.0f;
  24. // Create height samples
  25. float heights[n * n];
  26. for (int y = 0; y < n; ++y)
  27. for (int x = 0; x < n; ++x)
  28. heights[y * n + x] = max_height * PerlinNoise3(float(x) * 2.0f / n, 0, float(y) * 2.0f / n, 256, 256, 256);
  29. // Create 'wall' around height field
  30. for (int x = 0; x < n; ++x)
  31. {
  32. heights[x] += 2.0f;
  33. heights[x + n * (n - 1)] += 2.0f;
  34. }
  35. for (int y = 1; y < n - 1; ++y)
  36. {
  37. heights[n * y] += 2.0f;
  38. heights[n - 1 + n * y] += 2.0f;
  39. }
  40. // Create height field
  41. RefConst<ShapeSettings> height_field = new HeightFieldShapeSettings(heights, Vec3(-0.5f * cell_size * n, 0.0f, -0.5f * cell_size * n), Vec3(cell_size, 1.0f, cell_size), n);
  42. // Original shape
  43. mBodyInterface->CreateAndAddBody(BodyCreationSettings(height_field, RVec3(-60, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  44. // Uniformly scaled shape < 1
  45. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ScaledShapeSettings(height_field, Vec3::sReplicate(0.5f)), RVec3(-40, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  46. // Uniformly scaled shape > 1
  47. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ScaledShapeSettings(height_field, Vec3::sReplicate(1.5f)), RVec3(-20, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  48. // Non-uniform scaled shape
  49. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ScaledShapeSettings(height_field, Vec3(0.5f, 1.0f, 1.5f)), RVec3(0, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  50. // Flipped in 2 axis
  51. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ScaledShapeSettings(height_field, Vec3(-0.5f, 1.0f, -1.5f)), RVec3(20, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  52. // Inside out
  53. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ScaledShapeSettings(height_field, Vec3(-0.5f, 1.0f, 1.5f)), RVec3(40, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  54. // Upside down
  55. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ScaledShapeSettings(height_field, Vec3(0.5f, -1.0f, 1.5f)), RVec3(60, 10, 0), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  56. // Create a number of balls above the height fields
  57. RefConst<Shape> sphere_shape = new SphereShape(0.2f);
  58. RefConst<Shape> box_shape = new BoxShape(Vec3(0.2f, 0.2f, 0.4f), 0.01f);
  59. for (int i = 0; i < 7; ++i)
  60. for (int j = 0; j < 5; ++j)
  61. mBodyInterface->CreateAndAddBody(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), EActivation::Activate);
  62. }