FunnelTest.cpp 5.3 KB

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