VehicleTest.cpp 4.4 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 <Physics/Constraints/DistanceConstraint.h>
  6. #include <Physics/Collision/Shape/BoxShape.h>
  7. #include <Physics/Collision/GroupFilterTable.h>
  8. #include <Physics/Body/BodyCreationSettings.h>
  9. #include <Physics/PhysicsScene.h>
  10. #include <Layers.h>
  11. #include <Application/DebugUI.h>
  12. #include <Utils/Log.h>
  13. JPH_IMPLEMENT_RTTI_VIRTUAL(VehicleTest)
  14. {
  15. JPH_ADD_BASE_CLASS(VehicleTest, Test)
  16. }
  17. const char *VehicleTest::sScenes[] =
  18. {
  19. "Flat",
  20. "Playground",
  21. "Terrain1",
  22. };
  23. const char *VehicleTest::sSceneName = "Playground";
  24. void VehicleTest::Initialize()
  25. {
  26. if (strcmp(sSceneName, "Flat") == 0)
  27. {
  28. // Flat test floor
  29. 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));
  30. floor.SetFriction(1.0f);
  31. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  32. }
  33. else if (strcmp(sSceneName, "Playground") == 0)
  34. {
  35. // Scene with hilly terrain and some objects to drive into
  36. Body &floor = CreateMeshTerrain();
  37. floor.SetFriction(1.0f);
  38. CreateBridge();
  39. CreateWall();
  40. }
  41. else
  42. {
  43. // Load scene
  44. Ref<PhysicsScene> scene;
  45. if (!ObjectStreamIn::sReadObject((string("Assets/") + sSceneName + ".bof").c_str(), scene))
  46. FatalError("Failed to load scene");
  47. scene->FixInvalidScales();
  48. scene->CreateBodies(mPhysicsSystem);
  49. }
  50. }
  51. void VehicleTest::CreateBridge()
  52. {
  53. const int cChainLength = 20;
  54. // Build a collision group filter that disables collision between adjacent bodies
  55. Ref<GroupFilterTable> group_filter = new GroupFilterTable(cChainLength);
  56. for (CollisionGroup::SubGroupID i = 0; i < cChainLength - 1; ++i)
  57. group_filter->DisableCollision(i, i + 1);
  58. Vec3 part_half_size = Vec3(2.5f, 0.25f, 1.0f);
  59. RefConst<Shape> part_shape = new BoxShape(part_half_size);
  60. Vec3 large_part_half_size = Vec3(2.5f, 0.25f, 22.5f);
  61. RefConst<Shape> large_part_shape = new BoxShape(large_part_half_size);
  62. Quat first_part_rot = Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-10.0f));
  63. Vec3 prev_pos = Vec3(-25, 7, 0);
  64. Body *prev_part = nullptr;
  65. for (int i = 0; i < cChainLength; ++i)
  66. {
  67. Vec3 pos = prev_pos + Vec3(0, 0, 2.0f * part_half_size.GetZ());
  68. 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))
  69. : *mBodyInterface->CreateBody(BodyCreationSettings(part_shape, pos, Quat::sIdentity(), i == 19? EMotionType::Static : EMotionType::Dynamic, i == 19? Layers::NON_MOVING : Layers::MOVING));
  70. #ifdef _DEBUG
  71. part.SetDebugName("Bridge Part " + ConvertToString(i));
  72. #endif
  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. }