VehicleTest.cpp 11 KB

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