SoftBodyMotionProperties.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
  6. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  7. #include <Jolt/Physics/PhysicsSystem.h>
  8. #ifdef JPH_DEBUG_RENDERER
  9. #include <Jolt/Renderer/DebugRenderer.h>
  10. #endif // JPH_DEBUG_RENDERER
  11. JPH_NAMESPACE_BEGIN
  12. void SoftBodyMotionProperties::CalculateMassAndInertia()
  13. {
  14. MassProperties mp;
  15. for (const Vertex &v : mVertices)
  16. if (v.mInvMass > 0.0f)
  17. {
  18. Vec3 pos = v.mPosition;
  19. // Accumulate mass
  20. float mass = 1.0f / v.mInvMass;
  21. mp.mMass += mass;
  22. // Inertia tensor, diagonal
  23. // See equations https://en.wikipedia.org/wiki/Moment_of_inertia section 'Inertia Tensor'
  24. for (int i = 0; i < 3; ++i)
  25. mp.mInertia(i, i) += mass * (Square(pos[(i + 1) % 3]) + Square(pos[(i + 2) % 3]));
  26. // Inertia tensor off diagonal
  27. for (int i = 0; i < 3; ++i)
  28. for (int j = 0; j < 3; ++j)
  29. if (i != j)
  30. mp.mInertia(i, j) -= mass * pos[i] * pos[j];
  31. }
  32. else
  33. {
  34. // If one vertex is kinematic, the entire body will have infinite mass and inertia
  35. SetInverseMass(0.0f);
  36. SetInverseInertia(Vec3::sZero(), Quat::sIdentity());
  37. return;
  38. }
  39. SetMassProperties(EAllowedDOFs::All, mp);
  40. }
  41. void SoftBodyMotionProperties::Initialize(const SoftBodyCreationSettings &inSettings)
  42. {
  43. // Store settings
  44. mSettings = inSettings.mSettings;
  45. mNumIterations = inSettings.mNumIterations;
  46. mPressure = inSettings.mPressure;
  47. mUpdatePosition = inSettings.mUpdatePosition;
  48. // Initialize vertices
  49. mVertices.resize(inSettings.mSettings->mVertices.size());
  50. Mat44 rotation = inSettings.mMakeRotationIdentity? Mat44::sRotation(inSettings.mRotation) : Mat44::sIdentity();
  51. for (Array<Vertex>::size_type v = 0, s = mVertices.size(); v < s; ++v)
  52. {
  53. const SoftBodySharedSettings::Vertex &in_vertex = inSettings.mSettings->mVertices[v];
  54. Vertex &out_vertex = mVertices[v];
  55. out_vertex.mPreviousPosition = out_vertex.mPosition = rotation * Vec3(in_vertex.mPosition);
  56. out_vertex.mVelocity = rotation.Multiply3x3(Vec3(in_vertex.mVelocity));
  57. out_vertex.mCollidingShapeIndex = -1;
  58. out_vertex.mLargestPenetration = -FLT_MAX;
  59. out_vertex.mInvMass = in_vertex.mInvMass;
  60. mLocalBounds.Encapsulate(out_vertex.mPosition);
  61. }
  62. // We don't know delta time yet, so we can't predict the bounds and use the local bounds as the predicted bounds
  63. mLocalPredictedBounds = mLocalBounds;
  64. CalculateMassAndInertia();
  65. }
  66. float SoftBodyMotionProperties::GetVolumeTimesSix() const
  67. {
  68. float six_volume = 0.0f;
  69. for (const Face &f : mSettings->mFaces)
  70. {
  71. Vec3 x1 = mVertices[f.mVertex[0]].mPosition;
  72. Vec3 x2 = mVertices[f.mVertex[1]].mPosition;
  73. Vec3 x3 = mVertices[f.mVertex[2]].mPosition;
  74. six_volume += x1.Cross(x2).Dot(x3); // We pick zero as the origin as this is the center of the bounding box so should give good accuracy
  75. }
  76. return six_volume;
  77. }
  78. void SoftBodyMotionProperties::DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem)
  79. {
  80. JPH_PROFILE_FUNCTION();
  81. struct Collector : public CollideShapeBodyCollector
  82. {
  83. Collector(Body &inSoftBody, RMat44Arg inTransform, const PhysicsSystem &inSystem, Array<CollidingShape> &ioHits) :
  84. mSoftBody(inSoftBody),
  85. mInverseTransform(inTransform.InversedRotationTranslation()),
  86. mBodyLockInterface(inSystem.GetBodyLockInterfaceNoLock()),
  87. mCombineFriction(inSystem.GetCombineFriction()),
  88. mCombineRestitution(inSystem.GetCombineRestitution()),
  89. mHits(ioHits)
  90. {
  91. }
  92. virtual void AddHit(const BodyID &inResult) override
  93. {
  94. BodyLockRead lock(mBodyLockInterface, inResult);
  95. if (lock.Succeeded())
  96. {
  97. const Body &body = lock.GetBody();
  98. if (body.IsRigidBody() // TODO: We should support soft body vs soft body
  99. && mSoftBody.GetCollisionGroup().CanCollide(body.GetCollisionGroup()))
  100. {
  101. CollidingShape cs;
  102. cs.mCenterOfMassTransform = (mInverseTransform * body.GetCenterOfMassTransform()).ToMat44();
  103. cs.mShape = body.GetShape();
  104. cs.mBodyID = inResult;
  105. cs.mMotionType = body.GetMotionType();
  106. cs.mUpdateVelocities = false;
  107. cs.mFriction = mCombineFriction(mSoftBody, SubShapeID(), body, SubShapeID());
  108. cs.mRestitution = mCombineRestitution(mSoftBody, SubShapeID(), body, SubShapeID());
  109. if (cs.mMotionType == EMotionType::Dynamic)
  110. {
  111. const MotionProperties *mp = body.GetMotionProperties();
  112. cs.mInvMass = mp->GetInverseMass();
  113. cs.mInvInertia = mp->GetInverseInertiaForRotation(cs.mCenterOfMassTransform.GetRotation());
  114. cs.mOriginalLinearVelocity = cs.mLinearVelocity = mInverseTransform.Multiply3x3(mp->GetLinearVelocity());
  115. cs.mOriginalAngularVelocity = cs.mAngularVelocity = mInverseTransform.Multiply3x3(mp->GetAngularVelocity());
  116. }
  117. mHits.push_back(cs);
  118. }
  119. }
  120. }
  121. private:
  122. Body & mSoftBody;
  123. RMat44 mInverseTransform;
  124. const BodyLockInterface & mBodyLockInterface;
  125. ContactConstraintManager::CombineFunction mCombineFriction;
  126. ContactConstraintManager::CombineFunction mCombineRestitution;
  127. Array<CollidingShape> & mHits;
  128. };
  129. Collector collector(*inContext.mBody, inContext.mCenterOfMassTransform, inSystem, mCollidingShapes);
  130. AABox bounds = mLocalBounds;
  131. bounds.Encapsulate(mLocalPredictedBounds);
  132. bounds = bounds.Transformed(inContext.mCenterOfMassTransform);
  133. ObjectLayer layer = inContext.mBody->GetObjectLayer();
  134. DefaultBroadPhaseLayerFilter broadphase_layer_filter = inSystem.GetDefaultBroadPhaseLayerFilter(layer);
  135. DefaultObjectLayerFilter object_layer_filter = inSystem.GetDefaultLayerFilter(layer);
  136. inSystem.GetBroadPhaseQuery().CollideAABox(bounds, collector, broadphase_layer_filter, object_layer_filter);
  137. }
  138. void SoftBodyMotionProperties::DetermineCollisionPlanes(const SoftBodyUpdateContext &inContext, uint inVertexStart, uint inNumVertices)
  139. {
  140. JPH_PROFILE_FUNCTION();
  141. // Generate collision planes
  142. for (const CollidingShape &cs : mCollidingShapes)
  143. cs.mShape->CollideSoftBodyVertices(cs.mCenterOfMassTransform, Vec3::sReplicate(1.0f), mVertices.data() + inVertexStart, inNumVertices, inContext.mDeltaTime, inContext.mDisplacementDueToGravity, int(&cs - mCollidingShapes.data()));
  144. }
  145. void SoftBodyMotionProperties::ApplyPressure(const SoftBodyUpdateContext &inContext)
  146. {
  147. JPH_PROFILE_FUNCTION();
  148. float dt = inContext.mSubStepDeltaTime;
  149. float pressure_coefficient = mPressure;
  150. if (pressure_coefficient > 0.0f)
  151. {
  152. // Calculate total volume
  153. float six_volume = GetVolumeTimesSix();
  154. if (six_volume > 0.0f)
  155. {
  156. // Apply pressure
  157. // p = F / A = n R T / V (see https://en.wikipedia.org/wiki/Pressure)
  158. // Our pressure coefficient is n R T so the impulse is:
  159. // P = F dt = pressure_coefficient / V * A * dt
  160. float coefficient = pressure_coefficient * dt / six_volume; // Need to still multiply by 6 for the volume
  161. for (const Face &f : mSettings->mFaces)
  162. {
  163. Vec3 x1 = mVertices[f.mVertex[0]].mPosition;
  164. Vec3 x2 = mVertices[f.mVertex[1]].mPosition;
  165. Vec3 x3 = mVertices[f.mVertex[2]].mPosition;
  166. Vec3 impulse = coefficient * (x2 - x1).Cross(x3 - x1); // Area is half the cross product so need to still divide by 2
  167. for (uint32 i : f.mVertex)
  168. {
  169. Vertex &v = mVertices[i];
  170. v.mVelocity += v.mInvMass * impulse; // Want to divide by 3 because we spread over 3 vertices
  171. }
  172. }
  173. }
  174. }
  175. }
  176. void SoftBodyMotionProperties::IntegratePositions(const SoftBodyUpdateContext &inContext)
  177. {
  178. JPH_PROFILE_FUNCTION();
  179. float dt = inContext.mSubStepDeltaTime;
  180. float linear_damping = max(0.0f, 1.0f - GetLinearDamping() * dt); // See: MotionProperties::ApplyForceTorqueAndDragInternal
  181. // Integrate
  182. Vec3 sub_step_gravity = inContext.mGravity * dt;
  183. for (Vertex &v : mVertices)
  184. if (v.mInvMass > 0.0f)
  185. {
  186. // Gravity
  187. v.mVelocity += sub_step_gravity;
  188. // Damping
  189. v.mVelocity *= linear_damping;
  190. // Integrate
  191. v.mPreviousPosition = v.mPosition;
  192. v.mPosition += v.mVelocity * dt;
  193. }
  194. else
  195. {
  196. // Integrate
  197. v.mPreviousPosition = v.mPosition;
  198. v.mPosition += v.mVelocity * dt;
  199. }
  200. }
  201. void SoftBodyMotionProperties::ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext)
  202. {
  203. JPH_PROFILE_FUNCTION();
  204. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  205. // Satisfy volume constraints
  206. for (const Volume &v : mSettings->mVolumeConstraints)
  207. {
  208. Vertex &v1 = mVertices[v.mVertex[0]];
  209. Vertex &v2 = mVertices[v.mVertex[1]];
  210. Vertex &v3 = mVertices[v.mVertex[2]];
  211. Vertex &v4 = mVertices[v.mVertex[3]];
  212. Vec3 x1 = v1.mPosition;
  213. Vec3 x2 = v2.mPosition;
  214. Vec3 x3 = v3.mPosition;
  215. Vec3 x4 = v4.mPosition;
  216. // Calculate constraint equation
  217. Vec3 x1x2 = x2 - x1;
  218. Vec3 x1x3 = x3 - x1;
  219. Vec3 x1x4 = x4 - x1;
  220. float c = abs(x1x2.Cross(x1x3).Dot(x1x4)) - v.mSixRestVolume;
  221. // Calculate gradient of constraint equation
  222. Vec3 d1c = (x4 - x2).Cross(x3 - x2);
  223. Vec3 d2c = x1x3.Cross(x1x4);
  224. Vec3 d3c = x1x4.Cross(x1x2);
  225. Vec3 d4c = x1x2.Cross(x1x3);
  226. float w1 = v1.mInvMass;
  227. float w2 = v2.mInvMass;
  228. float w3 = v3.mInvMass;
  229. float w4 = v4.mInvMass;
  230. JPH_ASSERT(w1 > 0.0f || w2 > 0.0f || w3 > 0.0f || w4 > 0.0f);
  231. // Apply correction
  232. float lambda = -c / (w1 * d1c.LengthSq() + w2 * d2c.LengthSq() + w3 * d3c.LengthSq() + w4 * d4c.LengthSq() + v.mCompliance * inv_dt_sq);
  233. v1.mPosition += lambda * w1 * d1c;
  234. v2.mPosition += lambda * w2 * d2c;
  235. v3.mPosition += lambda * w3 * d3c;
  236. v4.mPosition += lambda * w4 * d4c;
  237. }
  238. }
  239. void SoftBodyMotionProperties::ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  240. {
  241. JPH_PROFILE_FUNCTION();
  242. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  243. // Satisfy edge constraints
  244. const Array<Edge> &edge_constraints = mSettings->mEdgeConstraints;
  245. for (uint i = inStartIndex; i < inEndIndex; ++i)
  246. {
  247. const Edge &e = edge_constraints[i];
  248. Vertex &v0 = mVertices[e.mVertex[0]];
  249. Vertex &v1 = mVertices[e.mVertex[1]];
  250. // Calculate current length
  251. Vec3 delta = v1.mPosition - v0.mPosition;
  252. float length = delta.Length();
  253. if (length > 0.0f)
  254. {
  255. // Apply correction
  256. Vec3 correction = delta * (length - e.mRestLength) / (length * (v0.mInvMass + v1.mInvMass + e.mCompliance * inv_dt_sq));
  257. v0.mPosition += v0.mInvMass * correction;
  258. v1.mPosition -= v1.mInvMass * correction;
  259. }
  260. }
  261. }
  262. void SoftBodyMotionProperties::ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext)
  263. {
  264. JPH_PROFILE_FUNCTION();
  265. float dt = inContext.mSubStepDeltaTime;
  266. float restitution_treshold = -2.0f * inContext.mGravity.Length() * dt;
  267. for (Vertex &v : mVertices)
  268. if (v.mInvMass > 0.0f)
  269. {
  270. // Remember previous velocity for restitution calculations
  271. Vec3 prev_v = v.mVelocity;
  272. // XPBD velocity update
  273. v.mVelocity = (v.mPosition - v.mPreviousPosition) / dt;
  274. // Satisfy collision constraint
  275. if (v.mCollidingShapeIndex >= 0)
  276. {
  277. // Check if there is a collision
  278. float projected_distance = -v.mCollisionPlane.SignedDistance(v.mPosition);
  279. if (projected_distance > 0.0f)
  280. {
  281. // Note that we already calculated the velocity, so this does not affect the velocity (next iteration starts by setting previous position to current position)
  282. Vec3 contact_normal = v.mCollisionPlane.GetNormal();
  283. v.mPosition += contact_normal * projected_distance;
  284. CollidingShape &cs = mCollidingShapes[v.mCollidingShapeIndex];
  285. // Apply friction as described in Detailed Rigid Body Simulation with Extended Position Based Dynamics - Matthias Muller et al.
  286. // See section 3.6:
  287. // Inverse mass: w1 = 1 / m1, w2 = 1 / m2 + (r2 x n)^T I^-1 (r2 x n) = 0 for a static object
  288. // r2 are the contact point relative to the center of mass of body 2
  289. // Lagrange multiplier for contact: lambda = -c / (w1 + w2)
  290. // Where c is the constraint equation (the distance to the plane, negative because penetrating)
  291. // Contact normal force: fn = lambda / dt^2
  292. // Delta velocity due to friction dv = -vt / |vt| * min(dt * friction * fn * (w1 + w2), |vt|) = -vt * min(-friction * c / (|vt| * dt), 1)
  293. // Note that I think there is an error in the paper, I added a mass term, see: https://github.com/matthias-research/pages/issues/29
  294. // Relative velocity: vr = v1 - v2 - omega2 x r2
  295. // Normal velocity: vn = vr . contact_normal
  296. // Tangential velocity: vt = vr - contact_normal * vn
  297. // Impulse: p = dv / (w1 + w2)
  298. // Changes in particle velocities:
  299. // v1 = v1 + p / m1
  300. // v2 = v2 - p / m2 (no change when colliding with a static body)
  301. // w2 = w2 - I^-1 (r2 x p) (no change when colliding with a static body)
  302. if (cs.mMotionType == EMotionType::Dynamic)
  303. {
  304. // Calculate normal and tangential velocity (equation 30)
  305. Vec3 r2 = v.mPosition - cs.mCenterOfMassTransform.GetTranslation();
  306. Vec3 v2 = cs.GetPointVelocity(r2);
  307. Vec3 relative_velocity = v.mVelocity - v2;
  308. Vec3 v_normal = contact_normal * contact_normal.Dot(relative_velocity);
  309. Vec3 v_tangential = relative_velocity - v_normal;
  310. float v_tangential_length = v_tangential.Length();
  311. // Calculate inverse effective mass
  312. Vec3 r2_cross_n = r2.Cross(contact_normal);
  313. float w2 = cs.mInvMass + r2_cross_n.Dot(cs.mInvInertia * r2_cross_n);
  314. float w1_plus_w2 = v.mInvMass + w2;
  315. // Calculate delta relative velocity due to friction (modified equation 31)
  316. Vec3 dv;
  317. if (v_tangential_length > 0.0f)
  318. dv = v_tangential * min(cs.mFriction * projected_distance / (v_tangential_length * dt), 1.0f);
  319. else
  320. dv = Vec3::sZero();
  321. // Calculate delta relative velocity due to restitution (equation 35)
  322. dv += v_normal;
  323. float prev_v_normal = (prev_v - v2).Dot(contact_normal);
  324. if (prev_v_normal < restitution_treshold)
  325. dv += cs.mRestitution * prev_v_normal * contact_normal;
  326. // Calculate impulse
  327. Vec3 p = dv / w1_plus_w2;
  328. // Apply impulse to particle
  329. v.mVelocity -= p * v.mInvMass;
  330. // Apply impulse to rigid body
  331. cs.mLinearVelocity += p * cs.mInvMass;
  332. cs.mAngularVelocity += cs.mInvInertia * r2.Cross(p);
  333. // Mark that the velocities of the body we hit need to be updated
  334. cs.mUpdateVelocities = true;
  335. }
  336. else
  337. {
  338. // Body is not moveable, equations are simpler
  339. // Calculate normal and tangential velocity (equation 30)
  340. Vec3 v_normal = contact_normal * contact_normal.Dot(v.mVelocity);
  341. Vec3 v_tangential = v.mVelocity - v_normal;
  342. float v_tangential_length = v_tangential.Length();
  343. // Apply friction (modified equation 31)
  344. if (v_tangential_length > 0.0f)
  345. v.mVelocity -= v_tangential * min(cs.mFriction * projected_distance / (v_tangential_length * dt), 1.0f);
  346. // Apply restitution (equation 35)
  347. v.mVelocity -= v_normal;
  348. float prev_v_normal = prev_v.Dot(contact_normal);
  349. if (prev_v_normal < restitution_treshold)
  350. v.mVelocity -= cs.mRestitution * prev_v_normal * contact_normal;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. void SoftBodyMotionProperties::UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  357. {
  358. JPH_PROFILE_FUNCTION();
  359. // Loop through vertices once more to update the global state
  360. float dt = ioContext.mDeltaTime;
  361. float max_linear_velocity_sq = Square(GetMaxLinearVelocity());
  362. float max_v_sq = 0.0f;
  363. Vec3 linear_velocity = Vec3::sZero(), angular_velocity = Vec3::sZero();
  364. mLocalPredictedBounds = mLocalBounds = { };
  365. for (Vertex &v : mVertices)
  366. {
  367. // Calculate max square velocity
  368. float v_sq = v.mVelocity.LengthSq();
  369. max_v_sq = max(max_v_sq, v_sq);
  370. // Clamp if velocity is too high
  371. if (v_sq > max_linear_velocity_sq)
  372. v.mVelocity *= sqrt(max_linear_velocity_sq / v_sq);
  373. // Calculate local linear/angular velocity
  374. linear_velocity += v.mVelocity;
  375. angular_velocity += v.mPosition.Cross(v.mVelocity);
  376. // Update local bounding box
  377. mLocalBounds.Encapsulate(v.mPosition);
  378. // Create predicted position for the next frame in order to detect collisions before they happen
  379. mLocalPredictedBounds.Encapsulate(v.mPosition + v.mVelocity * dt + ioContext.mDisplacementDueToGravity);
  380. // Reset collision data for the next iteration
  381. v.mCollidingShapeIndex = -1;
  382. v.mLargestPenetration = -FLT_MAX;
  383. }
  384. // Calculate linear/angular velocity of the body by averaging all vertices and bringing the value to world space
  385. float num_vertices_divider = float(max(int(mVertices.size()), 1));
  386. SetLinearVelocity(ioContext.mCenterOfMassTransform.Multiply3x3(linear_velocity / num_vertices_divider));
  387. SetAngularVelocity(ioContext.mCenterOfMassTransform.Multiply3x3(angular_velocity / num_vertices_divider));
  388. if (mUpdatePosition)
  389. {
  390. // Shift the body so that the position is the center of the local bounds
  391. Vec3 delta = mLocalBounds.GetCenter();
  392. ioContext.mDeltaPosition = ioContext.mCenterOfMassTransform.Multiply3x3(delta);
  393. for (Vertex &v : mVertices)
  394. v.mPosition -= delta;
  395. // Offset bounds to match new position
  396. mLocalBounds.Translate(-delta);
  397. mLocalPredictedBounds.Translate(-delta);
  398. }
  399. else
  400. ioContext.mDeltaPosition = Vec3::sZero();
  401. // Test if we should go to sleep
  402. if (GetAllowSleeping())
  403. {
  404. if (max_v_sq > inPhysicsSettings.mPointVelocitySleepThreshold)
  405. {
  406. ResetSleepTestTimer();
  407. ioContext.mCanSleep = ECanSleep::CannotSleep;
  408. }
  409. else
  410. ioContext.mCanSleep = AccumulateSleepTime(dt, inPhysicsSettings.mTimeBeforeSleep);
  411. }
  412. else
  413. ioContext.mCanSleep = ECanSleep::CannotSleep;
  414. }
  415. void SoftBodyMotionProperties::UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, PhysicsSystem &inSystem)
  416. {
  417. JPH_PROFILE_FUNCTION();
  418. // Write back velocity deltas
  419. BodyInterface &body_interface = inSystem.GetBodyInterfaceNoLock();
  420. for (const CollidingShape &cs : mCollidingShapes)
  421. if (cs.mUpdateVelocities)
  422. body_interface.AddLinearAndAngularVelocity(cs.mBodyID, inContext.mCenterOfMassTransform.Multiply3x3(cs.mLinearVelocity - cs.mOriginalLinearVelocity), inContext.mCenterOfMassTransform.Multiply3x3(cs.mAngularVelocity - cs.mOriginalAngularVelocity));
  423. // Clear colliding shapes to avoid hanging on to references to shapes
  424. mCollidingShapes.clear();
  425. }
  426. void SoftBodyMotionProperties::InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext)
  427. {
  428. JPH_PROFILE_FUNCTION();
  429. // Store body
  430. ioContext.mBody = &inSoftBody;
  431. ioContext.mMotionProperties = this;
  432. // Convert gravity to local space
  433. ioContext.mCenterOfMassTransform = inSoftBody.GetCenterOfMassTransform();
  434. ioContext.mGravity = ioContext.mCenterOfMassTransform.Multiply3x3Transposed(GetGravityFactor() * inSystem.GetGravity());
  435. // Calculate delta time for sub step
  436. ioContext.mDeltaTime = inDeltaTime;
  437. ioContext.mSubStepDeltaTime = inDeltaTime / mNumIterations;
  438. // Calculate total displacement we'll have due to gravity over all sub steps
  439. // The total displacement as produced by our integrator can be written as: Sum(i * g * dt^2, i = 0..mNumIterations).
  440. // This is bigger than 0.5 * g * dt^2 because we first increment the velocity and then update the position
  441. // Using Sum(i, i = 0..n) = n * (n + 1) / 2 we can write this as:
  442. ioContext.mDisplacementDueToGravity = (0.5f * mNumIterations * (mNumIterations + 1) * Square(ioContext.mSubStepDeltaTime)) * ioContext.mGravity;
  443. }
  444. void SoftBodyMotionProperties::StartNextIteration(const SoftBodyUpdateContext &ioContext)
  445. {
  446. ApplyPressure(ioContext);
  447. IntegratePositions(ioContext);
  448. ApplyVolumeConstraints(ioContext);
  449. }
  450. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext)
  451. {
  452. // Do a relaxed read first to see if there is any work to do (this prevents us from doing expensive atomic operations and also prevents us from continuously incrementing the counter and overflowing it)
  453. uint num_vertices = (uint)mVertices.size();
  454. if (ioContext.mNextCollisionVertex.load(memory_order_relaxed) < num_vertices)
  455. {
  456. // Fetch next batch of vertices to process
  457. uint next_vertex = ioContext.mNextCollisionVertex.fetch_add(SoftBodyUpdateContext::cVertexCollisionBatch, memory_order_acquire);
  458. if (next_vertex < num_vertices)
  459. {
  460. // Process collision planes
  461. uint num_vertices_to_process = min(SoftBodyUpdateContext::cVertexCollisionBatch, num_vertices - next_vertex);
  462. DetermineCollisionPlanes(ioContext, next_vertex, num_vertices_to_process);
  463. uint vertices_processed = ioContext.mNumCollisionVerticesProcessed.fetch_add(SoftBodyUpdateContext::cVertexCollisionBatch, memory_order_release) + num_vertices_to_process;
  464. if (vertices_processed >= num_vertices)
  465. {
  466. // Start the first iteration
  467. JPH_IF_ENABLE_ASSERTS(uint iteration =) ioContext.mNextIteration.fetch_add(1, memory_order_relaxed);
  468. JPH_ASSERT(iteration == 0);
  469. StartNextIteration(ioContext);
  470. ioContext.mState.store(SoftBodyUpdateContext::EState::ApplyEdgeConstraints, memory_order_release);
  471. }
  472. return EStatus::DidWork;
  473. }
  474. }
  475. return EStatus::NoWork;
  476. }
  477. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelApplyEdgeConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  478. {
  479. // Do a relaxed read first to see if there is any work to do (this prevents us from doing expensive atomic operations and also prevents us from continuously incrementing the counter and overflowing it)
  480. uint num_groups = (uint)mSettings->mEdgeGroupEndIndices.size();
  481. JPH_ASSERT(num_groups > 0, "SoftBodySharedSettings::Optimize should have been called!");
  482. uint32 edge_group, edge_start_idx;
  483. SoftBodyUpdateContext::sGetEdgeGroupAndStartIdx(ioContext.mNextEdgeConstraint.load(memory_order_relaxed), edge_group, edge_start_idx);
  484. if (edge_group < num_groups && edge_start_idx < mSettings->GetEdgeGroupSize(edge_group))
  485. {
  486. // Fetch the next batch of edges to process
  487. uint64 next_edge_batch = ioContext.mNextEdgeConstraint.fetch_add(SoftBodyUpdateContext::cEdgeConstraintBatch, memory_order_acquire);
  488. SoftBodyUpdateContext::sGetEdgeGroupAndStartIdx(next_edge_batch, edge_group, edge_start_idx);
  489. if (edge_group < num_groups)
  490. {
  491. bool non_parallel_group = edge_group == num_groups - 1; // Last group is the non-parallel group and goes as a whole
  492. uint edge_group_size = mSettings->GetEdgeGroupSize(edge_group);
  493. if (non_parallel_group? edge_start_idx == 0 : edge_start_idx < edge_group_size)
  494. {
  495. // Process edges
  496. uint num_edges_to_process = non_parallel_group? edge_group_size : min(SoftBodyUpdateContext::cEdgeConstraintBatch, edge_group_size - edge_start_idx);
  497. if (edge_group > 0)
  498. edge_start_idx += mSettings->mEdgeGroupEndIndices[edge_group - 1];
  499. ApplyEdgeConstraints(ioContext, edge_start_idx, edge_start_idx + num_edges_to_process);
  500. // Test if we're at the end of this group
  501. uint edge_constraints_processed = ioContext.mNumEdgeConstraintsProcessed.fetch_add(num_edges_to_process, memory_order_relaxed) + num_edges_to_process;
  502. if (edge_constraints_processed >= edge_group_size)
  503. {
  504. // Non parallel group is the last group (which is also the only group that can be empty)
  505. if (non_parallel_group || mSettings->GetEdgeGroupSize(edge_group + 1) == 0)
  506. {
  507. // Finish the iteration
  508. ApplyCollisionConstraintsAndUpdateVelocities(ioContext);
  509. uint iteration = ioContext.mNextIteration.fetch_add(1, memory_order_relaxed);
  510. if (iteration < mNumIterations)
  511. {
  512. // Start a new iteration
  513. StartNextIteration(ioContext);
  514. // Reset next edge to process
  515. ioContext.mNumEdgeConstraintsProcessed.store(0, memory_order_relaxed);
  516. ioContext.mNextEdgeConstraint.store(0, memory_order_release);
  517. }
  518. else
  519. {
  520. // On final iteration we update the state
  521. UpdateSoftBodyState(ioContext, inPhysicsSettings);
  522. ioContext.mState.store(SoftBodyUpdateContext::EState::Done, memory_order_release);
  523. return EStatus::Done;
  524. }
  525. }
  526. else
  527. {
  528. // Next group
  529. ioContext.mNumEdgeConstraintsProcessed.store(0, memory_order_relaxed);
  530. ioContext.mNextEdgeConstraint.store(SoftBodyUpdateContext::sGetEdgeGroupStart(edge_group + 1), memory_order_release);
  531. }
  532. }
  533. return EStatus::DidWork;
  534. }
  535. }
  536. }
  537. return EStatus::NoWork;
  538. }
  539. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  540. {
  541. switch (ioContext.mState.load(memory_order_relaxed))
  542. {
  543. case SoftBodyUpdateContext::EState::DetermineCollisionPlanes:
  544. return ParallelDetermineCollisionPlanes(ioContext);
  545. case SoftBodyUpdateContext::EState::ApplyEdgeConstraints:
  546. return ParallelApplyEdgeConstraints(ioContext, inPhysicsSettings);
  547. case SoftBodyUpdateContext::EState::Done:
  548. return EStatus::Done;
  549. default:
  550. JPH_ASSERT(false);
  551. return EStatus::NoWork;
  552. }
  553. }
  554. #ifdef JPH_DEBUG_RENDERER
  555. void SoftBodyMotionProperties::DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  556. {
  557. for (const Vertex &v : mVertices)
  558. inRenderer->DrawMarker(inCenterOfMassTransform * v.mPosition, Color::sRed, 0.05f);
  559. }
  560. void SoftBodyMotionProperties::DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  561. {
  562. for (const Edge &e : mSettings->mEdgeConstraints)
  563. inRenderer->DrawLine(inCenterOfMassTransform * mVertices[e.mVertex[0]].mPosition, inCenterOfMassTransform * mVertices[e.mVertex[1]].mPosition, Color::sWhite);
  564. }
  565. void SoftBodyMotionProperties::DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  566. {
  567. for (const Volume &v : mSettings->mVolumeConstraints)
  568. {
  569. RVec3 x1 = inCenterOfMassTransform * mVertices[v.mVertex[0]].mPosition;
  570. RVec3 x2 = inCenterOfMassTransform * mVertices[v.mVertex[1]].mPosition;
  571. RVec3 x3 = inCenterOfMassTransform * mVertices[v.mVertex[2]].mPosition;
  572. RVec3 x4 = inCenterOfMassTransform * mVertices[v.mVertex[3]].mPosition;
  573. inRenderer->DrawTriangle(x1, x3, x2, Color::sYellow, DebugRenderer::ECastShadow::On);
  574. inRenderer->DrawTriangle(x2, x3, x4, Color::sYellow, DebugRenderer::ECastShadow::On);
  575. inRenderer->DrawTriangle(x1, x4, x3, Color::sYellow, DebugRenderer::ECastShadow::On);
  576. inRenderer->DrawTriangle(x1, x2, x4, Color::sYellow, DebugRenderer::ECastShadow::On);
  577. }
  578. }
  579. void SoftBodyMotionProperties::DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  580. {
  581. inRenderer->DrawWireBox(inCenterOfMassTransform, mLocalPredictedBounds, Color::sRed);
  582. }
  583. #endif // JPH_DEBUG_RENDERER
  584. void SoftBodyMotionProperties::SaveState(StateRecorder &inStream) const
  585. {
  586. MotionProperties::SaveState(inStream);
  587. for (const Vertex &v : mVertices)
  588. {
  589. inStream.Write(v.mPreviousPosition);
  590. inStream.Write(v.mPosition);
  591. inStream.Write(v.mVelocity);
  592. }
  593. inStream.Write(mLocalBounds.mMin);
  594. inStream.Write(mLocalBounds.mMax);
  595. inStream.Write(mLocalPredictedBounds.mMin);
  596. inStream.Write(mLocalPredictedBounds.mMax);
  597. }
  598. void SoftBodyMotionProperties::RestoreState(StateRecorder &inStream)
  599. {
  600. MotionProperties::RestoreState(inStream);
  601. for (Vertex &v : mVertices)
  602. {
  603. inStream.Read(v.mPreviousPosition);
  604. inStream.Read(v.mPosition);
  605. inStream.Read(v.mVelocity);
  606. }
  607. inStream.Read(mLocalBounds.mMin);
  608. inStream.Read(mLocalBounds.mMax);
  609. inStream.Read(mLocalPredictedBounds.mMin);
  610. inStream.Read(mLocalPredictedBounds.mMax);
  611. }
  612. JPH_NAMESPACE_END