FunnelTest.cpp 5.4 KB

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