FunnelTest.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/FunnelTest.h>
  5. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  8. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  9. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  11. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  12. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  13. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  14. #include <random>
  15. #include <Layers.h>
  16. JPH_IMPLEMENT_RTTI_VIRTUAL(FunnelTest)
  17. {
  18. JPH_ADD_BASE_CLASS(FunnelTest, Test)
  19. }
  20. void FunnelTest::Initialize()
  21. {
  22. RefConst<Shape> box = new BoxShape(Vec3(50, 1, 50), 0.0f);
  23. // Funnel
  24. for (int i = 0; i < 4; ++i)
  25. {
  26. Quat rotation = Quat::sRotation(Vec3::sAxisY(), 0.5f * JPH_PI * i);
  27. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box, rotation * Vec3(25, 25, 0), rotation * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), EMotionType::Static, Layers::NON_MOVING), EActivation::DontActivate);
  28. }
  29. default_random_engine random;
  30. uniform_real_distribution<float> feature_size(0.1f, 2.0f);
  31. uniform_real_distribution<float> position_variation(-40, 40);
  32. uniform_real_distribution<float> scale_variation(-1.5f, 1.5f);
  33. // Random scale
  34. Vec3 scale(scale_variation(random), scale_variation(random), scale_variation(random));
  35. // Make it minimally -0.5 or 0.5 depending on the sign
  36. scale += Vec3::sSelect(Vec3::sReplicate(-0.5f), Vec3::sReplicate(0.5f), Vec3::sGreaterOrEqual(scale, Vec3::sZero()));
  37. RefConst<Shape> shape;
  38. for (int i = 0; i < 1000; ++i)
  39. {
  40. switch (random() % 8)
  41. {
  42. case 0:
  43. {
  44. shape = new SphereShape(feature_size(random));
  45. scale = scale.Swizzle<SWIZZLE_X, SWIZZLE_X, SWIZZLE_X>(); // Only uniform scale supported
  46. break;
  47. }
  48. case 1:
  49. {
  50. shape = new BoxShape(Vec3(feature_size(random), feature_size(random), feature_size(random)));
  51. break;
  52. }
  53. case 2:
  54. {
  55. // Create random points
  56. vector<Vec3> points;
  57. for (int j = 0; j < 20; ++j)
  58. points.push_back(feature_size(random) * Vec3::sRandom(random));
  59. shape = ConvexHullShapeSettings(points).Create().Get();
  60. break;
  61. }
  62. case 3:
  63. {
  64. shape = new CapsuleShape(0.5f * feature_size(random), feature_size(random));
  65. scale = scale.Swizzle<SWIZZLE_X, SWIZZLE_X, SWIZZLE_X>(); // Only uniform scale supported
  66. break;
  67. }
  68. case 4:
  69. {
  70. float top = feature_size(random);
  71. float bottom = feature_size(random);
  72. float half_height = max(0.5f * feature_size(random), 0.5f * abs(top - bottom) + 0.001f);
  73. shape = TaperedCapsuleShapeSettings(half_height, top, bottom).Create().Get();
  74. scale = scale.Swizzle<SWIZZLE_X, SWIZZLE_X, SWIZZLE_X>(); // Only uniform scale supported
  75. break;
  76. }
  77. case 5:
  78. {
  79. shape = new CylinderShape(0.5f * feature_size(random), feature_size(random));
  80. scale = scale.Swizzle<SWIZZLE_X, SWIZZLE_Y, SWIZZLE_X>(); // Scale X must be same as Z
  81. break;
  82. }
  83. case 6:
  84. {
  85. // Simple compound
  86. StaticCompoundShapeSettings compound_shape_settings;
  87. compound_shape_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new CapsuleShape(1, 0.1f));
  88. compound_shape_settings.AddShape(Vec3(0, -1, 0), Quat::sIdentity(), new SphereShape(0.5f));
  89. compound_shape_settings.AddShape(Vec3(0, 1, 0), Quat::sIdentity(), new SphereShape(0.5f));
  90. shape = compound_shape_settings.Create().Get();
  91. scale = scale.Swizzle<SWIZZLE_X, SWIZZLE_X, SWIZZLE_X>(); // Only uniform scale supported
  92. break;
  93. }
  94. case 7:
  95. {
  96. // Compound with sub compound and rotation
  97. Ref<StaticCompoundShapeSettings> sub_compound = new StaticCompoundShapeSettings();
  98. sub_compound->AddShape(Vec3(0, 0.75f, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new BoxShape(Vec3(0.75f, 0.25f, 0.2f)));
  99. sub_compound->AddShape(Vec3(0.75f, 0, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new CylinderShape(0.75f, 0.2f));
  100. sub_compound->AddShape(Vec3(0, 0, 0.75f), Quat::sRotation(Vec3::sAxisX(), 0.5f * JPH_PI), new TaperedCapsuleShapeSettings(0.75f, 0.25f, 0.2f));
  101. StaticCompoundShapeSettings compound_shape_settings;
  102. compound_shape_settings.AddShape(Vec3(0, 0, 0), Quat::sRotation(Vec3::sAxisX(), -0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), sub_compound);
  103. compound_shape_settings.AddShape(Vec3(0, -0.1f, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI) * Quat::sRotation(Vec3::sAxisZ(), -0.75f * JPH_PI), sub_compound);
  104. shape = compound_shape_settings.Create().Get();
  105. scale = scale.Swizzle<SWIZZLE_X, SWIZZLE_X, SWIZZLE_X>(); // Only uniform scale supported
  106. break;
  107. }
  108. }
  109. // Scale the shape
  110. if ((random() % 3) == 0)
  111. shape = new ScaledShape(shape, scale);
  112. Vec3 position(position_variation(random), 100.0f + position_variation(random), position_variation(random));
  113. mBodyInterface->CreateAndAddBody(BodyCreationSettings(shape, position, Quat::sRandom(random), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  114. }
  115. }
  116. void FunnelTest::GetInitialCamera(CameraState &ioState) const
  117. {
  118. ioState.mPos = Vec3(50, 100, 50);
  119. ioState.mForward = (Vec3(0, 50, 0) - ioState.mPos).Normalized();
  120. }