VehicleTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/Vehicle/VehicleTest.h>
  5. #include <Jolt/Physics/Constraints/DistanceConstraint.h>
  6. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  8. #include <Jolt/Physics/Collision/GroupFilterTable.h>
  9. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  10. #include <Jolt/Physics/PhysicsScene.h>
  11. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  12. #include <Layers.h>
  13. #include <Application/DebugUI.h>
  14. #include <Utils/Log.h>
  15. JPH_IMPLEMENT_RTTI_VIRTUAL(VehicleTest)
  16. {
  17. JPH_ADD_BASE_CLASS(VehicleTest, Test)
  18. }
  19. const char *VehicleTest::sScenes[] =
  20. {
  21. "Flat",
  22. "Steep Slope",
  23. "Playground",
  24. "Terrain1",
  25. };
  26. const char *VehicleTest::sSceneName = "Playground";
  27. void VehicleTest::Initialize()
  28. {
  29. if (strcmp(sSceneName, "Flat") == 0)
  30. {
  31. // Flat test floor
  32. Body &floor = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(1000.0f, 1.0f, 1000.0f), 0.0f), Vec3(0.0f, -1.0f, 0.0f), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING));
  33. floor.SetFriction(1.0f);
  34. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  35. }
  36. else if (strcmp(sSceneName, "Steep Slope") == 0)
  37. {
  38. // Steep slope test floor (20 degrees = 36% grade)
  39. Body &floor = *mBodyInterface->CreateBody(BodyCreationSettings(new BoxShape(Vec3(1000.0f, 1.0f, 1000.0f), 0.0f), Vec3(0.0f, -1.0f, 0.0f), Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-20.0f)), EMotionType::Static, Layers::NON_MOVING));
  40. floor.SetFriction(1.0f);
  41. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  42. }
  43. else if (strcmp(sSceneName, "Playground") == 0)
  44. {
  45. // Scene with hilly terrain and some objects to drive into
  46. Body &floor = CreateMeshTerrain();
  47. floor.SetFriction(1.0f);
  48. CreateBridge();
  49. CreateWall();
  50. CreateRubble();
  51. }
  52. else
  53. {
  54. // Load scene
  55. Ref<PhysicsScene> scene;
  56. if (!ObjectStreamIn::sReadObject((String("Assets/") + sSceneName + ".bof").c_str(), scene))
  57. FatalError("Failed to load scene");
  58. for (BodyCreationSettings &body : scene->GetBodies())
  59. body.mObjectLayer = Layers::NON_MOVING;
  60. scene->FixInvalidScales();
  61. scene->CreateBodies(mPhysicsSystem);
  62. }
  63. }
  64. void VehicleTest::CreateBridge()
  65. {
  66. const int cChainLength = 20;
  67. // Build a collision group filter that disables collision between adjacent bodies
  68. Ref<GroupFilterTable> group_filter = new GroupFilterTable(cChainLength);
  69. for (CollisionGroup::SubGroupID i = 0; i < cChainLength - 1; ++i)
  70. group_filter->DisableCollision(i, i + 1);
  71. Vec3 part_half_size = Vec3(2.5f, 0.25f, 1.0f);
  72. RefConst<Shape> part_shape = new BoxShape(part_half_size);
  73. Vec3 large_part_half_size = Vec3(2.5f, 0.25f, 22.5f);
  74. RefConst<Shape> large_part_shape = new BoxShape(large_part_half_size);
  75. Quat first_part_rot = Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-10.0f));
  76. Vec3 prev_pos = Vec3(-25, 7, 0);
  77. Body *prev_part = nullptr;
  78. for (int i = 0; i < cChainLength; ++i)
  79. {
  80. Vec3 pos = prev_pos + Vec3(0, 0, 2.0f * part_half_size.GetZ());
  81. Body &part = i == 0? *mBodyInterface->CreateBody(BodyCreationSettings(large_part_shape, pos - first_part_rot * Vec3(0, large_part_half_size.GetY() - part_half_size.GetY(), large_part_half_size.GetZ() - part_half_size.GetZ()), first_part_rot, EMotionType::Static, Layers::NON_MOVING))
  82. : *mBodyInterface->CreateBody(BodyCreationSettings(part_shape, pos, Quat::sIdentity(), i == 19? EMotionType::Static : EMotionType::Dynamic, i == 19? Layers::NON_MOVING : Layers::MOVING));
  83. part.SetCollisionGroup(CollisionGroup(group_filter, 1, CollisionGroup::SubGroupID(i)));
  84. part.SetFriction(1.0f);
  85. mBodyInterface->AddBody(part.GetID(), EActivation::Activate);
  86. if (prev_part != nullptr)
  87. {
  88. DistanceConstraintSettings dc;
  89. dc.mPoint1 = prev_pos + Vec3(-part_half_size.GetX(), 0, part_half_size.GetZ());
  90. dc.mPoint2 = pos + Vec3(-part_half_size.GetX(), 0, -part_half_size.GetZ());
  91. mPhysicsSystem->AddConstraint(dc.Create(*prev_part, part));
  92. dc.mPoint1 = prev_pos + Vec3(part_half_size.GetX(), 0, part_half_size.GetZ());
  93. dc.mPoint2 = pos + Vec3(part_half_size.GetX(), 0, -part_half_size.GetZ());
  94. mPhysicsSystem->AddConstraint(dc.Create(*prev_part, part));
  95. }
  96. prev_part = &part;
  97. prev_pos = pos;
  98. }
  99. }
  100. void VehicleTest::CreateWall()
  101. {
  102. RefConst<Shape> box_shape = new BoxShape(Vec3(0.5f, 0.5f, 0.5f));
  103. for (int i = 0; i < 3; ++i)
  104. for (int j = i / 2; j < 5 - (i + 1) / 2; ++j)
  105. {
  106. Vec3 position(2.0f + j * 1.0f + (i & 1? 0.5f : 0.0f), 2.0f + i * 1.0f, 10.0f);
  107. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  108. }
  109. }
  110. void VehicleTest::CreateRubble()
  111. {
  112. // Flat and light objects
  113. RefConst<Shape> box_shape = new BoxShape(Vec3(0.5f, 0.1f, 0.5f));
  114. for (int i = 0; i < 5; ++i)
  115. for (int j = 0; j < 5; ++j)
  116. {
  117. Vec3 position(-5.0f + j, 2.0f + i * 0.2f, 10.0f + 0.5f * i);
  118. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  119. }
  120. // Light convex shapes
  121. default_random_engine random;
  122. uniform_real_distribution<float> hull_size(0.2f, 0.4f);
  123. for (int i = 0; i < 10; ++i)
  124. for (int j = 0; j < 10; ++j)
  125. {
  126. // Create random points
  127. Array<Vec3> points;
  128. for (int k = 0; k < 20; ++k)
  129. points.push_back(hull_size(random) * Vec3::sRandom(random));
  130. mBodyInterface->CreateAndAddBody(BodyCreationSettings(new ConvexHullShapeSettings(points), Vec3(-5.0f + 0.5f * j, 2.0f, 15.0f + 0.5f * i), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  131. }
  132. }
  133. void VehicleTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  134. {
  135. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  136. UIElement *scene_name = inUI->CreateMenu();
  137. for (uint i = 0; i < size(sScenes); ++i)
  138. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  139. inUI->ShowMenu(scene_name);
  140. });
  141. }