VehicleTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/GroupFilterTable.h>
  8. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  9. #include <Jolt/Physics/PhysicsScene.h>
  10. #include <Jolt/ObjectStream/ObjectStreamIn.h>
  11. #include <Layers.h>
  12. #include <Application/DebugUI.h>
  13. #include <Utils/Log.h>
  14. JPH_IMPLEMENT_RTTI_VIRTUAL(VehicleTest)
  15. {
  16. JPH_ADD_BASE_CLASS(VehicleTest, Test)
  17. }
  18. const char *VehicleTest::sScenes[] =
  19. {
  20. "Flat",
  21. "Playground",
  22. "Terrain1",
  23. };
  24. const char *VehicleTest::sSceneName = "Playground";
  25. void VehicleTest::Initialize()
  26. {
  27. if (strcmp(sSceneName, "Flat") == 0)
  28. {
  29. // Flat test floor
  30. 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));
  31. floor.SetFriction(1.0f);
  32. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  33. }
  34. else if (strcmp(sSceneName, "Playground") == 0)
  35. {
  36. // Scene with hilly terrain and some objects to drive into
  37. Body &floor = CreateMeshTerrain();
  38. floor.SetFriction(1.0f);
  39. CreateBridge();
  40. CreateWall();
  41. }
  42. else
  43. {
  44. // Load scene
  45. Ref<PhysicsScene> scene;
  46. if (!ObjectStreamIn::sReadObject((string("Assets/") + sSceneName + ".bof").c_str(), scene))
  47. FatalError("Failed to load scene");
  48. for (BodyCreationSettings &body : scene->GetBodies())
  49. body.mObjectLayer = Layers::NON_MOVING;
  50. scene->FixInvalidScales();
  51. scene->CreateBodies(mPhysicsSystem);
  52. }
  53. }
  54. void VehicleTest::CreateBridge()
  55. {
  56. const int cChainLength = 20;
  57. // Build a collision group filter that disables collision between adjacent bodies
  58. Ref<GroupFilterTable> group_filter = new GroupFilterTable(cChainLength);
  59. for (CollisionGroup::SubGroupID i = 0; i < cChainLength - 1; ++i)
  60. group_filter->DisableCollision(i, i + 1);
  61. Vec3 part_half_size = Vec3(2.5f, 0.25f, 1.0f);
  62. RefConst<Shape> part_shape = new BoxShape(part_half_size);
  63. Vec3 large_part_half_size = Vec3(2.5f, 0.25f, 22.5f);
  64. RefConst<Shape> large_part_shape = new BoxShape(large_part_half_size);
  65. Quat first_part_rot = Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-10.0f));
  66. Vec3 prev_pos = Vec3(-25, 7, 0);
  67. Body *prev_part = nullptr;
  68. for (int i = 0; i < cChainLength; ++i)
  69. {
  70. Vec3 pos = prev_pos + Vec3(0, 0, 2.0f * part_half_size.GetZ());
  71. 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))
  72. : *mBodyInterface->CreateBody(BodyCreationSettings(part_shape, pos, Quat::sIdentity(), i == 19? EMotionType::Static : EMotionType::Dynamic, i == 19? Layers::NON_MOVING : Layers::MOVING));
  73. part.SetCollisionGroup(CollisionGroup(group_filter, 1, CollisionGroup::SubGroupID(i)));
  74. part.SetFriction(1.0f);
  75. mBodyInterface->AddBody(part.GetID(), EActivation::Activate);
  76. if (prev_part != nullptr)
  77. {
  78. DistanceConstraintSettings dc;
  79. dc.mPoint1 = prev_pos + Vec3(-part_half_size.GetX(), 0, part_half_size.GetZ());
  80. dc.mPoint2 = pos + Vec3(-part_half_size.GetX(), 0, -part_half_size.GetZ());
  81. mPhysicsSystem->AddConstraint(dc.Create(*prev_part, part));
  82. dc.mPoint1 = prev_pos + Vec3(part_half_size.GetX(), 0, part_half_size.GetZ());
  83. dc.mPoint2 = pos + Vec3(part_half_size.GetX(), 0, -part_half_size.GetZ());
  84. mPhysicsSystem->AddConstraint(dc.Create(*prev_part, part));
  85. }
  86. prev_part = &part;
  87. prev_pos = pos;
  88. }
  89. }
  90. void VehicleTest::CreateWall()
  91. {
  92. RefConst<Shape> box_shape = new BoxShape(Vec3(0.5f, 0.5f, 0.5f));
  93. for (int i = 0; i < 3; ++i)
  94. for (int j = i / 2; j < 5 - (i + 1) / 2; ++j)
  95. {
  96. Vec3 position(2.0f + j * 1.0f + (i & 1? 0.5f : 0.0f), 2.0f + i * 1.0f, 10.0f);
  97. Body &wall = *mBodyInterface->CreateBody(BodyCreationSettings(box_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  98. mBodyInterface->AddBody(wall.GetID(), EActivation::Activate);
  99. }
  100. }
  101. void VehicleTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  102. {
  103. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  104. UIElement *scene_name = inUI->CreateMenu();
  105. for (uint i = 0; i < size(sScenes); ++i)
  106. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  107. inUI->ShowMenu(scene_name);
  108. });
  109. }