WheeledVehicleController.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/Vehicle/WheeledVehicleController.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. #include <Jolt/Math/DynMatrix.h>
  10. #include <Jolt/Math/GaussianElimination.h>
  11. #ifdef JPH_DEBUG_RENDERER
  12. #include <Jolt/Renderer/DebugRenderer.h>
  13. #endif // JPH_DEBUG_RENDERER
  14. JPH_NAMESPACE_BEGIN
  15. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(WheeledVehicleControllerSettings)
  16. {
  17. JPH_ADD_BASE_CLASS(WheeledVehicleControllerSettings, VehicleControllerSettings)
  18. JPH_ADD_ATTRIBUTE(WheeledVehicleControllerSettings, mEngine)
  19. JPH_ADD_ATTRIBUTE(WheeledVehicleControllerSettings, mTransmission)
  20. JPH_ADD_ATTRIBUTE(WheeledVehicleControllerSettings, mDifferentials)
  21. JPH_ADD_ATTRIBUTE(WheeledVehicleControllerSettings, mDifferentialLimitedSlipRatio)
  22. }
  23. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(WheelSettingsWV)
  24. {
  25. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mInertia)
  26. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mAngularDamping)
  27. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mMaxSteerAngle)
  28. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mLongitudinalFriction)
  29. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mLateralFriction)
  30. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mMaxBrakeTorque)
  31. JPH_ADD_ATTRIBUTE(WheelSettingsWV, mMaxHandBrakeTorque)
  32. }
  33. WheelSettingsWV::WheelSettingsWV()
  34. {
  35. mLongitudinalFriction.Reserve(3);
  36. mLongitudinalFriction.AddPoint(0.0f, 0.0f);
  37. mLongitudinalFriction.AddPoint(0.06f, 1.2f);
  38. mLongitudinalFriction.AddPoint(0.2f, 1.0f);
  39. mLateralFriction.Reserve(3);
  40. mLateralFriction.AddPoint(0.0f, 0.0f);
  41. mLateralFriction.AddPoint(3.0f, 1.2f);
  42. mLateralFriction.AddPoint(20.0f, 1.0f);
  43. }
  44. void WheelSettingsWV::SaveBinaryState(StreamOut &inStream) const
  45. {
  46. inStream.Write(mInertia);
  47. inStream.Write(mAngularDamping);
  48. inStream.Write(mMaxSteerAngle);
  49. mLongitudinalFriction.SaveBinaryState(inStream);
  50. mLateralFriction.SaveBinaryState(inStream);
  51. inStream.Write(mMaxBrakeTorque);
  52. inStream.Write(mMaxHandBrakeTorque);
  53. }
  54. void WheelSettingsWV::RestoreBinaryState(StreamIn &inStream)
  55. {
  56. inStream.Read(mInertia);
  57. inStream.Read(mAngularDamping);
  58. inStream.Read(mMaxSteerAngle);
  59. mLongitudinalFriction.RestoreBinaryState(inStream);
  60. mLateralFriction.RestoreBinaryState(inStream);
  61. inStream.Read(mMaxBrakeTorque);
  62. inStream.Read(mMaxHandBrakeTorque);
  63. }
  64. WheelWV::WheelWV(const WheelSettingsWV &inSettings) :
  65. Wheel(inSettings)
  66. {
  67. JPH_ASSERT(inSettings.mInertia >= 0.0f);
  68. JPH_ASSERT(inSettings.mAngularDamping >= 0.0f);
  69. JPH_ASSERT(abs(inSettings.mMaxSteerAngle) <= 0.5f * JPH_PI);
  70. JPH_ASSERT(inSettings.mMaxBrakeTorque >= 0.0f);
  71. JPH_ASSERT(inSettings.mMaxHandBrakeTorque >= 0.0f);
  72. }
  73. void WheelWV::Update(float inDeltaTime, const VehicleConstraint &inConstraint)
  74. {
  75. const WheelSettingsWV *settings = GetSettings();
  76. // Angular damping: dw/dt = -c * w
  77. // Solution: w(t) = w(0) * e^(-c * t) or w2 = w1 * e^(-c * dt)
  78. // Taylor expansion of e^(-c * dt) = 1 - c * dt + ...
  79. // Since dt is usually in the order of 1/60 and c is a low number too this approximation is good enough
  80. mAngularVelocity *= max(0.0f, 1.0f - settings->mAngularDamping * inDeltaTime);
  81. // Update rotation of wheel
  82. mAngle = fmod(mAngle + mAngularVelocity * inDeltaTime, 2.0f * JPH_PI);
  83. if (mContactBody != nullptr)
  84. {
  85. const Body *body = inConstraint.GetVehicleBody();
  86. // Calculate relative velocity between wheel contact point and floor
  87. Vec3 relative_velocity = body->GetPointVelocity(mContactPosition) - mContactPointVelocity;
  88. // Cancel relative velocity in the normal plane
  89. relative_velocity -= mContactNormal.Dot(relative_velocity) * mContactNormal;
  90. float relative_longitudinal_velocity = relative_velocity.Dot(mContactLongitudinal);
  91. // Calculate longitudinal friction based on difference between velocity of rolling wheel and drive surface
  92. float relative_longitudinal_velocity_denom = Sign(relative_longitudinal_velocity) * max(1.0e-3f, abs(relative_longitudinal_velocity)); // Ensure we don't divide by zero
  93. mLongitudinalSlip = abs((mAngularVelocity * settings->mRadius - relative_longitudinal_velocity) / relative_longitudinal_velocity_denom);
  94. float longitudinal_slip_friction = settings->mLongitudinalFriction.GetValue(mLongitudinalSlip);
  95. // Calculate lateral friction based on slip angle
  96. float relative_velocity_len = relative_velocity.Length();
  97. float lateral_slip_angle = relative_velocity_len < 1.0e-3f? 0.0f : RadiansToDegrees(ACos(abs(relative_longitudinal_velocity) / relative_velocity_len));
  98. float lateral_slip_friction = settings->mLateralFriction.GetValue(lateral_slip_angle);
  99. // Tire friction
  100. mCombinedLongitudinalFriction = sqrt(longitudinal_slip_friction * mContactBody->GetFriction());
  101. mCombinedLateralFriction = sqrt(lateral_slip_friction * mContactBody->GetFriction());
  102. }
  103. else
  104. {
  105. // No collision
  106. mLongitudinalSlip = 0.0f;
  107. mCombinedLongitudinalFriction = mCombinedLateralFriction = 0.0f;
  108. }
  109. }
  110. VehicleController *WheeledVehicleControllerSettings::ConstructController(VehicleConstraint &inConstraint) const
  111. {
  112. return new WheeledVehicleController(*this, inConstraint);
  113. }
  114. void WheeledVehicleControllerSettings::SaveBinaryState(StreamOut &inStream) const
  115. {
  116. mEngine.SaveBinaryState(inStream);
  117. mTransmission.SaveBinaryState(inStream);
  118. uint32 num_differentials = (uint32)mDifferentials.size();
  119. inStream.Write(num_differentials);
  120. for (const VehicleDifferentialSettings &d : mDifferentials)
  121. d.SaveBinaryState(inStream);
  122. inStream.Write(mDifferentialLimitedSlipRatio);
  123. }
  124. void WheeledVehicleControllerSettings::RestoreBinaryState(StreamIn &inStream)
  125. {
  126. mEngine.RestoreBinaryState(inStream);
  127. mTransmission.RestoreBinaryState(inStream);
  128. uint32 num_differentials = 0;
  129. inStream.Read(num_differentials);
  130. mDifferentials.resize(num_differentials);
  131. for (VehicleDifferentialSettings &d : mDifferentials)
  132. d.RestoreBinaryState(inStream);
  133. inStream.Read(mDifferentialLimitedSlipRatio);
  134. }
  135. WheeledVehicleController::WheeledVehicleController(const WheeledVehicleControllerSettings &inSettings, VehicleConstraint &inConstraint) :
  136. VehicleController(inConstraint)
  137. {
  138. // Copy engine settings
  139. static_cast<VehicleEngineSettings &>(mEngine) = inSettings.mEngine;
  140. JPH_ASSERT(inSettings.mEngine.mMinRPM >= 0.0f);
  141. JPH_ASSERT(inSettings.mEngine.mMinRPM <= inSettings.mEngine.mMaxRPM);
  142. // Copy transmission settings
  143. static_cast<VehicleTransmissionSettings &>(mTransmission) = inSettings.mTransmission;
  144. #ifdef JPH_ENABLE_ASSERTS
  145. for (float r : inSettings.mTransmission.mGearRatios)
  146. JPH_ASSERT(r > 0.0f);
  147. for (float r : inSettings.mTransmission.mReverseGearRatios)
  148. JPH_ASSERT(r < 0.0f);
  149. #endif // JPH_ENABLE_ASSERTS
  150. JPH_ASSERT(inSettings.mTransmission.mSwitchTime >= 0.0f);
  151. JPH_ASSERT(inSettings.mTransmission.mShiftDownRPM > 0.0f);
  152. JPH_ASSERT(inSettings.mTransmission.mMode != ETransmissionMode::Auto || inSettings.mTransmission.mShiftUpRPM < inSettings.mEngine.mMaxRPM);
  153. JPH_ASSERT(inSettings.mTransmission.mShiftUpRPM > inSettings.mTransmission.mShiftDownRPM);
  154. JPH_ASSERT(inSettings.mTransmission.mClutchStrength > 0.0f);
  155. // Copy differential settings
  156. mDifferentials.resize(inSettings.mDifferentials.size());
  157. for (uint i = 0; i < mDifferentials.size(); ++i)
  158. {
  159. const VehicleDifferentialSettings &d = inSettings.mDifferentials[i];
  160. mDifferentials[i] = d;
  161. JPH_ASSERT(d.mDifferentialRatio > 0.0f);
  162. JPH_ASSERT(d.mLeftRightSplit >= 0.0f && d.mLeftRightSplit <= 1.0f);
  163. JPH_ASSERT(d.mEngineTorqueRatio >= 0.0f);
  164. JPH_ASSERT(d.mLimitedSlipRatio > 1.0f);
  165. }
  166. mDifferentialLimitedSlipRatio = inSettings.mDifferentialLimitedSlipRatio;
  167. JPH_ASSERT(mDifferentialLimitedSlipRatio > 1.0f);
  168. }
  169. void WheeledVehicleController::PreCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem)
  170. {
  171. JPH_PROFILE_FUNCTION();
  172. for (Wheel *w_base : mConstraint.GetWheels())
  173. {
  174. WheelWV *w = static_cast<WheelWV *>(w_base);
  175. // Set steering angle
  176. w->SetSteerAngle(-mRightInput * w->GetSettings()->mMaxSteerAngle);
  177. }
  178. }
  179. void WheeledVehicleController::PostCollide(float inDeltaTime, PhysicsSystem &inPhysicsSystem)
  180. {
  181. JPH_PROFILE_FUNCTION();
  182. // Remember old RPM so we're increasing or decreasing
  183. float old_engine_rpm = mEngine.GetCurrentRPM();
  184. Wheels &wheels = mConstraint.GetWheels();
  185. // Update wheel angle, do this before applying torque to the wheels (as friction will slow them down again)
  186. for (Wheel *w_base : wheels)
  187. {
  188. WheelWV *w = static_cast<WheelWV *>(w_base);
  189. w->Update(inDeltaTime, mConstraint);
  190. }
  191. // In auto transmission mode, don't accelerate the engine when switching gears
  192. float forward_input = abs(mForwardInput);
  193. if (mTransmission.mMode == ETransmissionMode::Auto)
  194. forward_input *= mTransmission.GetClutchFriction();
  195. // Apply damping if there is no acceleration
  196. if (forward_input < 1.0e-3f)
  197. mEngine.ApplyDamping(inDeltaTime);
  198. // Calculate engine torque
  199. float engine_torque = mEngine.GetTorque(forward_input);
  200. // Define a struct that contains information about driven differentials (i.e. that have wheels connected)
  201. struct DrivenDifferential
  202. {
  203. const VehicleDifferentialSettings * mDifferential;
  204. float mAngularVelocity;
  205. float mClutchToDifferentialTorqueRatio;
  206. float mTempTorqueFactor;
  207. };
  208. // Collect driven differentials and their speeds
  209. Array<DrivenDifferential> driven_differentials;
  210. driven_differentials.reserve(mDifferentials.size());
  211. float differential_omega_min = FLT_MAX, differential_omega_max = 0.0f;
  212. for (const VehicleDifferentialSettings &d : mDifferentials)
  213. {
  214. float avg_omega = 0.0f;
  215. int avg_omega_denom = 0;
  216. int indices[] = { d.mLeftWheel, d.mRightWheel };
  217. for (int idx : indices)
  218. if (idx != -1)
  219. {
  220. avg_omega += wheels[idx]->GetAngularVelocity();
  221. avg_omega_denom++;
  222. }
  223. if (avg_omega_denom > 0)
  224. {
  225. avg_omega = abs(avg_omega * d.mDifferentialRatio / float(avg_omega_denom)); // ignoring that the differentials may be rotating in different directions
  226. driven_differentials.push_back({ &d, avg_omega, d.mEngineTorqueRatio, 0 });
  227. // Remember min and max velocity
  228. differential_omega_min = min(differential_omega_min, avg_omega);
  229. differential_omega_max = max(differential_omega_max, avg_omega);
  230. }
  231. }
  232. if (mDifferentialLimitedSlipRatio < FLT_MAX // Limited slip differential needs to be turned on
  233. && differential_omega_max > differential_omega_min) // There needs to be a velocity difference
  234. {
  235. // Calculate factor based on relative speed of a differential
  236. float sum_factor = 0.0f;
  237. for (DrivenDifferential &d : driven_differentials)
  238. {
  239. // Differential with max velocity gets factor 0, differential with min velocity 1
  240. d.mTempTorqueFactor = (differential_omega_max - d.mAngularVelocity) / (differential_omega_max - differential_omega_min);
  241. sum_factor += d.mTempTorqueFactor;
  242. }
  243. // Normalize the result
  244. for (DrivenDifferential &d : driven_differentials)
  245. d.mTempTorqueFactor /= sum_factor;
  246. // Prevent div by zero
  247. differential_omega_min = max(1.0e-3f, differential_omega_min);
  248. differential_omega_max = max(1.0e-3f, differential_omega_max);
  249. // Map into a value that is 0 when the wheels are turning at an equal rate and 1 when the wheels are turning at mDifferentialLimitedSlipRatio
  250. float alpha = min((differential_omega_max / differential_omega_min - 1.0f) / (mDifferentialLimitedSlipRatio - 1.0f), 1.0f);
  251. JPH_ASSERT(alpha >= 0.0f);
  252. float one_min_alpha = 1.0f - alpha;
  253. // Update torque ratio for all differentials
  254. for (DrivenDifferential &d : driven_differentials)
  255. d.mClutchToDifferentialTorqueRatio = one_min_alpha * d.mClutchToDifferentialTorqueRatio + alpha * d.mTempTorqueFactor;
  256. }
  257. #ifdef JPH_ENABLE_ASSERTS
  258. // Assert the values add up to 1
  259. float sum_torque_factors = 0.0f;
  260. for (DrivenDifferential &d : driven_differentials)
  261. sum_torque_factors += d.mClutchToDifferentialTorqueRatio;
  262. JPH_ASSERT(abs(sum_torque_factors - 1.0f) < 1.0e-6f);
  263. #endif // JPH_ENABLE_ASSERTS
  264. // Define a struct that collects information about the wheels that connect to the engine
  265. struct DrivenWheel
  266. {
  267. WheelWV * mWheel;
  268. float mClutchToWheelRatio;
  269. float mClutchToWheelTorqueRatio;
  270. };
  271. Array<DrivenWheel> driven_wheels;
  272. driven_wheels.reserve(wheels.size());
  273. // Collect driven wheels
  274. float transmission_ratio = mTransmission.GetCurrentRatio();
  275. for (const DrivenDifferential &dd : driven_differentials)
  276. {
  277. VehicleDifferentialSettings d = *dd.mDifferential;
  278. WheelWV *wl = d.mLeftWheel != -1? static_cast<WheelWV *>(wheels[d.mLeftWheel]) : nullptr;
  279. WheelWV *wr = d.mRightWheel != -1? static_cast<WheelWV *>(wheels[d.mRightWheel]) : nullptr;
  280. float clutch_to_wheel_ratio = transmission_ratio * d.mDifferentialRatio;
  281. if (wl != nullptr && wr != nullptr)
  282. {
  283. // Calculate torque ratio
  284. float ratio_l, ratio_r;
  285. d.CalculateTorqueRatio(wl->GetAngularVelocity(), wr->GetAngularVelocity(), ratio_l, ratio_r);
  286. // Add both wheels
  287. driven_wheels.push_back({ wl, clutch_to_wheel_ratio, dd.mClutchToDifferentialTorqueRatio * ratio_l });
  288. driven_wheels.push_back({ wr, clutch_to_wheel_ratio, dd.mClutchToDifferentialTorqueRatio * ratio_r });
  289. }
  290. else if (wl != nullptr)
  291. {
  292. // Only left wheel, all power to left
  293. driven_wheels.push_back({ wl, clutch_to_wheel_ratio, dd.mClutchToDifferentialTorqueRatio });
  294. }
  295. else if (wr != nullptr)
  296. {
  297. // Only right wheel, all power to right
  298. driven_wheels.push_back({ wr, clutch_to_wheel_ratio, dd.mClutchToDifferentialTorqueRatio });
  299. }
  300. }
  301. bool solved = false;
  302. if (!driven_wheels.empty())
  303. {
  304. // Define the torque at the clutch at time t as:
  305. //
  306. // tc(t):=S*(we(t)-sum(R(j)*ww(j,t),j,1,N)/N)
  307. //
  308. // Where:
  309. // S is the total strength of clutch (= friction * strength)
  310. // we(t) is the engine angular velocity at time t
  311. // R(j) is the total gear ratio of clutch to wheel for wheel j
  312. // ww(j,t) is the angular velocity of wheel j at time t
  313. // N is the amount of wheels
  314. //
  315. // The torque that increases the engine angular velocity at time t is:
  316. //
  317. // te(t):=TE-tc(t)
  318. //
  319. // Where:
  320. // TE is the torque delivered by the engine
  321. //
  322. // The torque that increases the wheel angular velocity for wheel i at time t is:
  323. //
  324. // tw(i,t):=TW(i)+R(i)*F(i)*tc(t)
  325. //
  326. // Where:
  327. // TW(i) is the torque applied to the wheel outside of the engine (brake + torque due to friction with the ground)
  328. // F(i) is the fraction of the engine torque applied from engine to wheel i
  329. //
  330. // Because the angular accelaration and torque are connected through: Torque = I * dw/dt
  331. //
  332. // We have the angular acceleration of the engine at time t:
  333. //
  334. // ddt_we(t):=te(t)/Ie
  335. //
  336. // Where:
  337. // Ie is the inertia of the engine
  338. //
  339. // We have the angular acceleration of wheel i at time t:
  340. //
  341. // ddt_ww(i,t):=tw(i,t)/Iw(i)
  342. //
  343. // Where:
  344. // Iw(i) is the inertia of wheel i
  345. //
  346. // We could take a simple Euler step to calculate the resulting accelerations but because the system is very stiff this turns out to be unstable, so we need to use implicit Euler instead:
  347. //
  348. // we(t+dt)=we(t)+dt*ddt_we(t+dt)
  349. //
  350. // and:
  351. //
  352. // ww(i,t+dt)=ww(i,t)+dt*ddt_ww(i,t+dt)
  353. //
  354. // Expanding both equations (the equations above are in wxMaxima format and this can easily be done by expand(%)):
  355. //
  356. // For wheel:
  357. //
  358. // ww(i,t+dt) + (S*dt*F(i)*R(i)*sum(R(j)*ww(j,t+dt),j,1,N))/(N*Iw(i)) - (S*dt*F(i)*R(i)*we(t+dt))/Iw(i) = ww(i,t)+(dt*TW(i))/Iw(i)
  359. //
  360. // For engine:
  361. //
  362. // we(t+dt) + (S*dt*we(t+dt))/Ie - (S*dt*sum(R(j)*ww(j,t+dt),j,1,N))/(Ie*N) = we(t)+(TE*dt)/Ie
  363. //
  364. // Defining a vector w(t) = (ww(1, t), ww(2, t), ..., ww(N, t), we(t)) we can write both equations as a matrix multiplication:
  365. //
  366. // a * w(t + dt) = b
  367. //
  368. // We then invert the matrix to get the new angular velocities.
  369. //
  370. // Note that currently we set TW(i) = 0 so that the wheels will accelerate as if no external force was applied to them. These external forces are applied later and will slow down the wheel before the end of the time step.
  371. // Dimension of matrix is N + 1
  372. int n = (int)driven_wheels.size() + 1;
  373. // Last column of w is for the engine angular velocity
  374. int engine = n - 1;
  375. // Define a and b
  376. DynMatrix a(n, n);
  377. DynMatrix b(n, 1);
  378. // Get number of driven wheels as a float
  379. float num_driven_wheels_float = float(driven_wheels.size());
  380. // Angular velocity of engine
  381. float w_engine = mEngine.GetAngularVelocity();
  382. // Calculate the total strength of the clutch
  383. float clutch_strength = transmission_ratio != 0.0f? mTransmission.GetClutchFriction() * mTransmission.mClutchStrength : 0.0f;
  384. // dt / Ie
  385. float dt_div_ie = inDeltaTime / mEngine.mInertia;
  386. // Iterate the rows for the wheels
  387. for (int i = 0; i < (int)driven_wheels.size(); ++i)
  388. {
  389. const DrivenWheel &w_i = driven_wheels[i];
  390. // dt / Iw
  391. float dt_div_iw = inDeltaTime / w_i.mWheel->GetSettings()->mInertia;
  392. // S * R(i)
  393. float s_r = clutch_strength * w_i.mClutchToWheelRatio;
  394. // dt * S * R(i) * F(i) / Iw
  395. float dt_s_r_f_div_iw = dt_div_iw * s_r * w_i.mClutchToWheelTorqueRatio;
  396. // Fill in the columns of a for wheel j
  397. for (int j = 0; j < (int)driven_wheels.size(); ++j)
  398. {
  399. const DrivenWheel &w_j = driven_wheels[j];
  400. a(i, j) = dt_s_r_f_div_iw * w_j.mClutchToWheelRatio / num_driven_wheels_float;
  401. }
  402. // Add ww(i, t+dt)
  403. a(i, i) += 1.0f;
  404. // Add the column for the engine
  405. a(i, engine) = -dt_s_r_f_div_iw;
  406. // Fill in the constant b
  407. b(i, 0) = w_i.mWheel->GetAngularVelocity(); // + dt_div_iw * (brake and tire torques)
  408. // To avoid looping over the wheels again, we also fill in the wheel columns of the engine row here
  409. a(engine, i) = -dt_div_ie * s_r / num_driven_wheels_float;
  410. }
  411. // Finalize the engine row
  412. a(engine, engine) = (1.0f + dt_div_ie * clutch_strength);
  413. b(engine, 0) = w_engine + dt_div_ie * engine_torque;
  414. // Solve the linear equation
  415. if (GaussianElimination(a, b))
  416. {
  417. // Update the angular velocities for the wheels
  418. for (int i = 0; i < (int)driven_wheels.size(); ++i)
  419. {
  420. DrivenWheel &dw1 = driven_wheels[i];
  421. dw1.mWheel->SetAngularVelocity(b(i, 0));
  422. }
  423. // Update the engine RPM
  424. mEngine.SetCurrentRPM(b(engine, 0) * VehicleEngine::cAngularVelocityToRPM);
  425. // The speeds have been solved
  426. solved = true;
  427. }
  428. else
  429. {
  430. JPH_ASSERT(false, "New engine/wheel speeds could not be calculated!");
  431. }
  432. }
  433. if (!solved)
  434. {
  435. // Engine not connected to wheels, apply all torque to engine rotation
  436. mEngine.ApplyTorque(engine_torque, inDeltaTime);
  437. }
  438. // Calculate if any of the wheels are slipping, this is used to prevent gear switching
  439. bool wheels_slipping = false;
  440. for (const DrivenWheel &w : driven_wheels)
  441. wheels_slipping |= w.mClutchToWheelTorqueRatio > 0.0f && (!w.mWheel->HasContact() || w.mWheel->mLongitudinalSlip > 0.1f);
  442. // Only allow shifting up when we're not slipping and we're increasing our RPM.
  443. // After a jump, we have a very high engine RPM but once we hit the ground the RPM should be decreasing and we don't want to shift up
  444. // during that time.
  445. bool can_shift_up = !wheels_slipping && mEngine.GetCurrentRPM() >= old_engine_rpm;
  446. // Update transmission
  447. mTransmission.Update(inDeltaTime, mEngine.GetCurrentRPM(), mForwardInput, can_shift_up);
  448. // Braking
  449. for (Wheel *w_base : wheels)
  450. {
  451. WheelWV *w = static_cast<WheelWV *>(w_base);
  452. const WheelSettingsWV *settings = w->GetSettings();
  453. // Combine brake with hand brake torque
  454. float brake_torque = mBrakeInput * settings->mMaxBrakeTorque + mHandBrakeInput * settings->mMaxHandBrakeTorque;
  455. if (brake_torque > 0.0f)
  456. {
  457. // Calculate how much torque is needed to stop the wheels from rotating in this time step
  458. float brake_torque_to_lock_wheels = abs(w->GetAngularVelocity()) * settings->mInertia / inDeltaTime;
  459. if (brake_torque > brake_torque_to_lock_wheels)
  460. {
  461. // Wheels are locked
  462. w->SetAngularVelocity(0.0f);
  463. w->mBrakeImpulse = (brake_torque - brake_torque_to_lock_wheels) * inDeltaTime / settings->mRadius;
  464. }
  465. else
  466. {
  467. // Slow down the wheels
  468. w->ApplyTorque(-Sign(w->GetAngularVelocity()) * brake_torque, inDeltaTime);
  469. w->mBrakeImpulse = 0.0f;
  470. }
  471. }
  472. else
  473. {
  474. // Not braking
  475. w->mBrakeImpulse = 0.0f;
  476. }
  477. }
  478. }
  479. bool WheeledVehicleController::SolveLongitudinalAndLateralConstraints(float inDeltaTime)
  480. {
  481. bool impulse = false;
  482. for (Wheel *w_base : mConstraint.GetWheels())
  483. if (w_base->HasContact())
  484. {
  485. WheelWV *w = static_cast<WheelWV *>(w_base);
  486. const WheelSettingsWV *settings = w->GetSettings();
  487. // Calculate max impulse that we can apply on the ground
  488. float max_longitudinal_friction_impulse = w->mCombinedLongitudinalFriction * w->GetSuspensionLambda();
  489. // Calculate relative velocity between wheel contact point and floor in longitudinal direction
  490. Vec3 relative_velocity = mConstraint.GetVehicleBody()->GetPointVelocity(w->GetContactPosition()) - w->GetContactPointVelocity();
  491. float relative_longitudinal_velocity = relative_velocity.Dot(w->GetContactLongitudinal());
  492. // Calculate brake force to apply
  493. float min_longitudinal_impulse, max_longitudinal_impulse;
  494. if (w->mBrakeImpulse != 0.0f)
  495. {
  496. // Limit brake force by max tire friction
  497. float brake_impulse = min(w->mBrakeImpulse, max_longitudinal_friction_impulse);
  498. // Check which direction the brakes should be applied (we don't want to apply an impulse that would accelerate the vehicle)
  499. if (relative_longitudinal_velocity >= 0.0f)
  500. {
  501. min_longitudinal_impulse = -brake_impulse;
  502. max_longitudinal_impulse = 0.0f;
  503. }
  504. else
  505. {
  506. min_longitudinal_impulse = 0.0f;
  507. max_longitudinal_impulse = brake_impulse;
  508. }
  509. // 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
  510. impulse |= w->SolveLongitudinalConstraintPart(mConstraint, min_longitudinal_impulse, max_longitudinal_impulse);
  511. }
  512. else
  513. {
  514. // Assume we want to apply an angular impulse that makes the delta velocity between wheel and ground zero in one time step, calculate the amount of linear impulse needed to do that
  515. float desired_angular_velocity = relative_longitudinal_velocity / settings->mRadius;
  516. float linear_impulse = (w->GetAngularVelocity() - desired_angular_velocity) * settings->mInertia / settings->mRadius;
  517. // Limit the impulse by max tire friction
  518. min_longitudinal_impulse = max_longitudinal_impulse = w->GetLongitudinalLambda() + Sign(linear_impulse) * min(abs(linear_impulse), max_longitudinal_friction_impulse);
  519. // Longitudinal impulse
  520. float prev_lambda = w->GetLongitudinalLambda();
  521. impulse |= w->SolveLongitudinalConstraintPart(mConstraint, min_longitudinal_impulse, max_longitudinal_impulse);
  522. // Update the angular velocity of the wheels according to the lambda that was applied
  523. w->SetAngularVelocity(w->GetAngularVelocity() - (w->GetLongitudinalLambda() - prev_lambda) * settings->mRadius / settings->mInertia);
  524. }
  525. }
  526. for (Wheel *w_base : mConstraint.GetWheels())
  527. if (w_base->HasContact())
  528. {
  529. WheelWV *w = static_cast<WheelWV *>(w_base);
  530. // Lateral friction
  531. float max_lateral_friction_impulse = w->mCombinedLateralFriction * w->GetSuspensionLambda();
  532. impulse |= w->SolveLateralConstraintPart(mConstraint, -max_lateral_friction_impulse, max_lateral_friction_impulse);
  533. }
  534. return impulse;
  535. }
  536. #ifdef JPH_DEBUG_RENDERER
  537. void WheeledVehicleController::Draw(DebugRenderer *inRenderer) const
  538. {
  539. // Draw RPM
  540. Body *body = mConstraint.GetVehicleBody();
  541. Vec3 rpm_meter_up = body->GetRotation() * mConstraint.GetLocalUp();
  542. Vec3 rpm_meter_pos = body->GetPosition() + body->GetRotation() * mRPMMeterPosition;
  543. Vec3 rpm_meter_fwd = body->GetRotation() * mConstraint.GetLocalForward();
  544. mEngine.DrawRPM(inRenderer, rpm_meter_pos, rpm_meter_fwd, rpm_meter_up, mRPMMeterSize, mTransmission.mShiftDownRPM, mTransmission.mShiftUpRPM);
  545. // Draw current vehicle state
  546. String status = StringFormat("Forward: %.1f, Right: %.1f\nBrake: %.1f, HandBrake: %.1f\n"
  547. "Gear: %d, Clutch: %.1f\nEngineRPM: %.0f, V: %.1f km/h",
  548. (double)mForwardInput, (double)mRightInput, (double)mBrakeInput, (double)mHandBrakeInput,
  549. mTransmission.GetCurrentGear(), (double)mTransmission.GetClutchFriction(), (double)mEngine.GetCurrentRPM(), (double)body->GetLinearVelocity().Length() * 3.6);
  550. inRenderer->DrawText3D(body->GetPosition(), status, Color::sWhite, mConstraint.GetDrawConstraintSize());
  551. for (const Wheel *w_base : mConstraint.GetWheels())
  552. {
  553. const WheelWV *w = static_cast<const WheelWV *>(w_base);
  554. const WheelSettings *settings = w->GetSettings();
  555. // Calculate where the suspension attaches to the body in world space
  556. Vec3 ws_position = body->GetCenterOfMassPosition() + body->GetRotation() * (settings->mPosition - body->GetShape()->GetCenterOfMass());
  557. if (w->HasContact())
  558. {
  559. // Draw contact
  560. inRenderer->DrawLine(ws_position, w->GetContactPosition(), w->HasHitHardPoint()? Color::sRed : Color::sGreen); // Red if we hit the 'max up' limit
  561. inRenderer->DrawLine(w->GetContactPosition(), w->GetContactPosition() + w->GetContactNormal(), Color::sYellow);
  562. inRenderer->DrawLine(w->GetContactPosition(), w->GetContactPosition() + w->GetContactLongitudinal(), Color::sRed);
  563. inRenderer->DrawLine(w->GetContactPosition(), w->GetContactPosition() + w->GetContactLateral(), Color::sBlue);
  564. DebugRenderer::sInstance->DrawText3D(w->GetContactPosition(), StringFormat("W: %.1f, S: %.2f\nSlip: %.2f, FrLateral: %.1f, FrLong: %.1f", (double)w->GetAngularVelocity(), (double)w->GetSuspensionLength(), (double)w->mLongitudinalSlip, (double)w->mCombinedLateralFriction, (double)w->mCombinedLongitudinalFriction), Color::sWhite, 0.1f);
  565. }
  566. else
  567. {
  568. // Draw 'no hit'
  569. Vec3 max_droop = body->GetRotation() * settings->mDirection * (settings->mSuspensionMaxLength + settings->mRadius);
  570. inRenderer->DrawLine(ws_position, ws_position + max_droop, Color::sYellow);
  571. DebugRenderer::sInstance->DrawText3D(ws_position + max_droop, StringFormat("W: %.1f", (double)w->GetAngularVelocity()), Color::sRed, 0.1f);
  572. }
  573. }
  574. }
  575. #endif // JPH_DEBUG_RENDERER
  576. void WheeledVehicleController::SaveState(StateRecorder &inStream) const
  577. {
  578. inStream.Write(mForwardInput);
  579. inStream.Write(mRightInput);
  580. inStream.Write(mBrakeInput);
  581. inStream.Write(mHandBrakeInput);
  582. mEngine.SaveState(inStream);
  583. mTransmission.SaveState(inStream);
  584. }
  585. void WheeledVehicleController::RestoreState(StateRecorder &inStream)
  586. {
  587. inStream.Read(mForwardInput);
  588. inStream.Read(mRightInput);
  589. inStream.Read(mBrakeInput);
  590. inStream.Read(mHandBrakeInput);
  591. mEngine.RestoreState(inStream);
  592. mTransmission.RestoreState(inStream);
  593. }
  594. JPH_NAMESPACE_END