TrackedVehicleController.cpp 21 KB

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