VehicleTest.cpp 5.5 KB

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