TrackedVehicleController.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Vehicle/TrackedVehicleController.h>
  5. #include <Jolt/Physics/PhysicsSystem.h>
  6. #include <Jolt/ObjectStream/TypeDeclarations.h>
  7. #include <Jolt/Core/StreamIn.h>
  8. #include <Jolt/Core/StreamOut.h>
  9. JPH_NAMESPACE_BEGIN
  10. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(TrackedVehicleControllerSettings)
  11. {
  12. JPH_ADD_BASE_CLASS(TrackedVehicleControllerSettings, VehicleControllerSettings)
  13. JPH_ADD_ATTRIBUTE(TrackedVehicleControllerSettings, mEngine)
  14. JPH_ADD_ATTRIBUTE(TrackedVehicleControllerSettings, mTransmission)
  15. JPH_ADD_ATTRIBUTE(TrackedVehicleControllerSettings, mTracks)
  16. }
  17. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(WheelSettingsTV)
  18. {
  19. JPH_ADD_ATTRIBUTE(WheelSettingsTV, mLongitudinalFriction)
  20. JPH_ADD_ATTRIBUTE(WheelSettingsTV, mLateralFriction)
  21. }
  22. void WheelSettingsTV::SaveBinaryState(StreamOut &inStream) const
  23. {
  24. inStream.Write(mLongitudinalFriction);
  25. inStream.Write(mLateralFriction);
  26. }
  27. void WheelSettingsTV::RestoreBinaryState(StreamIn &inStream)
  28. {
  29. inStream.Read(mLongitudinalFriction);
  30. inStream.Read(mLateralFriction);
  31. }
  32. WheelTV::WheelTV(const WheelSettingsTV &inSettings) :
  33. Wheel(inSettings)
  34. {
  35. }
  36. void WheelTV::CalculateAngularVelocity(const VehicleConstraint &inConstraint)
  37. {
  38. const WheelSettingsTV *settings = GetSettings();
  39. const Wheels &wheels = inConstraint.GetWheels();
  40. const VehicleTrack &track = static_cast<const TrackedVehicleController *>(inConstraint.GetController())->GetTracks()[mTrackIndex];
  41. // Calculate angular velocity of this wheel
  42. mAngularVelocity = track.mAngularVelocity * wheels[track.mDrivenWheel]->GetSettings()->mRadius / settings->mRadius;
  43. }
  44. void WheelTV::Update(float inDeltaTime, const VehicleConstraint &inConstraint)
  45. {
  46. CalculateAngularVelocity(inConstraint);
  47. // Update rotation of wheel
  48. mAngle = fmod(mAngle + mAngularVelocity * inDeltaTime, 2.0f * JPH_PI);
  49. // Reset brake impulse, will be set during post collision again
  50. mBrakeImpulse = 0.0f;
  51. if (mContactBody != nullptr)
  52. {
  53. // Friction at the point of this wheel between track and floor
  54. const WheelSettingsTV *settings = GetSettings();
  55. mCombinedLongitudinalFriction = sqrt(settings->mLongitudinalFriction * mContactBody->GetFriction());
  56. mCombinedLateralFriction = sqrt(settings->mLateralFriction * mContactBody->GetFriction());
  57. }
  58. else
  59. {
  60. // No collision
  61. mCombinedLongitudinalFriction = mCombinedLateralFriction = 0.0f;
  62. }
  63. }
  64. VehicleController *TrackedVehicleControllerSettings::ConstructController(VehicleConstraint &inConstraint) const
  65. {
  66. return new TrackedVehicleController(*this, inConstraint);
  67. }
  68. TrackedVehicleControllerSettings::TrackedVehicleControllerSettings()
  69. {
  70. // Numbers guestimated from: https://en.wikipedia.org/wiki/M1_Abrams
  71. mEngine.mMinRPM = 500.0f;
  72. mEngine.mMaxRPM = 4000.0f;
  73. mEngine.mMaxTorque = 500.0f; // Note actual torque for M1 is around 5000 but we need a reduced mass in order to keep the simulation sane
  74. mTransmission.mShiftDownRPM = 1000.0f;
  75. mTransmission.mShiftUpRPM = 3500.0f;
  76. mTransmission.mGearRatios = { 4.0f, 3.0f, 2.0f, 1.0f };
  77. mTransmission.mReverseGearRatios = { -4.0f, -3.0f };
  78. }
  79. void TrackedVehicleControllerSettings::SaveBinaryState(StreamOut &inStream) const
  80. {
  81. mEngine.SaveBinaryState(inStream);
  82. mTransmission.SaveBinaryState(inStream);
  83. for (const VehicleTrackSettings &t : mTracks)
  84. t.SaveBinaryState(inStream);
  85. }
  86. void TrackedVehicleControllerSettings::RestoreBinaryState(StreamIn &inStream)
  87. {
  88. mEngine.RestoreBinaryState(inStream);
  89. mTransmission.RestoreBinaryState(inStream);
  90. for (VehicleTrackSettings &t : mTracks)
  91. t.RestoreBinaryState(inStream);
  92. }
  93. TrackedVehicleController::TrackedVehicleController(const TrackedVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint) :
  94. VehicleController(inConstraint)
  95. {
  96. // Copy engine settings
  97. static_cast<VehicleEngineSettings &>(mEngine) = inSettings.mEngine;
  98. JPH_ASSERT(inSettings.mEngine.mMinRPM >= 0.0f);
  99. JPH_ASSERT(inSettings.mEngine.mMinRPM <= inSettings.mEngine.mMaxRPM);
  100. // Copy transmission settings
  101. static_cast<VehicleTransmissionSettings &>(mTransmission) = inSettings.mTransmission;
  102. #ifdef JPH_ENABLE_ASSERTS
  103. for (float r : inSettings.mTransmission.mGearRatios)
  104. JPH_ASSERT(r > 0.0f);
  105. for (float r : inSettings.mTransmission.mReverseGearRatios)
  106. JPH_ASSERT(r < 0.0f);
  107. #endif // JPH_ENABLE_ASSERTS
  108. JPH_ASSERT(inSettings.mTransmission.mSwitchTime >= 0.0f);
  109. JPH_ASSERT(inSettings.mTransmission.mShiftDownRPM > 0.0f);
  110. JPH_ASSERT(inSettings.mTransmission.mMode != ETransmissionMode::Auto || inSettings.mTransmission.mShiftUpRPM < inSettings.mEngine.mMaxRPM);
  111. JPH_ASSERT(inSettings.mTransmission.mShiftUpRPM > inSettings.mTransmission.mShiftDownRPM);
  112. // Copy track settings
  113. for (uint i = 0; i < size(mTracks); ++i)
  114. {
  115. const VehicleTrackSettings &d = inSettings.mTracks[i];
  116. static_cast<VehicleTrackSettings &>(mTracks[i]) = d;
  117. JPH_ASSERT(d.mInertia >= 0.0f);
  118. JPH_ASSERT(d.mAngularDamping >= 0.0f);
  119. JPH_ASSERT(d.mMaxBrakeTorque >= 0.0f);
  120. JPH_ASSERT(d.mDifferentialRatio > 0.0f);
  121. }
  122. }
  123. void TrackedVehicleController::PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem)
  124. {
  125. Wheels &wheels = mConstraint.GetWheels();
  126. // Fill in track index
  127. for (size_t t = 0; t < size(mTracks); ++t)
  128. for (uint w : mTracks[t].mWheels)
  129. static_cast<WheelTV *>(wheels[w])->mTrackIndex = (uint)t;
  130. // Angular damping: dw/dt = -c * w
  131. // Solution: w(t) = w(0) * e^(-c * t) or w2 = w1 * e^(-c * dt)
  132. // Taylor expansion of e^(-c * dt) = 1 - c * dt + ...
  133. // Since dt is usually in the order of 1/60 and c is a low number too this approximation is good enough
  134. for (VehicleTrack &t : mTracks)
  135. t.mAngularVelocity *= max(0.0f, 1.0f - t.mAngularDamping * inDeltaTime);
  136. }
  137. void TrackedVehicleController::SyncLeftRightTracks()
  138. {
  139. // Apply left to right ratio according to track inertias
  140. VehicleTrack &tl = mTracks[(int)ETrackSide::Left];
  141. VehicleTrack &tr = mTracks[(int)ETrackSide::Right];
  142. if (mLeftRatio * mRightRatio > 0.0f)
  143. {
  144. // Solve: (tl.mAngularVelocity + dl) / (tr.mAngularVelocity + dr) = mLeftRatio / mRightRatio and dl * tr.mInertia = -dr * tl.mInertia, where dl/dr are the delta angular velocities for left and right tracks
  145. float impulse = (mLeftRatio * tr.mAngularVelocity - mRightRatio * tl.mAngularVelocity) / (mLeftRatio * tr.mInertia + mRightRatio * tl.mInertia);
  146. tl.mAngularVelocity += impulse * tl.mInertia;
  147. tr.mAngularVelocity -= impulse * tr.mInertia;
  148. }
  149. else
  150. {
  151. // Solve: (tl.mAngularVelocity + dl) / (tr.mAngularVelocity + dr) = mLeftRatio / mRightRatio and dl * tr.mInertia = dr * tl.mInertia, where dl/dr are the delta angular velocities for left and right tracks
  152. float impulse = (mLeftRatio * tr.mAngularVelocity - mRightRatio * tl.mAngularVelocity) / (mRightRatio * tl.mInertia - mLeftRatio * tr.mInertia);
  153. tl.mAngularVelocity += impulse * tl.mInertia;
  154. tr.mAngularVelocity += impulse * tr.mInertia;
  155. }
  156. }
  157. void TrackedVehicleController::PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem)
  158. {
  159. JPH_PROFILE_FUNCTION();
  160. Wheels &wheels = mConstraint.GetWheels();
  161. // Update wheel angle, do this before applying torque to the wheels (as friction will slow them down again)
  162. for (Wheel *w_base : wheels)
  163. {
  164. WheelTV *w = static_cast<WheelTV *>(w_base);
  165. w->Update(inDeltaTime, mConstraint);
  166. }
  167. // First calculate engine speed based on speed of all wheels
  168. bool can_engine_apply_torque = false;
  169. if (mTransmission.GetCurrentGear() != 0 && mTransmission.GetClutchFriction() > 1.0e-3f)
  170. {
  171. float transmission_ratio = mTransmission.GetCurrentRatio();
  172. bool forward = transmission_ratio >= 0.0f;
  173. float fastest_wheel_speed = forward? -FLT_MAX : FLT_MAX;
  174. for (const VehicleTrack &t : mTracks)
  175. {
  176. if (forward)
  177. fastest_wheel_speed = max(fastest_wheel_speed, t.mAngularVelocity * t.mDifferentialRatio);
  178. else
  179. fastest_wheel_speed = min(fastest_wheel_speed, t.mAngularVelocity * t.mDifferentialRatio);
  180. for (uint w : t.mWheels)
  181. if (wheels[w]->HasContact())
  182. {
  183. can_engine_apply_torque = true;
  184. break;
  185. }
  186. }
  187. // Update RPM only if the tracks are connected to the engine
  188. if (fastest_wheel_speed > -FLT_MAX && fastest_wheel_speed < FLT_MAX)
  189. mEngine.SetCurrentRPM(fastest_wheel_speed * mTransmission.GetCurrentRatio() * VehicleEngine::cAngularVelocityToRPM);
  190. mEngine.SetCurrentRPM(Clamp(mEngine.GetCurrentRPM(), mEngine.mMinRPM, mEngine.mMaxRPM));
  191. }
  192. else
  193. {
  194. // Engine not connected to tracks, update RPM based on engine inertia alone
  195. mEngine.UpdateRPM(inDeltaTime, abs(mForwardInput));
  196. }
  197. // Update transmission
  198. // Note: only allow switching gears up when the tracks are rolling in the same direction
  199. mTransmission.Update(inDeltaTime, mEngine.GetCurrentRPM(), mForwardInput, mLeftRatio * mRightRatio > 0.0f && can_engine_apply_torque);
  200. // Calculate the amount of torque the transmission gives to the differentials
  201. float transmission_ratio = mTransmission.GetCurrentRatio();
  202. float transmission_torque = mTransmission.GetClutchFriction() * transmission_ratio * mEngine.GetTorque(abs(mForwardInput));
  203. if (transmission_torque != 0.0f)
  204. {
  205. // Apply the transmission torque to the wheels
  206. for (uint i = 0; i < size(mTracks); ++i)
  207. {
  208. VehicleTrack &t = mTracks[i];
  209. // Get wheel rotation ratio for this track
  210. float ratio = i == 0? mLeftRatio : mRightRatio;
  211. // Calculate the max angular velocity of the driven wheel of the track given current engine RPM
  212. // Note this adds 0.1% slop to avoid numerical accuracy issues
  213. float track_max_angular_velocity = mEngine.GetCurrentRPM() / (transmission_ratio * t.mDifferentialRatio * ratio * VehicleEngine::cAngularVelocityToRPM) * 1.001f;
  214. // Calculate torque on the driven wheel
  215. float differential_torque = t.mDifferentialRatio * ratio * transmission_torque;
  216. // Apply torque to driven wheel
  217. if (t.mAngularVelocity * track_max_angular_velocity < 0.0f || abs(t.mAngularVelocity) < abs(track_max_angular_velocity))
  218. t.mAngularVelocity += differential_torque * inDeltaTime / t.mInertia;
  219. }
  220. }
  221. // Ensure that we have the correct ratio between the two tracks
  222. SyncLeftRightTracks();
  223. // Braking
  224. for (VehicleTrack &t : mTracks)
  225. {
  226. // Calculate brake torque
  227. float brake_torque = mBrakeInput * t.mMaxBrakeTorque;
  228. if (brake_torque > 0.0f)
  229. {
  230. // Calculate how much torque is needed to stop the track from rotating in this time step
  231. float brake_torque_to_lock_track = abs(t.mAngularVelocity) * t.mInertia / inDeltaTime;
  232. if (brake_torque > brake_torque_to_lock_track)
  233. {
  234. // Wheels are locked
  235. t.mAngularVelocity = 0.0f;
  236. brake_torque -= brake_torque_to_lock_track;
  237. }
  238. else
  239. {
  240. // Slow down the track
  241. t.mAngularVelocity -= Sign(t.mAngularVelocity) * brake_torque * inDeltaTime / t.mInertia;
  242. }
  243. }
  244. if (brake_torque > 0.0f)
  245. {
  246. // Sum the radius of all wheels touching the floor
  247. float total_radius = 0.0f;
  248. for (uint wheel_index : t.mWheels)
  249. {
  250. const WheelTV *w = static_cast<WheelTV *>(wheels[wheel_index]);
  251. if (w->HasContact())
  252. total_radius += w->GetSettings()->mRadius;
  253. }
  254. if (total_radius > 0.0f)
  255. {
  256. brake_torque /= total_radius;
  257. for (uint wheel_index : t.mWheels)
  258. {
  259. WheelTV *w = static_cast<WheelTV *>(wheels[wheel_index]);
  260. if (w->HasContact())
  261. {
  262. // Impulse: p = F * dt = Torque / Wheel_Radius * dt, Torque = Total_Torque * Wheel_Radius / Summed_Radius => p = Total_Torque * dt / Summed_Radius
  263. w->mBrakeImpulse = brake_torque * inDeltaTime;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. // Update wheel angular velocity based on that of the track
  270. for (Wheel *w_base : wheels)
  271. {
  272. WheelTV *w = static_cast<WheelTV *>(w_base);
  273. w->CalculateAngularVelocity(mConstraint);
  274. }
  275. }
  276. bool TrackedVehicleController::SolveLongitudinalAndLateralConstraints(float inDeltaTime)
  277. {
  278. bool impulse = false;
  279. for (Wheel *w_base : mConstraint.GetWheels())
  280. if (w_base->HasContact())
  281. {
  282. WheelTV *w = static_cast<WheelTV *>(w_base);
  283. const WheelSettingsTV *settings = w->GetSettings();
  284. VehicleTrack &track = mTracks[w->mTrackIndex];
  285. // Calculate max impulse that we can apply on the ground
  286. float max_longitudinal_friction_impulse = w->mCombinedLongitudinalFriction * w->GetSuspensionLambda();
  287. // Calculate relative velocity between wheel contact point and floor in longitudinal direction
  288. Vec3 relative_velocity = mConstraint.GetVehicleBody()->GetPointVelocity(w->GetContactPosition()) - w->GetContactPointVelocity();
  289. float relative_longitudinal_velocity = relative_velocity.Dot(w->GetContactLongitudinal());
  290. // Calculate brake force to apply
  291. float min_longitudinal_impulse, max_longitudinal_impulse;
  292. if (w->mBrakeImpulse != 0.0f)
  293. {
  294. // Limit brake force by max tire friction
  295. float brake_impulse = min(w->mBrakeImpulse, max_longitudinal_friction_impulse);
  296. // Check which direction the brakes should be applied (we don't want to apply an impulse that would accelerate the vehicle)
  297. if (relative_longitudinal_velocity >= 0.0f)
  298. {
  299. min_longitudinal_impulse = -brake_impulse;
  300. max_longitudinal_impulse = 0.0f;
  301. }
  302. else
  303. {
  304. min_longitudinal_impulse = 0.0f;
  305. max_longitudinal_impulse = brake_impulse;
  306. }
  307. // Longitudinal impulse, note that we assume that once the wheels are locked that the brakes have more than enough torque to keep the wheels locked so we exclude any rotation deltas
  308. impulse |= w->SolveLongitudinalConstraintPart(mConstraint, min_longitudinal_impulse, max_longitudinal_impulse);
  309. }
  310. else
  311. {
  312. // Assume we want to apply an angular impulse that makes the delta velocity between track and ground zero in one time step, calculate the amount of linear impulse needed to do that
  313. float desired_angular_velocity = relative_longitudinal_velocity / settings->mRadius;
  314. float linear_impulse = (track.mAngularVelocity - desired_angular_velocity) * track.mInertia / settings->mRadius;
  315. // Limit the impulse by max track friction
  316. min_longitudinal_impulse = max_longitudinal_impulse = w->GetLongitudinalLambda() + Sign(linear_impulse) * min(abs(linear_impulse), max_longitudinal_friction_impulse);
  317. // Longitudinal impulse
  318. float prev_lambda = w->GetLongitudinalLambda();
  319. impulse |= w->SolveLongitudinalConstraintPart(mConstraint, min_longitudinal_impulse, max_longitudinal_impulse);
  320. // Update the angular velocity of the track according to the lambda that was applied
  321. track.mAngularVelocity -= (w->GetLongitudinalLambda() - prev_lambda) * settings->mRadius / track.mInertia;
  322. SyncLeftRightTracks();
  323. }
  324. }
  325. for (Wheel *w_base : mConstraint.GetWheels())
  326. if (w_base->HasContact())
  327. {
  328. WheelTV *w = static_cast<WheelTV *>(w_base);
  329. // Update angular velocity of wheel for the next iteration
  330. w->CalculateAngularVelocity(mConstraint);
  331. // Lateral friction
  332. float max_lateral_friction_impulse = w->mCombinedLateralFriction * w->GetSuspensionLambda();
  333. impulse |= w->SolveLateralConstraintPart(mConstraint, -max_lateral_friction_impulse, max_lateral_friction_impulse);
  334. }
  335. return impulse;
  336. }
  337. #ifdef JPH_DEBUG_RENDERER
  338. void TrackedVehicleController::Draw(DebugRenderer *inRenderer) const
  339. {
  340. // Draw RPM
  341. Body *body = mConstraint.GetVehicleBody();
  342. Vec3 rpm_meter_up = body->GetRotation() * mConstraint.GetLocalUp();
  343. Vec3 rpm_meter_pos = body->GetPosition() + body->GetRotation() * mRPMMeterPosition;
  344. Vec3 rpm_meter_fwd = body->GetRotation() * mConstraint.GetLocalForward();
  345. mEngine.DrawRPM(inRenderer, rpm_meter_pos, rpm_meter_fwd, rpm_meter_up, mRPMMeterSize, mTransmission.mShiftDownRPM, mTransmission.mShiftUpRPM);
  346. // Draw current vehicle state
  347. string status = StringFormat("Forward: %.1f, LRatio: %.1f, RRatio: %.1f, Brake: %.1f\n"
  348. "Gear: %d, Clutch: %.1f, EngineRPM: %.0f, V: %.1f km/h",
  349. (double)mForwardInput, (double)mLeftRatio, (double)mRightRatio, (double)mBrakeInput,
  350. mTransmission.GetCurrentGear(), (double)mTransmission.GetClutchFriction(), (double)mEngine.GetCurrentRPM(), (double)body->GetLinearVelocity().Length() * 3.6);
  351. inRenderer->DrawText3D(body->GetPosition(), status, Color::sWhite, mConstraint.GetDrawConstraintSize());
  352. for (const VehicleTrack &t : mTracks)
  353. {
  354. const WheelTV *w = static_cast<const WheelTV *>(mConstraint.GetWheels()[t.mDrivenWheel]);
  355. const WheelSettings *settings = w->GetSettings();
  356. // Calculate where the suspension attaches to the body in world space
  357. Vec3 ws_position = body->GetCenterOfMassPosition() + body->GetRotation() * (settings->mPosition - body->GetShape()->GetCenterOfMass());
  358. DebugRenderer::sInstance->DrawText3D(ws_position, StringFormat("W: %.1f", (double)t.mAngularVelocity), Color::sWhite, 0.1f);
  359. }
  360. for (const Wheel *w_base : mConstraint.GetWheels())
  361. {
  362. const WheelTV *w = static_cast<const WheelTV *>(w_base);
  363. const WheelSettings *settings = w->GetSettings();
  364. // Calculate where the suspension attaches to the body in world space
  365. Vec3 ws_position = body->GetCenterOfMassPosition() + body->GetRotation() * (settings->mPosition - body->GetShape()->GetCenterOfMass());
  366. if (w->HasContact())
  367. {
  368. // Draw contact
  369. inRenderer->DrawLine(ws_position, w->GetContactPosition(), w->HasHitHardPoint()? Color::sRed : Color::sGreen); // Red if we hit the 'max up' limit
  370. inRenderer->DrawLine(w->GetContactPosition(), w->GetContactPosition() + w->GetContactNormal(), Color::sYellow);
  371. inRenderer->DrawLine(w->GetContactPosition(), w->GetContactPosition() + w->GetContactLongitudinal(), Color::sRed);
  372. inRenderer->DrawLine(w->GetContactPosition(), w->GetContactPosition() + w->GetContactLateral(), Color::sBlue);
  373. DebugRenderer::sInstance->DrawText3D(w->GetContactPosition(), StringFormat("S: %.2f", (double)w->GetSuspensionLength()), Color::sWhite, 0.1f);
  374. }
  375. else
  376. {
  377. // Draw 'no hit'
  378. Vec3 max_droop = body->GetRotation() * settings->mDirection * (settings->mSuspensionMaxLength + settings->mRadius);
  379. inRenderer->DrawLine(ws_position, ws_position + max_droop, Color::sYellow);
  380. }
  381. }
  382. }
  383. #endif // JPH_DEBUG_RENDERER
  384. void TrackedVehicleController::SaveState(StateRecorder &inStream) const
  385. {
  386. inStream.Write(mForwardInput);
  387. inStream.Write(mLeftRatio);
  388. inStream.Write(mRightRatio);
  389. inStream.Write(mBrakeInput);
  390. mEngine.SaveState(inStream);
  391. mTransmission.SaveState(inStream);
  392. for (const VehicleTrack &t : mTracks)
  393. t.SaveState(inStream);
  394. }
  395. void TrackedVehicleController::RestoreState(StateRecorder &inStream)
  396. {
  397. inStream.Read(mForwardInput);
  398. inStream.Read(mLeftRatio);
  399. inStream.Read(mRightRatio);
  400. inStream.Read(mBrakeInput);
  401. mEngine.RestoreState(inStream);
  402. mTransmission.RestoreState(inStream);
  403. for (VehicleTrack &t : mTracks)
  404. t.RestoreState(inStream);
  405. }
  406. JPH_NAMESPACE_END