VehicleConstraint.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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/VehicleConstraint.h>
  6. #include <Jolt/Physics/Vehicle/VehicleController.h>
  7. #include <Jolt/Physics/PhysicsSystem.h>
  8. #include <Jolt/ObjectStream/TypeDeclarations.h>
  9. #include <Jolt/Core/StreamIn.h>
  10. #include <Jolt/Core/StreamOut.h>
  11. #include <Jolt/Core/Factory.h>
  12. JPH_NAMESPACE_BEGIN
  13. JPH_IMPLEMENT_SERIALIZABLE_VIRTUAL(VehicleConstraintSettings)
  14. {
  15. JPH_ADD_BASE_CLASS(VehicleConstraintSettings, ConstraintSettings)
  16. JPH_ADD_ATTRIBUTE(VehicleConstraintSettings, mUp)
  17. JPH_ADD_ATTRIBUTE(VehicleConstraintSettings, mForward)
  18. JPH_ADD_ATTRIBUTE(VehicleConstraintSettings, mMaxPitchRollAngle)
  19. JPH_ADD_ATTRIBUTE(VehicleConstraintSettings, mWheels)
  20. JPH_ADD_ATTRIBUTE(VehicleConstraintSettings, mAntiRollBars)
  21. JPH_ADD_ATTRIBUTE(VehicleConstraintSettings, mController)
  22. }
  23. void VehicleConstraintSettings::SaveBinaryState(StreamOut &inStream) const
  24. {
  25. ConstraintSettings::SaveBinaryState(inStream);
  26. inStream.Write(mUp);
  27. inStream.Write(mForward);
  28. inStream.Write(mMaxPitchRollAngle);
  29. uint32 num_anti_rollbars = (uint32)mAntiRollBars.size();
  30. inStream.Write(num_anti_rollbars);
  31. for (const VehicleAntiRollBar &r : mAntiRollBars)
  32. r.SaveBinaryState(inStream);
  33. uint32 num_wheels = (uint32)mWheels.size();
  34. inStream.Write(num_wheels);
  35. for (const WheelSettings *w : mWheels)
  36. w->SaveBinaryState(inStream);
  37. inStream.Write(mController->GetRTTI()->GetHash());
  38. mController->SaveBinaryState(inStream);
  39. }
  40. void VehicleConstraintSettings::RestoreBinaryState(StreamIn &inStream)
  41. {
  42. ConstraintSettings::RestoreBinaryState(inStream);
  43. inStream.Read(mUp);
  44. inStream.Read(mForward);
  45. inStream.Read(mMaxPitchRollAngle);
  46. uint32 num_anti_rollbars = 0;
  47. inStream.Read(num_anti_rollbars);
  48. mAntiRollBars.resize(num_anti_rollbars);
  49. for (VehicleAntiRollBar &r : mAntiRollBars)
  50. r.RestoreBinaryState(inStream);
  51. uint32 num_wheels = 0;
  52. inStream.Read(num_wheels);
  53. mWheels.resize(num_wheels);
  54. for (WheelSettings *w : mWheels)
  55. w->RestoreBinaryState(inStream);
  56. uint32 hash = 0;
  57. inStream.Read(hash);
  58. const RTTI *rtti = Factory::sInstance->Find(hash);
  59. mController = reinterpret_cast<VehicleControllerSettings *>(rtti->CreateObject());
  60. mController->RestoreBinaryState(inStream);
  61. }
  62. VehicleConstraint::VehicleConstraint(Body &inVehicleBody, const VehicleConstraintSettings &inSettings) :
  63. Constraint(inSettings),
  64. mBody(&inVehicleBody),
  65. mForward(inSettings.mForward),
  66. mUp(inSettings.mUp),
  67. mWorldUp(inSettings.mUp)
  68. {
  69. // Check sanity of incoming settings
  70. JPH_ASSERT(inSettings.mUp.IsNormalized());
  71. JPH_ASSERT(inSettings.mForward.IsNormalized());
  72. JPH_ASSERT(!inSettings.mWheels.empty());
  73. // Store max pitch/roll angle
  74. SetMaxPitchRollAngle(inSettings.mMaxPitchRollAngle);
  75. // Copy anti-rollbar settings
  76. mAntiRollBars.resize(inSettings.mAntiRollBars.size());
  77. for (uint i = 0; i < mAntiRollBars.size(); ++i)
  78. {
  79. const VehicleAntiRollBar &r = inSettings.mAntiRollBars[i];
  80. mAntiRollBars[i] = r;
  81. JPH_ASSERT(r.mStiffness >= 0.0f);
  82. }
  83. // Construct our controler class
  84. mController = inSettings.mController->ConstructController(*this);
  85. // Create wheels
  86. mWheels.resize(inSettings.mWheels.size());
  87. for (uint i = 0; i < mWheels.size(); ++i)
  88. mWheels[i] = mController->ConstructWheel(*inSettings.mWheels[i]);
  89. }
  90. VehicleConstraint::~VehicleConstraint()
  91. {
  92. // Destroy controller
  93. delete mController;
  94. // Destroy our wheels
  95. for (Wheel *w : mWheels)
  96. delete w;
  97. }
  98. void VehicleConstraint::GetWheelLocalBasis(const Wheel *inWheel, Vec3 &outForward, Vec3 &outUp, Vec3 &outRight) const
  99. {
  100. const WheelSettings *settings = inWheel->mSettings;
  101. Quat steer_rotation = Quat::sRotation(settings->mSteeringAxis, inWheel->mSteerAngle);
  102. outUp = steer_rotation * settings->mWheelUp;
  103. outForward = steer_rotation * settings->mWheelForward;
  104. outRight = outForward.Cross(outUp).Normalized();
  105. outForward = outUp.Cross(outRight).Normalized();
  106. }
  107. Mat44 VehicleConstraint::GetWheelLocalTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const
  108. {
  109. JPH_ASSERT(inWheelIndex < mWheels.size());
  110. const Wheel *wheel = mWheels[inWheelIndex];
  111. const WheelSettings *settings = wheel->mSettings;
  112. // Use the two vectors provided to calculate a matrix that takes us from wheel model space to X = right, Y = up, Z = forward (the space where we will rotate the wheel)
  113. Mat44 wheel_to_rotational = Mat44(Vec4(inWheelRight, 0), Vec4(inWheelUp, 0), Vec4(inWheelUp.Cross(inWheelRight), 0), Vec4(0, 0, 0, 1)).Transposed();
  114. // Calculate the matrix that takes us from the rotational space to vehicle local space
  115. Vec3 local_forward, local_up, local_right;
  116. GetWheelLocalBasis(wheel, local_forward, local_up, local_right);
  117. Vec3 local_wheel_pos = settings->mPosition + settings->mSuspensionDirection * wheel->mSuspensionLength;
  118. Mat44 rotational_to_local(Vec4(local_right, 0), Vec4(local_up, 0), Vec4(local_forward, 0), Vec4(local_wheel_pos, 1));
  119. // Calculate transform of rotated wheel
  120. return rotational_to_local * Mat44::sRotationX(wheel->mAngle) * wheel_to_rotational;
  121. }
  122. RMat44 VehicleConstraint::GetWheelWorldTransform(uint inWheelIndex, Vec3Arg inWheelRight, Vec3Arg inWheelUp) const
  123. {
  124. return mBody->GetWorldTransform() * GetWheelLocalTransform(inWheelIndex, inWheelRight, inWheelUp);
  125. }
  126. void VehicleConstraint::OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem)
  127. {
  128. JPH_PROFILE_FUNCTION();
  129. // Calculate new world up vector by inverting gravity
  130. mWorldUp = (-inPhysicsSystem.GetGravity()).NormalizedOr(mWorldUp);
  131. // Callback on our controller
  132. mController->PreCollide(inDeltaTime, inPhysicsSystem);
  133. // Calculate if this constraint is active by checking if our main vehicle body is active or any of the bodies we touch are active
  134. mIsActive = mBody->IsActive();
  135. RMat44 body_transform = mBody->GetWorldTransform();
  136. // Test collision for wheels
  137. for (uint wheel_index = 0; wheel_index < mWheels.size(); ++wheel_index)
  138. {
  139. Wheel *w = mWheels[wheel_index];
  140. const WheelSettings *settings = w->mSettings;
  141. // Reset contact
  142. w->mContactBodyID = BodyID();
  143. w->mContactBody = nullptr;
  144. w->mContactSubShapeID = SubShapeID();
  145. w->mSuspensionLength = settings->mSuspensionMaxLength;
  146. // Test collision to find the floor
  147. RVec3 ws_origin = body_transform * settings->mPosition;
  148. Vec3 ws_direction = body_transform.Multiply3x3(settings->mSuspensionDirection);
  149. if (mVehicleCollisionTester->Collide(inPhysicsSystem, *this, wheel_index, ws_origin, ws_direction, mBody->GetID(), w->mContactBody, w->mContactSubShapeID, w->mContactPosition, w->mContactNormal, w->mSuspensionLength))
  150. {
  151. // Store ID (pointer is not valid outside of the simulation step)
  152. w->mContactBodyID = w->mContactBody->GetID();
  153. // Store contact velocity, cache this as the contact body may be removed
  154. w->mContactPointVelocity = w->mContactBody->GetPointVelocity(w->mContactPosition);
  155. // Determine plane constant for axle contact plane
  156. w->mAxlePlaneConstant = RVec3(w->mContactNormal).Dot(ws_origin + w->mSuspensionLength * ws_direction);
  157. // Check if body is active, if so the entire vehicle should be active
  158. mIsActive |= w->mContactBody->IsActive();
  159. // Determine world space forward using steering angle and body rotation
  160. Vec3 forward, up, right;
  161. GetWheelLocalBasis(w, forward, up, right);
  162. forward = body_transform.Multiply3x3(forward);
  163. right = body_transform.Multiply3x3(right);
  164. // The longitudinal axis is in the up/forward plane
  165. w->mContactLongitudinal = w->mContactNormal.Cross(right);
  166. // Make sure that the longitudinal axis is aligned with the forward axis
  167. if (w->mContactLongitudinal.Dot(forward) < 0.0f)
  168. w->mContactLongitudinal = -w->mContactLongitudinal;
  169. // Normalize it
  170. w->mContactLongitudinal = w->mContactLongitudinal.NormalizedOr(w->mContactNormal.GetNormalizedPerpendicular());
  171. // The lateral axis is perpendicular to contact normal and longitudinal axis
  172. w->mContactLateral = w->mContactLongitudinal.Cross(w->mContactNormal).Normalized();
  173. }
  174. }
  175. // Calculate anti-rollbar impulses
  176. for (const VehicleAntiRollBar &r : mAntiRollBars)
  177. {
  178. Wheel *lw = mWheels[r.mLeftWheel];
  179. Wheel *rw = mWheels[r.mRightWheel];
  180. if (lw->mContactBody != nullptr && rw->mContactBody != nullptr)
  181. {
  182. // Calculate the impulse to apply based on the difference in suspension length
  183. float difference = rw->mSuspensionLength - lw->mSuspensionLength;
  184. float impulse = difference * r.mStiffness * inDeltaTime;
  185. lw->mAntiRollBarImpulse = -impulse;
  186. rw->mAntiRollBarImpulse = impulse;
  187. }
  188. else
  189. {
  190. // When one of the wheels is not on the ground we don't apply any impulses
  191. lw->mAntiRollBarImpulse = rw->mAntiRollBarImpulse = 0.0f;
  192. }
  193. }
  194. // Callback on our controller
  195. mController->PostCollide(inDeltaTime, inPhysicsSystem);
  196. // If the wheels are rotating, we don't want to go to sleep yet
  197. bool allow_sleep = mController->AllowSleep();
  198. if (allow_sleep)
  199. for (const Wheel *w : mWheels)
  200. if (abs(w->mAngularVelocity) > DegreesToRadians(10.0f))
  201. {
  202. allow_sleep = false;
  203. break;
  204. }
  205. if (mBody->GetAllowSleeping() != allow_sleep)
  206. mBody->SetAllowSleeping(allow_sleep);
  207. }
  208. void VehicleConstraint::BuildIslands(uint32 inConstraintIndex, IslandBuilder &ioBuilder, BodyManager &inBodyManager)
  209. {
  210. // Find dynamic bodies that our wheels are touching
  211. BodyID *body_ids = (BodyID *)JPH_STACK_ALLOC((mWheels.size() + 1) * sizeof(BodyID));
  212. int num_bodies = 0;
  213. bool needs_to_activate = false;
  214. for (const Wheel *w : mWheels)
  215. if (w->mContactBody != nullptr)
  216. {
  217. // Avoid adding duplicates
  218. bool duplicate = false;
  219. BodyID id = w->mContactBody->GetID();
  220. for (int i = 0; i < num_bodies; ++i)
  221. if (body_ids[i] == id)
  222. {
  223. duplicate = true;
  224. break;
  225. }
  226. if (duplicate)
  227. continue;
  228. if (w->mContactBody->IsDynamic())
  229. {
  230. body_ids[num_bodies++] = id;
  231. needs_to_activate |= !w->mContactBody->IsActive();
  232. }
  233. }
  234. // Activate bodies, note that if we get here we have already told the system that we're active so that means our main body needs to be active too
  235. if (!mBody->IsActive())
  236. {
  237. // Our main body is not active, activate it too
  238. body_ids[num_bodies] = mBody->GetID();
  239. inBodyManager.ActivateBodies(body_ids, num_bodies + 1);
  240. }
  241. else if (needs_to_activate)
  242. {
  243. // Only activate bodies the wheels are touching
  244. inBodyManager.ActivateBodies(body_ids, num_bodies);
  245. }
  246. // Link the bodies into the same island
  247. uint32 min_active_index = Body::cInactiveIndex;
  248. for (int i = 0; i < num_bodies; ++i)
  249. {
  250. const Body &body = inBodyManager.GetBody(body_ids[i]);
  251. min_active_index = min(min_active_index, body.GetIndexInActiveBodiesInternal());
  252. ioBuilder.LinkBodies(mBody->GetIndexInActiveBodiesInternal(), body.GetIndexInActiveBodiesInternal());
  253. }
  254. // Link the constraint in the island
  255. ioBuilder.LinkConstraint(inConstraintIndex, mBody->GetIndexInActiveBodiesInternal(), min_active_index);
  256. }
  257. uint VehicleConstraint::BuildIslandSplits(LargeIslandSplitter &ioSplitter) const
  258. {
  259. return ioSplitter.AssignToNonParallelSplit(mBody);
  260. }
  261. void VehicleConstraint::CalculateSuspensionForcePoint(const Wheel &inWheel, Vec3 &outR1PlusU, Vec3 &outR2) const
  262. {
  263. // Determine point to apply force to
  264. RVec3 force_point;
  265. if (inWheel.mSettings->mEnableSuspensionForcePoint)
  266. force_point = mBody->GetWorldTransform() * inWheel.mSettings->mSuspensionForcePoint;
  267. else
  268. force_point = inWheel.mContactPosition;
  269. // Calculate r1 + u and r2
  270. outR1PlusU = Vec3(force_point - mBody->GetCenterOfMassPosition());
  271. outR2 = Vec3(force_point - inWheel.mContactBody->GetCenterOfMassPosition());
  272. }
  273. void VehicleConstraint::CalculatePitchRollConstraintProperties(RMat44Arg inBodyTransform)
  274. {
  275. // Check if a limit was specified
  276. if (mCosMaxPitchRollAngle > -1.0f)
  277. {
  278. // Calculate cos of angle between world up vector and vehicle up vector
  279. Vec3 vehicle_up = inBodyTransform.Multiply3x3(mUp);
  280. mCosPitchRollAngle = mWorldUp.Dot(vehicle_up);
  281. if (mCosPitchRollAngle < mCosMaxPitchRollAngle)
  282. {
  283. // Calculate rotation axis to rotate vehicle towards up
  284. Vec3 rotation_axis = mWorldUp.Cross(vehicle_up);
  285. float len = rotation_axis.Length();
  286. if (len > 0.0f)
  287. mPitchRollRotationAxis = rotation_axis / len;
  288. mPitchRollPart.CalculateConstraintProperties(*mBody, Body::sFixedToWorld, mPitchRollRotationAxis);
  289. }
  290. else
  291. mPitchRollPart.Deactivate();
  292. }
  293. else
  294. mPitchRollPart.Deactivate();
  295. }
  296. void VehicleConstraint::SetupVelocityConstraint(float inDeltaTime)
  297. {
  298. RMat44 body_transform = mBody->GetWorldTransform();
  299. for (Wheel *w : mWheels)
  300. if (w->mContactBody != nullptr)
  301. {
  302. const WheelSettings *settings = w->mSettings;
  303. Vec3 neg_contact_normal = -w->mContactNormal;
  304. Vec3 r1_plus_u, r2;
  305. CalculateSuspensionForcePoint(*w, r1_plus_u, r2);
  306. // Suspension spring
  307. if (settings->mSuspensionMaxLength > settings->mSuspensionMinLength)
  308. {
  309. // Calculate cos(alpha) where alpha is the angle between suspension direction and contact normal
  310. // Note that we clamp 1 / cos(alpha) to the range [0.1, 1] in order not to increase the stiffness / damping by too much.
  311. Vec3 ws_direction = body_transform.Multiply3x3(settings->mSuspensionDirection);
  312. float cos_angle = max(0.1f, ws_direction.Dot(neg_contact_normal));
  313. SpringSettings spring_settings = settings->mSuspensionSpring;
  314. if (spring_settings.mMode == ESpringMode::FrequencyAndDamping)
  315. {
  316. // Calculate the damping and frequency of the suspension spring given the angle between the suspension direction and the contact normal
  317. // If the angle between the suspension direction and the inverse of the contact normal is alpha then the force on the spring relates to the force along the contact normal as:
  318. //
  319. // Fspring = Fnormal * cos(alpha)
  320. //
  321. // The spring force is:
  322. //
  323. // Fspring = -k * x
  324. //
  325. // where k is the spring constant and x is the displacement of the spring. So we have:
  326. //
  327. // Fnormal * cos(alpha) = -k * x <=> Fnormal = -k / cos(alpha) * x
  328. //
  329. // So we can see this as a spring with spring constant:
  330. //
  331. // k' = k / cos(alpha)
  332. //
  333. // In the same way the velocity relates like:
  334. //
  335. // Vspring = Vnormal * cos(alpha)
  336. //
  337. // Which results in the modified damping constant c:
  338. //
  339. // c' = c / cos(alpha)
  340. //
  341. // Since we're not supplying k and c directly but rather the frequency and damping we can calculate the spring constant and damping constant as:
  342. //
  343. // w = 2 * pi * f
  344. // k = m * w^2
  345. // c = 2 * m * w * d
  346. //
  347. // where m is the mass of the spring, f is the frequency and d is the damping factor (see SpringPart::CalculateSpringProperties). So we have:
  348. //
  349. // w' = w * pi * f'
  350. // k' = m * w'^2
  351. // c' = 2 * m * w' * d'
  352. //
  353. // where f' = f / sqrt(cos(alpha)) and d' = d / sqrt(cos(alpha))
  354. //
  355. // We ensure that the frequency doesn't go over half the simulation frequency to prevent the spring from getting unstable.
  356. float sqrt_cos_angle = sqrt(cos_angle);
  357. spring_settings.mDamping /= sqrt_cos_angle;
  358. spring_settings.mFrequency = min(0.5f / inDeltaTime, spring_settings.mFrequency / sqrt_cos_angle);
  359. }
  360. else
  361. {
  362. // This case is similar to the one above but we're not supplying frequency and damping but rather the spring constant and damping constant directly.
  363. spring_settings.mStiffness /= cos_angle;
  364. spring_settings.mDamping /= cos_angle;
  365. }
  366. // Get the value of the constraint equation
  367. float c = w->mSuspensionLength - settings->mSuspensionMaxLength - settings->mSuspensionPreloadLength;
  368. w->mSuspensionPart.CalculateConstraintPropertiesWithSettings(inDeltaTime, *mBody, r1_plus_u, *w->mContactBody, r2, neg_contact_normal, w->mAntiRollBarImpulse, c, spring_settings);
  369. }
  370. else
  371. w->mSuspensionPart.Deactivate();
  372. // Check if we reached the 'max up' position and if so add a hard velocity constraint that stops any further movement in the normal direction
  373. if (w->mSuspensionLength < settings->mSuspensionMinLength)
  374. w->mSuspensionMaxUpPart.CalculateConstraintProperties(*mBody, r1_plus_u, *w->mContactBody, r2, neg_contact_normal);
  375. else
  376. w->mSuspensionMaxUpPart.Deactivate();
  377. // Friction and propulsion
  378. w->mLongitudinalPart.CalculateConstraintProperties(*mBody, r1_plus_u, *w->mContactBody, r2, -w->mContactLongitudinal);
  379. w->mLateralPart.CalculateConstraintProperties(*mBody, r1_plus_u, *w->mContactBody, r2, -w->mContactLateral);
  380. }
  381. else
  382. {
  383. // No contact -> disable everything
  384. w->mSuspensionPart.Deactivate();
  385. w->mSuspensionMaxUpPart.Deactivate();
  386. w->mLongitudinalPart.Deactivate();
  387. w->mLateralPart.Deactivate();
  388. }
  389. CalculatePitchRollConstraintProperties(body_transform);
  390. }
  391. void VehicleConstraint::WarmStartVelocityConstraint(float inWarmStartImpulseRatio)
  392. {
  393. for (Wheel *w : mWheels)
  394. if (w->mContactBody != nullptr)
  395. {
  396. Vec3 neg_contact_normal = -w->mContactNormal;
  397. w->mSuspensionPart.WarmStart(*mBody, *w->mContactBody, neg_contact_normal, inWarmStartImpulseRatio);
  398. w->mSuspensionMaxUpPart.WarmStart(*mBody, *w->mContactBody, neg_contact_normal, inWarmStartImpulseRatio);
  399. w->mLongitudinalPart.WarmStart(*mBody, *w->mContactBody, -w->mContactLongitudinal, 0.0f); // Don't warm start the longitudinal part (the engine/brake force, we don't want to preserve anything from the last frame)
  400. w->mLateralPart.WarmStart(*mBody, *w->mContactBody, -w->mContactLateral, inWarmStartImpulseRatio);
  401. }
  402. mPitchRollPart.WarmStart(*mBody, Body::sFixedToWorld, inWarmStartImpulseRatio);
  403. }
  404. bool VehicleConstraint::SolveVelocityConstraint(float inDeltaTime)
  405. {
  406. bool impulse = false;
  407. // Solve suspension
  408. for (Wheel *w : mWheels)
  409. if (w->mContactBody != nullptr)
  410. {
  411. Vec3 neg_contact_normal = -w->mContactNormal;
  412. // Suspension spring, note that it can only push and not pull
  413. if (w->mSuspensionPart.IsActive())
  414. impulse |= w->mSuspensionPart.SolveVelocityConstraint(*mBody, *w->mContactBody, neg_contact_normal, 0.0f, FLT_MAX);
  415. // When reaching the minimal suspension length only allow forces pushing the bodies away
  416. if (w->mSuspensionMaxUpPart.IsActive())
  417. impulse |= w->mSuspensionMaxUpPart.SolveVelocityConstraint(*mBody, *w->mContactBody, neg_contact_normal, 0.0f, FLT_MAX);
  418. }
  419. // Solve the horizontal movement of the vehicle
  420. impulse |= mController->SolveLongitudinalAndLateralConstraints(inDeltaTime);
  421. // Apply the pitch / roll constraint to avoid the vehicle from toppling over
  422. if (mPitchRollPart.IsActive())
  423. impulse |= mPitchRollPart.SolveVelocityConstraint(*mBody, Body::sFixedToWorld, mPitchRollRotationAxis, 0, FLT_MAX);
  424. return impulse;
  425. }
  426. bool VehicleConstraint::SolvePositionConstraint(float inDeltaTime, float inBaumgarte)
  427. {
  428. bool impulse = false;
  429. RMat44 body_transform = mBody->GetWorldTransform();
  430. for (Wheel *w : mWheels)
  431. if (w->mContactBody != nullptr)
  432. {
  433. const WheelSettings *settings = w->mSettings;
  434. // Check if we reached the 'max up' position now that the body has possibly moved
  435. // We do this by calculating the axle position at minimum suspension length and making sure it does not go through the
  436. // plane defined by the contact normal and the axle position when the contact happened
  437. // TODO: This assumes that only the vehicle moved and not the ground as we kept the axle contact plane in world space
  438. Vec3 ws_direction = body_transform.Multiply3x3(settings->mSuspensionDirection);
  439. RVec3 ws_position = body_transform * settings->mPosition;
  440. RVec3 min_suspension_pos = ws_position + settings->mSuspensionMinLength * ws_direction;
  441. float max_up_error = float(RVec3(w->mContactNormal).Dot(min_suspension_pos) - w->mAxlePlaneConstant);
  442. if (max_up_error < 0.0f)
  443. {
  444. Vec3 neg_contact_normal = -w->mContactNormal;
  445. // Recalculate constraint properties since the body may have moved
  446. Vec3 r1_plus_u, r2;
  447. CalculateSuspensionForcePoint(*w, r1_plus_u, r2);
  448. w->mSuspensionMaxUpPart.CalculateConstraintProperties(*mBody, r1_plus_u, *w->mContactBody, r2, neg_contact_normal);
  449. impulse |= w->mSuspensionMaxUpPart.SolvePositionConstraint(*mBody, *w->mContactBody, neg_contact_normal, max_up_error, inBaumgarte);
  450. }
  451. }
  452. // Apply the pitch / roll constraint to avoid the vehicle from toppling over
  453. CalculatePitchRollConstraintProperties(body_transform);
  454. if (mPitchRollPart.IsActive())
  455. impulse |= mPitchRollPart.SolvePositionConstraint(*mBody, Body::sFixedToWorld, mCosPitchRollAngle - mCosMaxPitchRollAngle, inBaumgarte);
  456. return impulse;
  457. }
  458. #ifdef JPH_DEBUG_RENDERER
  459. void VehicleConstraint::DrawConstraint(DebugRenderer *inRenderer) const
  460. {
  461. mController->Draw(inRenderer);
  462. }
  463. void VehicleConstraint::DrawConstraintLimits(DebugRenderer *inRenderer) const
  464. {
  465. }
  466. #endif // JPH_DEBUG_RENDERER
  467. void VehicleConstraint::SaveState(StateRecorder &inStream) const
  468. {
  469. Constraint::SaveState(inStream);
  470. mController->SaveState(inStream);
  471. for (const Wheel *w : mWheels)
  472. {
  473. inStream.Write(w->mAngularVelocity);
  474. inStream.Write(w->mAngle);
  475. inStream.Write(w->mContactBodyID); // Used by MotorcycleController::PreCollide
  476. inStream.Write(w->mContactNormal); // Used by MotorcycleController::PreCollide
  477. inStream.Write(w->mContactLateral); // Used by MotorcycleController::PreCollide
  478. w->mSuspensionPart.SaveState(inStream);
  479. w->mSuspensionMaxUpPart.SaveState(inStream);
  480. w->mLongitudinalPart.SaveState(inStream);
  481. w->mLateralPart.SaveState(inStream);
  482. }
  483. inStream.Write(mPitchRollRotationAxis); // When rotation is too small we use last frame so we need to store it
  484. mPitchRollPart.SaveState(inStream);
  485. }
  486. void VehicleConstraint::RestoreState(StateRecorder &inStream)
  487. {
  488. Constraint::RestoreState(inStream);
  489. mController->RestoreState(inStream);
  490. for (Wheel *w : mWheels)
  491. {
  492. inStream.Read(w->mAngularVelocity);
  493. inStream.Read(w->mAngle);
  494. inStream.Read(w->mContactBodyID);
  495. inStream.Read(w->mContactNormal);
  496. inStream.Read(w->mContactLateral);
  497. w->mContactBody = nullptr; // No longer valid
  498. w->mSuspensionPart.RestoreState(inStream);
  499. w->mSuspensionMaxUpPart.RestoreState(inStream);
  500. w->mLongitudinalPart.RestoreState(inStream);
  501. w->mLateralPart.RestoreState(inStream);
  502. }
  503. inStream.Read(mPitchRollRotationAxis);
  504. mPitchRollPart.RestoreState(inStream);
  505. }
  506. Ref<ConstraintSettings> VehicleConstraint::GetConstraintSettings() const
  507. {
  508. JPH_ASSERT(false); // Not implemented yet
  509. return nullptr;
  510. }
  511. JPH_NAMESPACE_END