TrackedVehicleController.cpp 19 KB

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