TrackedVehicleController.cpp 20 KB

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