VehicleTest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #include <Renderer/DebugRendererImp.h>
  17. JPH_IMPLEMENT_RTTI_VIRTUAL(VehicleTest)
  18. {
  19. JPH_ADD_BASE_CLASS(VehicleTest, Test)
  20. }
  21. const char *VehicleTest::sScenes[] =
  22. {
  23. "Flat",
  24. "Steep Slope",
  25. "Playground",
  26. "Terrain1",
  27. };
  28. const char *VehicleTest::sSceneName = "Playground";
  29. void VehicleTest::Initialize()
  30. {
  31. if (strcmp(sSceneName, "Flat") == 0)
  32. {
  33. // Flat test floor
  34. 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));
  35. floor.SetFriction(1.0f);
  36. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  37. // Load a race track to have something to assess speed and steering behavior
  38. LoadRaceTrack("Assets/Racetracks/Zandvoort.csv");
  39. }
  40. else if (strcmp(sSceneName, "Steep Slope") == 0)
  41. {
  42. // Steep slope test floor (20 degrees = 36% grade)
  43. 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));
  44. floor.SetFriction(1.0f);
  45. mBodyInterface->AddBody(floor.GetID(), EActivation::DontActivate);
  46. }
  47. else if (strcmp(sSceneName, "Playground") == 0)
  48. {
  49. // Scene with hilly terrain and some objects to drive into
  50. Body &floor = CreateMeshTerrain();
  51. floor.SetFriction(1.0f);
  52. CreateBridge();
  53. CreateWall();
  54. CreateRubble();
  55. }
  56. else
  57. {
  58. // Load scene
  59. Ref<PhysicsScene> scene;
  60. if (!ObjectStreamIn::sReadObject((String("Assets/") + sSceneName + ".bof").c_str(), scene))
  61. FatalError("Failed to load scene");
  62. for (BodyCreationSettings &body : scene->GetBodies())
  63. body.mObjectLayer = Layers::NON_MOVING;
  64. scene->FixInvalidScales();
  65. scene->CreateBodies(mPhysicsSystem);
  66. }
  67. }
  68. void VehicleTest::CreateBridge()
  69. {
  70. const int cChainLength = 20;
  71. // Build a collision group filter that disables collision between adjacent bodies
  72. Ref<GroupFilterTable> group_filter = new GroupFilterTable(cChainLength);
  73. for (CollisionGroup::SubGroupID i = 0; i < cChainLength - 1; ++i)
  74. group_filter->DisableCollision(i, i + 1);
  75. Vec3 part_half_size = Vec3(2.5f, 0.25f, 1.0f);
  76. RefConst<Shape> part_shape = new BoxShape(part_half_size);
  77. Vec3 large_part_half_size = Vec3(2.5f, 0.25f, 22.5f);
  78. RefConst<Shape> large_part_shape = new BoxShape(large_part_half_size);
  79. Quat first_part_rot = Quat::sRotation(Vec3::sAxisX(), DegreesToRadians(-10.0f));
  80. RVec3 prev_pos(-25, 7, 0);
  81. Body *prev_part = nullptr;
  82. for (int i = 0; i < cChainLength; ++i)
  83. {
  84. RVec3 pos = prev_pos + Vec3(0, 0, 2.0f * part_half_size.GetZ());
  85. 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))
  86. : *mBodyInterface->CreateBody(BodyCreationSettings(part_shape, pos, Quat::sIdentity(), i == 19? EMotionType::Static : EMotionType::Dynamic, i == 19? Layers::NON_MOVING : Layers::MOVING));
  87. part.SetCollisionGroup(CollisionGroup(group_filter, 1, CollisionGroup::SubGroupID(i)));
  88. part.SetFriction(1.0f);
  89. mBodyInterface->AddBody(part.GetID(), EActivation::Activate);
  90. if (prev_part != nullptr)
  91. {
  92. DistanceConstraintSettings dc;
  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. dc.mPoint1 = prev_pos + Vec3(part_half_size.GetX(), 0, part_half_size.GetZ());
  97. dc.mPoint2 = pos + Vec3(part_half_size.GetX(), 0, -part_half_size.GetZ());
  98. mPhysicsSystem->AddConstraint(dc.Create(*prev_part, part));
  99. }
  100. prev_part = &part;
  101. prev_pos = pos;
  102. }
  103. }
  104. void VehicleTest::CreateWall()
  105. {
  106. RefConst<Shape> box_shape = new BoxShape(Vec3(0.5f, 0.5f, 0.5f));
  107. for (int i = 0; i < 3; ++i)
  108. for (int j = i / 2; j < 5 - (i + 1) / 2; ++j)
  109. {
  110. RVec3 position(2.0f + j * 1.0f + (i & 1? 0.5f : 0.0f), 2.0f + i * 1.0f, 10.0f);
  111. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  112. }
  113. }
  114. void VehicleTest::CreateRubble()
  115. {
  116. // Flat and light objects
  117. RefConst<Shape> box_shape = new BoxShape(Vec3(0.5f, 0.1f, 0.5f));
  118. for (int i = 0; i < 5; ++i)
  119. for (int j = 0; j < 5; ++j)
  120. {
  121. RVec3 position(-5.0f + j, 2.0f + i * 0.2f, 10.0f + 0.5f * i);
  122. mBodyInterface->CreateAndAddBody(BodyCreationSettings(box_shape, position, Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING), EActivation::Activate);
  123. }
  124. // Light convex shapes
  125. default_random_engine random;
  126. uniform_real_distribution<float> hull_size(0.2f, 0.4f);
  127. for (int i = 0; i < 10; ++i)
  128. for (int j = 0; j < 10; ++j)
  129. {
  130. // Create random points
  131. Array<Vec3> points;
  132. for (int k = 0; k < 20; ++k)
  133. points.push_back(hull_size(random) * Vec3::sRandom(random));
  134. 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);
  135. }
  136. }
  137. void VehicleTest::LoadRaceTrack(const char *inFileName)
  138. {
  139. // Open the track file
  140. std::ifstream stream;
  141. stream.open(inFileName, std::ifstream::in);
  142. if (!stream.is_open())
  143. return;
  144. // Ignore header line
  145. String line;
  146. std::getline(stream, line);
  147. // Read coordinates
  148. struct Segment
  149. {
  150. RVec3 mCenter;
  151. float mWidthLeft;
  152. float mWidthRight;
  153. };
  154. Array<Segment> segments;
  155. Real x, y;
  156. float wl, wr;
  157. char c;
  158. RVec3 track_center = RVec3::sZero();
  159. while (stream >> x >> c >> y >> c >> wl >> c >> wr)
  160. {
  161. RVec3 center(x, 0, -y);
  162. segments.push_back({ center, wl, wr });
  163. track_center += center;
  164. }
  165. if (!segments.empty())
  166. track_center /= (float)segments.size();
  167. // Convert to line segments
  168. RVec3 prev_tleft = RVec3::sZero(), prev_tright = RVec3::sZero();
  169. for (size_t i = 0; i < segments.size(); ++i)
  170. {
  171. const Segment &segment = segments[i];
  172. const Segment &next_segment = segments[(i + 1) % segments.size()];
  173. // Calculate left and right point of the track
  174. Vec3 fwd = Vec3(next_segment.mCenter - segment.mCenter);
  175. Vec3 right = fwd.Cross(Vec3::sAxisY()).Normalized();
  176. RVec3 tcenter = segment.mCenter - track_center + Vec3(0, 0.1f, 0); // Put a bit above the floor to avoid z fighting
  177. RVec3 tleft = tcenter - right * segment.mWidthLeft;
  178. RVec3 tright = tcenter + right * segment.mWidthRight;
  179. mTrackData.push_back({ tleft, tright });
  180. // Connect left and right point with the previous left and right point
  181. if (i > 0)
  182. {
  183. mTrackData.push_back({ prev_tleft, tleft });
  184. mTrackData.push_back({ prev_tright, tright });
  185. }
  186. prev_tleft = tleft;
  187. prev_tright = tright;
  188. }
  189. }
  190. void VehicleTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  191. {
  192. // Render the track
  193. for (const Line &l : mTrackData)
  194. mDebugRenderer->DrawLine(l.mStart, l.mEnd, Color::sBlack);
  195. }
  196. void VehicleTest::CreateSettingsMenu(DebugUI *inUI, UIElement *inSubMenu)
  197. {
  198. inUI->CreateTextButton(inSubMenu, "Select Scene", [this, inUI]() {
  199. UIElement *scene_name = inUI->CreateMenu();
  200. for (uint i = 0; i < size(sScenes); ++i)
  201. inUI->CreateTextButton(scene_name, sScenes[i], [this, i]() { sSceneName = sScenes[i]; RestartTest(); });
  202. inUI->ShowMenu(scene_name);
  203. });
  204. }