VehicleTest.cpp 6.0 KB

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