FunnelTest.cpp 5.9 KB

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