SoftBodyMotionProperties.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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/SoftBody/SoftBodyContactListener.h>
  8. #include <Jolt/Physics/SoftBody/SoftBodyManifold.h>
  9. #include <Jolt/Physics/PhysicsSystem.h>
  10. #ifdef JPH_DEBUG_RENDERER
  11. #include <Jolt/Renderer/DebugRenderer.h>
  12. #endif // JPH_DEBUG_RENDERER
  13. JPH_NAMESPACE_BEGIN
  14. using namespace JPH::literals;
  15. void SoftBodyMotionProperties::CalculateMassAndInertia()
  16. {
  17. MassProperties mp;
  18. for (const Vertex &v : mVertices)
  19. if (v.mInvMass > 0.0f)
  20. {
  21. Vec3 pos = v.mPosition;
  22. // Accumulate mass
  23. float mass = 1.0f / v.mInvMass;
  24. mp.mMass += mass;
  25. // Inertia tensor, diagonal
  26. // See equations https://en.wikipedia.org/wiki/Moment_of_inertia section 'Inertia Tensor'
  27. for (int i = 0; i < 3; ++i)
  28. mp.mInertia(i, i) += mass * (Square(pos[(i + 1) % 3]) + Square(pos[(i + 2) % 3]));
  29. // Inertia tensor off diagonal
  30. for (int i = 0; i < 3; ++i)
  31. for (int j = 0; j < 3; ++j)
  32. if (i != j)
  33. mp.mInertia(i, j) -= mass * pos[i] * pos[j];
  34. }
  35. else
  36. {
  37. // If one vertex is kinematic, the entire body will have infinite mass and inertia
  38. SetInverseMass(0.0f);
  39. SetInverseInertia(Vec3::sZero(), Quat::sIdentity());
  40. return;
  41. }
  42. SetMassProperties(EAllowedDOFs::All, mp);
  43. }
  44. void SoftBodyMotionProperties::Initialize(const SoftBodyCreationSettings &inSettings)
  45. {
  46. // Store settings
  47. mSettings = inSettings.mSettings;
  48. mNumIterations = inSettings.mNumIterations;
  49. mPressure = inSettings.mPressure;
  50. mUpdatePosition = inSettings.mUpdatePosition;
  51. // Initialize vertices
  52. mVertices.resize(inSettings.mSettings->mVertices.size());
  53. Mat44 rotation = inSettings.mMakeRotationIdentity? Mat44::sRotation(inSettings.mRotation) : Mat44::sIdentity();
  54. for (Array<Vertex>::size_type v = 0, s = mVertices.size(); v < s; ++v)
  55. {
  56. const SoftBodySharedSettings::Vertex &in_vertex = inSettings.mSettings->mVertices[v];
  57. Vertex &out_vertex = mVertices[v];
  58. out_vertex.mPreviousPosition = out_vertex.mPosition = rotation * Vec3(in_vertex.mPosition);
  59. out_vertex.mVelocity = rotation.Multiply3x3(Vec3(in_vertex.mVelocity));
  60. out_vertex.mCollidingShapeIndex = -1;
  61. out_vertex.mHasContact = false;
  62. out_vertex.mLargestPenetration = -FLT_MAX;
  63. out_vertex.mInvMass = in_vertex.mInvMass;
  64. mLocalBounds.Encapsulate(out_vertex.mPosition);
  65. }
  66. // Allocate space for skinned vertices
  67. if (!inSettings.mSettings->mSkinnedConstraints.empty())
  68. mSkinState.resize(mVertices.size());
  69. // We don't know delta time yet, so we can't predict the bounds and use the local bounds as the predicted bounds
  70. mLocalPredictedBounds = mLocalBounds;
  71. CalculateMassAndInertia();
  72. }
  73. float SoftBodyMotionProperties::GetVolumeTimesSix() const
  74. {
  75. float six_volume = 0.0f;
  76. for (const Face &f : mSettings->mFaces)
  77. {
  78. Vec3 x1 = mVertices[f.mVertex[0]].mPosition;
  79. Vec3 x2 = mVertices[f.mVertex[1]].mPosition;
  80. Vec3 x3 = mVertices[f.mVertex[2]].mPosition;
  81. 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
  82. }
  83. return six_volume;
  84. }
  85. void SoftBodyMotionProperties::DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface)
  86. {
  87. JPH_PROFILE_FUNCTION();
  88. struct Collector : public CollideShapeBodyCollector
  89. {
  90. Collector(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface, Array<CollidingShape> &ioHits) :
  91. mContext(inContext),
  92. mInverseTransform(inContext.mCenterOfMassTransform.InversedRotationTranslation()),
  93. mBodyLockInterface(inBodyLockInterface),
  94. mCombineFriction(inSystem.GetCombineFriction()),
  95. mCombineRestitution(inSystem.GetCombineRestitution()),
  96. mHits(ioHits)
  97. {
  98. }
  99. virtual void AddHit(const BodyID &inResult) override
  100. {
  101. BodyLockRead lock(mBodyLockInterface, inResult);
  102. if (lock.Succeeded())
  103. {
  104. const Body &soft_body = *mContext.mBody;
  105. const Body &body = lock.GetBody();
  106. if (body.IsRigidBody() // TODO: We should support soft body vs soft body
  107. && soft_body.GetCollisionGroup().CanCollide(body.GetCollisionGroup()))
  108. {
  109. // Call the contact listener to see if we should accept this contact
  110. // If there is no contact listener then we can ignore the contact if the other body is a sensor
  111. SoftBodyContactSettings settings;
  112. settings.mIsSensor = body.IsSensor();
  113. if (mContext.mContactListener == nullptr? !settings.mIsSensor : mContext.mContactListener->OnSoftBodyContactValidate(soft_body, body, settings) == SoftBodyValidateResult::AcceptContact)
  114. {
  115. CollidingShape cs;
  116. cs.mCenterOfMassTransform = (mInverseTransform * body.GetCenterOfMassTransform()).ToMat44();
  117. cs.mShape = body.GetShape();
  118. cs.mBodyID = inResult;
  119. cs.mMotionType = body.GetMotionType();
  120. cs.mIsSensor = settings.mIsSensor;
  121. cs.mUpdateVelocities = false;
  122. cs.mFriction = mCombineFriction(soft_body, SubShapeID(), body, SubShapeID());
  123. cs.mRestitution = mCombineRestitution(soft_body, SubShapeID(), body, SubShapeID());
  124. if (cs.mMotionType == EMotionType::Dynamic)
  125. {
  126. const MotionProperties *mp = body.GetMotionProperties();
  127. cs.mInvMass = settings.mInvMassScale2 * mp->GetInverseMass();
  128. cs.mInvInertia = settings.mInvInertiaScale2 * mp->GetInverseInertiaForRotation(cs.mCenterOfMassTransform.GetRotation());
  129. cs.mSoftBodyInvMassScale = settings.mInvMassScale1;
  130. cs.mOriginalLinearVelocity = cs.mLinearVelocity = mInverseTransform.Multiply3x3(mp->GetLinearVelocity());
  131. cs.mOriginalAngularVelocity = cs.mAngularVelocity = mInverseTransform.Multiply3x3(mp->GetAngularVelocity());
  132. }
  133. mHits.push_back(cs);
  134. }
  135. }
  136. }
  137. }
  138. private:
  139. const SoftBodyUpdateContext &mContext;
  140. RMat44 mInverseTransform;
  141. const BodyLockInterface & mBodyLockInterface;
  142. ContactConstraintManager::CombineFunction mCombineFriction;
  143. ContactConstraintManager::CombineFunction mCombineRestitution;
  144. Array<CollidingShape> & mHits;
  145. };
  146. Collector collector(inContext, inSystem, inBodyLockInterface, mCollidingShapes);
  147. AABox bounds = mLocalBounds;
  148. bounds.Encapsulate(mLocalPredictedBounds);
  149. bounds = bounds.Transformed(inContext.mCenterOfMassTransform);
  150. bounds.ExpandBy(Vec3::sReplicate(mSettings->mVertexRadius));
  151. ObjectLayer layer = inContext.mBody->GetObjectLayer();
  152. DefaultBroadPhaseLayerFilter broadphase_layer_filter = inSystem.GetDefaultBroadPhaseLayerFilter(layer);
  153. DefaultObjectLayerFilter object_layer_filter = inSystem.GetDefaultLayerFilter(layer);
  154. inSystem.GetBroadPhaseQuery().CollideAABox(bounds, collector, broadphase_layer_filter, object_layer_filter);
  155. }
  156. void SoftBodyMotionProperties::DetermineCollisionPlanes(const SoftBodyUpdateContext &inContext, uint inVertexStart, uint inNumVertices)
  157. {
  158. JPH_PROFILE_FUNCTION();
  159. // Generate collision planes
  160. for (const CollidingShape &cs : mCollidingShapes)
  161. cs.mShape->CollideSoftBodyVertices(cs.mCenterOfMassTransform, Vec3::sReplicate(1.0f), mVertices.data() + inVertexStart, inNumVertices, inContext.mDeltaTime, inContext.mDisplacementDueToGravity, int(&cs - mCollidingShapes.data()));
  162. }
  163. void SoftBodyMotionProperties::ApplyPressure(const SoftBodyUpdateContext &inContext)
  164. {
  165. JPH_PROFILE_FUNCTION();
  166. float dt = inContext.mSubStepDeltaTime;
  167. float pressure_coefficient = mPressure;
  168. if (pressure_coefficient > 0.0f)
  169. {
  170. // Calculate total volume
  171. float six_volume = GetVolumeTimesSix();
  172. if (six_volume > 0.0f)
  173. {
  174. // Apply pressure
  175. // p = F / A = n R T / V (see https://en.wikipedia.org/wiki/Pressure)
  176. // Our pressure coefficient is n R T so the impulse is:
  177. // P = F dt = pressure_coefficient / V * A * dt
  178. float coefficient = pressure_coefficient * dt / six_volume; // Need to still multiply by 6 for the volume
  179. for (const Face &f : mSettings->mFaces)
  180. {
  181. Vec3 x1 = mVertices[f.mVertex[0]].mPosition;
  182. Vec3 x2 = mVertices[f.mVertex[1]].mPosition;
  183. Vec3 x3 = mVertices[f.mVertex[2]].mPosition;
  184. Vec3 impulse = coefficient * (x2 - x1).Cross(x3 - x1); // Area is half the cross product so need to still divide by 2
  185. for (uint32 i : f.mVertex)
  186. {
  187. Vertex &v = mVertices[i];
  188. v.mVelocity += v.mInvMass * impulse; // Want to divide by 3 because we spread over 3 vertices
  189. }
  190. }
  191. }
  192. }
  193. }
  194. void SoftBodyMotionProperties::IntegratePositions(const SoftBodyUpdateContext &inContext)
  195. {
  196. JPH_PROFILE_FUNCTION();
  197. float dt = inContext.mSubStepDeltaTime;
  198. float linear_damping = max(0.0f, 1.0f - GetLinearDamping() * dt); // See: MotionProperties::ApplyForceTorqueAndDragInternal
  199. // Integrate
  200. Vec3 sub_step_gravity = inContext.mGravity * dt;
  201. for (Vertex &v : mVertices)
  202. if (v.mInvMass > 0.0f)
  203. {
  204. // Gravity
  205. v.mVelocity += sub_step_gravity;
  206. // Damping
  207. v.mVelocity *= linear_damping;
  208. // Integrate
  209. v.mPreviousPosition = v.mPosition;
  210. v.mPosition += v.mVelocity * dt;
  211. }
  212. else
  213. {
  214. // Integrate
  215. v.mPreviousPosition = v.mPosition;
  216. v.mPosition += v.mVelocity * dt;
  217. }
  218. }
  219. void SoftBodyMotionProperties::ApplyBendConstraints(const SoftBodyUpdateContext &inContext)
  220. {
  221. JPH_PROFILE_FUNCTION();
  222. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  223. for (const DihedralBend &b : mSettings->mDihedralBendConstraints)
  224. {
  225. Vertex &v0 = mVertices[b.mVertex[0]];
  226. Vertex &v1 = mVertices[b.mVertex[1]];
  227. Vertex &v2 = mVertices[b.mVertex[2]];
  228. Vertex &v3 = mVertices[b.mVertex[3]];
  229. // Get positions
  230. Vec3 x0 = v0.mPosition;
  231. Vec3 x1 = v1.mPosition;
  232. Vec3 x2 = v2.mPosition;
  233. Vec3 x3 = v3.mPosition;
  234. /*
  235. x2
  236. e1/ \e3
  237. / \
  238. x0----x1
  239. \ e0 /
  240. e2\ /e4
  241. x3
  242. */
  243. // Calculate the shared edge of the triangles
  244. Vec3 e = x1 - x0;
  245. float e_len = e.Length();
  246. if (e_len < 1.0e-6f)
  247. continue;
  248. // Calculate the normals of the triangles
  249. Vec3 x1x2 = x2 - x1;
  250. Vec3 x1x3 = x3 - x1;
  251. Vec3 n1 = (x2 - x0).Cross(x1x2);
  252. Vec3 n2 = x1x3.Cross(x3 - x0);
  253. float n1_len_sq = n1.LengthSq();
  254. float n2_len_sq = n2.LengthSq();
  255. float n1_len_sq_n2_len_sq = n1_len_sq * n2_len_sq;
  256. if (n1_len_sq_n2_len_sq < 1.0e-24f)
  257. continue;
  258. // Calculate constraint equation
  259. // As per "Strain Based Dynamics" Appendix A we need to negate the gradients when (n1 x n2) . e > 0, instead we make sure that the sign of the constraint equation is correct
  260. float sign = Sign(n2.Cross(n1).Dot(e));
  261. float d = n1.Dot(n2) / sqrt(n1_len_sq_n2_len_sq);
  262. float c = sign * ACos(d) - b.mInitialAngle;
  263. // Ensure the range is -PI to PI
  264. if (c > JPH_PI)
  265. c -= 2.0f * JPH_PI;
  266. else if (c < -JPH_PI)
  267. c += 2.0f * JPH_PI;
  268. // Calculate gradient of constraint equation
  269. // Taken from "Strain Based Dynamics" - Matthias Muller et al. (Appendix A)
  270. // with p1 = x2, p2 = x3, p3 = x0 and p4 = x1
  271. // which in turn is based on "Simulation of Clothing with Folds and Wrinkles" - R. Bridson et al. (Section 4)
  272. n1 /= n1_len_sq;
  273. n2 /= n2_len_sq;
  274. Vec3 d0c = (x1x2.Dot(e) * n1 + x1x3.Dot(e) * n2) / e_len;
  275. Vec3 d2c = e_len * n1;
  276. Vec3 d3c = e_len * n2;
  277. // The sum of the gradients must be zero (see "Strain Based Dynamics" section 4)
  278. Vec3 d1c = -d0c - d2c - d3c;
  279. // Get masses
  280. float w0 = v0.mInvMass;
  281. float w1 = v1.mInvMass;
  282. float w2 = v2.mInvMass;
  283. float w3 = v3.mInvMass;
  284. // Calculate -lambda
  285. float denom = w0 * d0c.LengthSq() + w1 * d1c.LengthSq() + w2 * d2c.LengthSq() + w3 * d3c.LengthSq() + b.mCompliance * inv_dt_sq;
  286. if (denom < 1.0e-12f)
  287. continue;
  288. float minus_lambda = c / denom;
  289. // Apply correction
  290. v0.mPosition = x0 - minus_lambda * w0 * d0c;
  291. v1.mPosition = x1 - minus_lambda * w1 * d1c;
  292. v2.mPosition = x2 - minus_lambda * w2 * d2c;
  293. v3.mPosition = x3 - minus_lambda * w3 * d3c;
  294. }
  295. }
  296. void SoftBodyMotionProperties::ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext)
  297. {
  298. JPH_PROFILE_FUNCTION();
  299. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  300. // Satisfy volume constraints
  301. for (const Volume &v : mSettings->mVolumeConstraints)
  302. {
  303. Vertex &v1 = mVertices[v.mVertex[0]];
  304. Vertex &v2 = mVertices[v.mVertex[1]];
  305. Vertex &v3 = mVertices[v.mVertex[2]];
  306. Vertex &v4 = mVertices[v.mVertex[3]];
  307. Vec3 x1 = v1.mPosition;
  308. Vec3 x2 = v2.mPosition;
  309. Vec3 x3 = v3.mPosition;
  310. Vec3 x4 = v4.mPosition;
  311. // Calculate constraint equation
  312. Vec3 x1x2 = x2 - x1;
  313. Vec3 x1x3 = x3 - x1;
  314. Vec3 x1x4 = x4 - x1;
  315. float c = abs(x1x2.Cross(x1x3).Dot(x1x4)) - v.mSixRestVolume;
  316. // Calculate gradient of constraint equation
  317. Vec3 d1c = (x4 - x2).Cross(x3 - x2);
  318. Vec3 d2c = x1x3.Cross(x1x4);
  319. Vec3 d3c = x1x4.Cross(x1x2);
  320. Vec3 d4c = x1x2.Cross(x1x3);
  321. // Get masses
  322. float w1 = v1.mInvMass;
  323. float w2 = v2.mInvMass;
  324. float w3 = v3.mInvMass;
  325. float w4 = v4.mInvMass;
  326. // Calculate -lambda
  327. float denom = w1 * d1c.LengthSq() + w2 * d2c.LengthSq() + w3 * d3c.LengthSq() + w4 * d4c.LengthSq() + v.mCompliance * inv_dt_sq;
  328. if (denom < 1.0e-12f)
  329. continue;
  330. float minus_lambda = c / denom;
  331. // Apply correction
  332. v1.mPosition = x1 - minus_lambda * w1 * d1c;
  333. v2.mPosition = x2 - minus_lambda * w2 * d2c;
  334. v3.mPosition = x3 - minus_lambda * w3 * d3c;
  335. v4.mPosition = x4 - minus_lambda * w4 * d4c;
  336. }
  337. }
  338. void SoftBodyMotionProperties::ApplySkinConstraints([[maybe_unused]] const SoftBodyUpdateContext &inContext)
  339. {
  340. // Early out if nothing to do
  341. if (mSettings->mSkinnedConstraints.empty() || !mEnableSkinConstraints)
  342. return;
  343. JPH_ASSERT(mSkinStateTransform == inContext.mCenterOfMassTransform, "Skinning state is stale, artifacts will show!");
  344. // Apply the constraints
  345. Vertex *vertices = mVertices.data();
  346. const SkinState *skin_states = mSkinState.data();
  347. for (const Skinned &s : mSettings->mSkinnedConstraints)
  348. {
  349. Vertex &vertex = vertices[s.mVertex];
  350. const SkinState &skin_state = skin_states[s.mVertex];
  351. float max_distance = s.mMaxDistance * mSkinnedMaxDistanceMultiplier;
  352. if (max_distance > 0.0f)
  353. {
  354. // Move vertex if it violated the back stop
  355. if (s.mBackStopDistance < max_distance)
  356. {
  357. // Center of the back stop sphere
  358. Vec3 center = skin_state.mPosition - skin_state.mNormal * (s.mBackStopDistance + s.mBackStopRadius);
  359. // Check if we're inside the back stop sphere
  360. Vec3 delta = vertex.mPosition - center;
  361. float delta_len_sq = delta.LengthSq();
  362. if (delta_len_sq < Square(s.mBackStopRadius))
  363. {
  364. // Push the vertex to the surface of the back stop sphere
  365. float delta_len = sqrt(delta_len_sq);
  366. vertex.mPosition = delta_len > 0.0f?
  367. center + delta * (s.mBackStopRadius / delta_len)
  368. : center + skin_state.mNormal * s.mBackStopRadius;
  369. }
  370. }
  371. // Clamp vertex distance to max distance from skinned position
  372. if (max_distance < FLT_MAX)
  373. {
  374. Vec3 delta = vertex.mPosition - skin_state.mPosition;
  375. float delta_len_sq = delta.LengthSq();
  376. float max_distance_sq = Square(max_distance);
  377. if (delta_len_sq > max_distance_sq)
  378. vertex.mPosition = skin_state.mPosition + delta * sqrt(max_distance_sq / delta_len_sq);
  379. }
  380. }
  381. else
  382. {
  383. // Kinematic: Just update the vertex position
  384. vertex.mPosition = skin_state.mPosition;
  385. }
  386. }
  387. }
  388. void SoftBodyMotionProperties::ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  389. {
  390. JPH_PROFILE_FUNCTION();
  391. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  392. // Satisfy edge constraints
  393. const Array<Edge> &edge_constraints = mSettings->mEdgeConstraints;
  394. for (uint i = inStartIndex; i < inEndIndex; ++i)
  395. {
  396. const Edge &e = edge_constraints[i];
  397. Vertex &v0 = mVertices[e.mVertex[0]];
  398. Vertex &v1 = mVertices[e.mVertex[1]];
  399. // Get positions
  400. Vec3 x0 = v0.mPosition;
  401. Vec3 x1 = v1.mPosition;
  402. // Calculate current length
  403. Vec3 delta = x1 - x0;
  404. float length = delta.Length();
  405. // Apply correction
  406. float denom = length * (v0.mInvMass + v1.mInvMass + e.mCompliance * inv_dt_sq);
  407. if (denom < 1.0e-12f)
  408. continue;
  409. Vec3 correction = delta * (length - e.mRestLength) / denom;
  410. v0.mPosition = x0 + v0.mInvMass * correction;
  411. v1.mPosition = x1 - v1.mInvMass * correction;
  412. }
  413. }
  414. void SoftBodyMotionProperties::ApplyLRAConstraints()
  415. {
  416. JPH_PROFILE_FUNCTION();
  417. // Satisfy LRA constraints
  418. Vertex *vertices = mVertices.data();
  419. for (const LRA &lra : mSettings->mLRAConstraints)
  420. {
  421. JPH_ASSERT(lra.mVertex[0] < mVertices.size());
  422. JPH_ASSERT(lra.mVertex[1] < mVertices.size());
  423. const Vertex &vertex0 = vertices[lra.mVertex[0]];
  424. Vertex &vertex1 = vertices[lra.mVertex[1]];
  425. Vec3 x0 = vertex0.mPosition;
  426. Vec3 delta = vertex1.mPosition - x0;
  427. float delta_len_sq = delta.LengthSq();
  428. if (delta_len_sq > Square(lra.mMaxDistance))
  429. vertex1.mPosition = x0 + delta * lra.mMaxDistance / sqrt(delta_len_sq);
  430. }
  431. }
  432. void SoftBodyMotionProperties::ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext)
  433. {
  434. JPH_PROFILE_FUNCTION();
  435. float dt = inContext.mSubStepDeltaTime;
  436. float restitution_treshold = -2.0f * inContext.mGravity.Length() * dt;
  437. float vertex_radius = mSettings->mVertexRadius;
  438. for (Vertex &v : mVertices)
  439. if (v.mInvMass > 0.0f)
  440. {
  441. // Remember previous velocity for restitution calculations
  442. Vec3 prev_v = v.mVelocity;
  443. // XPBD velocity update
  444. v.mVelocity = (v.mPosition - v.mPreviousPosition) / dt;
  445. // Satisfy collision constraint
  446. if (v.mCollidingShapeIndex >= 0)
  447. {
  448. // Check if there is a collision
  449. float projected_distance = -v.mCollisionPlane.SignedDistance(v.mPosition) + vertex_radius;
  450. if (projected_distance > 0.0f)
  451. {
  452. // Remember that there was a collision
  453. v.mHasContact = true;
  454. mHasContact = true;
  455. // Sensors should not have a collision response
  456. CollidingShape &cs = mCollidingShapes[v.mCollidingShapeIndex];
  457. if (!cs.mIsSensor)
  458. {
  459. // Note that we already calculated the velocity, so this does not affect the velocity (next iteration starts by setting previous position to current position)
  460. Vec3 contact_normal = v.mCollisionPlane.GetNormal();
  461. v.mPosition += contact_normal * projected_distance;
  462. // Apply friction as described in Detailed Rigid Body Simulation with Extended Position Based Dynamics - Matthias Muller et al.
  463. // See section 3.6:
  464. // Inverse mass: w1 = 1 / m1, w2 = 1 / m2 + (r2 x n)^T I^-1 (r2 x n) = 0 for a static object
  465. // r2 are the contact point relative to the center of mass of body 2
  466. // Lagrange multiplier for contact: lambda = -c / (w1 + w2)
  467. // Where c is the constraint equation (the distance to the plane, negative because penetrating)
  468. // Contact normal force: fn = lambda / dt^2
  469. // Delta velocity due to friction dv = -vt / |vt| * min(dt * friction * fn * (w1 + w2), |vt|) = -vt * min(-friction * c / (|vt| * dt), 1)
  470. // 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
  471. // Relative velocity: vr = v1 - v2 - omega2 x r2
  472. // Normal velocity: vn = vr . contact_normal
  473. // Tangential velocity: vt = vr - contact_normal * vn
  474. // Impulse: p = dv / (w1 + w2)
  475. // Changes in particle velocities:
  476. // v1 = v1 + p / m1
  477. // v2 = v2 - p / m2 (no change when colliding with a static body)
  478. // w2 = w2 - I^-1 (r2 x p) (no change when colliding with a static body)
  479. if (cs.mMotionType == EMotionType::Dynamic)
  480. {
  481. // Calculate normal and tangential velocity (equation 30)
  482. Vec3 r2 = v.mPosition - cs.mCenterOfMassTransform.GetTranslation();
  483. Vec3 v2 = cs.GetPointVelocity(r2);
  484. Vec3 relative_velocity = v.mVelocity - v2;
  485. Vec3 v_normal = contact_normal * contact_normal.Dot(relative_velocity);
  486. Vec3 v_tangential = relative_velocity - v_normal;
  487. float v_tangential_length = v_tangential.Length();
  488. // Calculate resulting inverse mass of vertex
  489. float vertex_inv_mass = cs.mSoftBodyInvMassScale * v.mInvMass;
  490. // Calculate inverse effective mass
  491. Vec3 r2_cross_n = r2.Cross(contact_normal);
  492. float w2 = cs.mInvMass + r2_cross_n.Dot(cs.mInvInertia * r2_cross_n);
  493. float w1_plus_w2 = vertex_inv_mass + w2;
  494. // Calculate delta relative velocity due to friction (modified equation 31)
  495. Vec3 dv;
  496. if (v_tangential_length > 0.0f)
  497. dv = v_tangential * min(cs.mFriction * projected_distance / (v_tangential_length * dt), 1.0f);
  498. else
  499. dv = Vec3::sZero();
  500. // Calculate delta relative velocity due to restitution (equation 35)
  501. dv += v_normal;
  502. float prev_v_normal = (prev_v - v2).Dot(contact_normal);
  503. if (prev_v_normal < restitution_treshold)
  504. dv += cs.mRestitution * prev_v_normal * contact_normal;
  505. // Calculate impulse
  506. Vec3 p = dv / w1_plus_w2;
  507. // Apply impulse to particle
  508. v.mVelocity -= p * vertex_inv_mass;
  509. // Apply impulse to rigid body
  510. cs.mLinearVelocity += p * cs.mInvMass;
  511. cs.mAngularVelocity += cs.mInvInertia * r2.Cross(p);
  512. // Mark that the velocities of the body we hit need to be updated
  513. cs.mUpdateVelocities = true;
  514. }
  515. else
  516. {
  517. // Body is not movable, equations are simpler
  518. // Calculate normal and tangential velocity (equation 30)
  519. Vec3 v_normal = contact_normal * contact_normal.Dot(v.mVelocity);
  520. Vec3 v_tangential = v.mVelocity - v_normal;
  521. float v_tangential_length = v_tangential.Length();
  522. // Apply friction (modified equation 31)
  523. if (v_tangential_length > 0.0f)
  524. v.mVelocity -= v_tangential * min(cs.mFriction * projected_distance / (v_tangential_length * dt), 1.0f);
  525. // Apply restitution (equation 35)
  526. v.mVelocity -= v_normal;
  527. float prev_v_normal = prev_v.Dot(contact_normal);
  528. if (prev_v_normal < restitution_treshold)
  529. v.mVelocity -= cs.mRestitution * prev_v_normal * contact_normal;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. }
  536. void SoftBodyMotionProperties::UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  537. {
  538. JPH_PROFILE_FUNCTION();
  539. // Contact callback
  540. if (mHasContact && ioContext.mContactListener != nullptr)
  541. ioContext.mContactListener->OnSoftBodyContactAdded(*ioContext.mBody, SoftBodyManifold(this));
  542. // Loop through vertices once more to update the global state
  543. float dt = ioContext.mDeltaTime;
  544. float max_linear_velocity_sq = Square(GetMaxLinearVelocity());
  545. float max_v_sq = 0.0f;
  546. Vec3 linear_velocity = Vec3::sZero(), angular_velocity = Vec3::sZero();
  547. mLocalPredictedBounds = mLocalBounds = { };
  548. mHasContact = false;
  549. for (Vertex &v : mVertices)
  550. {
  551. // Calculate max square velocity
  552. float v_sq = v.mVelocity.LengthSq();
  553. max_v_sq = max(max_v_sq, v_sq);
  554. // Clamp if velocity is too high
  555. if (v_sq > max_linear_velocity_sq)
  556. v.mVelocity *= sqrt(max_linear_velocity_sq / v_sq);
  557. // Calculate local linear/angular velocity
  558. linear_velocity += v.mVelocity;
  559. angular_velocity += v.mPosition.Cross(v.mVelocity);
  560. // Update local bounding box
  561. mLocalBounds.Encapsulate(v.mPosition);
  562. // Create predicted position for the next frame in order to detect collisions before they happen
  563. mLocalPredictedBounds.Encapsulate(v.mPosition + v.mVelocity * dt + ioContext.mDisplacementDueToGravity);
  564. // Reset collision data for the next iteration
  565. v.mCollidingShapeIndex = -1;
  566. v.mHasContact = false;
  567. v.mLargestPenetration = -FLT_MAX;
  568. }
  569. // Calculate linear/angular velocity of the body by averaging all vertices and bringing the value to world space
  570. float num_vertices_divider = float(max(int(mVertices.size()), 1));
  571. SetLinearVelocity(ioContext.mCenterOfMassTransform.Multiply3x3(linear_velocity / num_vertices_divider));
  572. SetAngularVelocity(ioContext.mCenterOfMassTransform.Multiply3x3(angular_velocity / num_vertices_divider));
  573. if (mUpdatePosition)
  574. {
  575. // Shift the body so that the position is the center of the local bounds
  576. Vec3 delta = mLocalBounds.GetCenter();
  577. ioContext.mDeltaPosition = ioContext.mCenterOfMassTransform.Multiply3x3(delta);
  578. for (Vertex &v : mVertices)
  579. v.mPosition -= delta;
  580. // Offset bounds to match new position
  581. mLocalBounds.Translate(-delta);
  582. mLocalPredictedBounds.Translate(-delta);
  583. }
  584. else
  585. ioContext.mDeltaPosition = Vec3::sZero();
  586. // Test if we should go to sleep
  587. if (GetAllowSleeping())
  588. {
  589. if (max_v_sq > inPhysicsSettings.mPointVelocitySleepThreshold)
  590. {
  591. ResetSleepTestTimer();
  592. ioContext.mCanSleep = ECanSleep::CannotSleep;
  593. }
  594. else
  595. ioContext.mCanSleep = AccumulateSleepTime(dt, inPhysicsSettings.mTimeBeforeSleep);
  596. }
  597. else
  598. ioContext.mCanSleep = ECanSleep::CannotSleep;
  599. }
  600. void SoftBodyMotionProperties::UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface)
  601. {
  602. JPH_PROFILE_FUNCTION();
  603. // Write back velocity deltas
  604. for (const CollidingShape &cs : mCollidingShapes)
  605. if (cs.mUpdateVelocities)
  606. inBodyInterface.AddLinearAndAngularVelocity(cs.mBodyID, inContext.mCenterOfMassTransform.Multiply3x3(cs.mLinearVelocity - cs.mOriginalLinearVelocity), inContext.mCenterOfMassTransform.Multiply3x3(cs.mAngularVelocity - cs.mOriginalAngularVelocity));
  607. // Clear colliding shapes to avoid hanging on to references to shapes
  608. mCollidingShapes.clear();
  609. }
  610. void SoftBodyMotionProperties::InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext)
  611. {
  612. JPH_PROFILE_FUNCTION();
  613. // Store body
  614. ioContext.mBody = &inSoftBody;
  615. ioContext.mMotionProperties = this;
  616. ioContext.mContactListener = inSystem.GetSoftBodyContactListener();
  617. // Convert gravity to local space
  618. ioContext.mCenterOfMassTransform = inSoftBody.GetCenterOfMassTransform();
  619. ioContext.mGravity = ioContext.mCenterOfMassTransform.Multiply3x3Transposed(GetGravityFactor() * inSystem.GetGravity());
  620. // Calculate delta time for sub step
  621. ioContext.mDeltaTime = inDeltaTime;
  622. ioContext.mSubStepDeltaTime = inDeltaTime / mNumIterations;
  623. // Calculate total displacement we'll have due to gravity over all sub steps
  624. // The total displacement as produced by our integrator can be written as: Sum(i * g * dt^2, i = 0..mNumIterations).
  625. // This is bigger than 0.5 * g * dt^2 because we first increment the velocity and then update the position
  626. // Using Sum(i, i = 0..n) = n * (n + 1) / 2 we can write this as:
  627. ioContext.mDisplacementDueToGravity = (0.5f * mNumIterations * (mNumIterations + 1) * Square(ioContext.mSubStepDeltaTime)) * ioContext.mGravity;
  628. }
  629. void SoftBodyMotionProperties::StartNextIteration(const SoftBodyUpdateContext &ioContext)
  630. {
  631. ApplyPressure(ioContext);
  632. IntegratePositions(ioContext);
  633. ApplyBendConstraints(ioContext);
  634. ApplyVolumeConstraints(ioContext);
  635. }
  636. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext)
  637. {
  638. // 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)
  639. uint num_vertices = (uint)mVertices.size();
  640. if (ioContext.mNextCollisionVertex.load(memory_order_relaxed) < num_vertices)
  641. {
  642. // Fetch next batch of vertices to process
  643. uint next_vertex = ioContext.mNextCollisionVertex.fetch_add(SoftBodyUpdateContext::cVertexCollisionBatch, memory_order_acquire);
  644. if (next_vertex < num_vertices)
  645. {
  646. // Process collision planes
  647. uint num_vertices_to_process = min(SoftBodyUpdateContext::cVertexCollisionBatch, num_vertices - next_vertex);
  648. DetermineCollisionPlanes(ioContext, next_vertex, num_vertices_to_process);
  649. uint vertices_processed = ioContext.mNumCollisionVerticesProcessed.fetch_add(SoftBodyUpdateContext::cVertexCollisionBatch, memory_order_release) + num_vertices_to_process;
  650. if (vertices_processed >= num_vertices)
  651. {
  652. // Start the first iteration
  653. JPH_IF_ENABLE_ASSERTS(uint iteration =) ioContext.mNextIteration.fetch_add(1, memory_order_relaxed);
  654. JPH_ASSERT(iteration == 0);
  655. StartNextIteration(ioContext);
  656. ioContext.mState.store(SoftBodyUpdateContext::EState::ApplyEdgeConstraints, memory_order_release);
  657. }
  658. return EStatus::DidWork;
  659. }
  660. }
  661. return EStatus::NoWork;
  662. }
  663. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelApplyEdgeConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  664. {
  665. // 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)
  666. uint num_groups = (uint)mSettings->mEdgeGroupEndIndices.size();
  667. JPH_ASSERT(num_groups > 0, "SoftBodySharedSettings::Optimize should have been called!");
  668. uint32 edge_group, edge_start_idx;
  669. SoftBodyUpdateContext::sGetEdgeGroupAndStartIdx(ioContext.mNextEdgeConstraint.load(memory_order_relaxed), edge_group, edge_start_idx);
  670. if (edge_group < num_groups)
  671. {
  672. uint edge_group_size = mSettings->GetEdgeGroupSize(edge_group);
  673. if (edge_start_idx < edge_group_size || edge_group_size == 0)
  674. {
  675. // Fetch the next batch of edges to process
  676. uint64 next_edge_batch = ioContext.mNextEdgeConstraint.fetch_add(SoftBodyUpdateContext::cEdgeConstraintBatch, memory_order_acquire);
  677. SoftBodyUpdateContext::sGetEdgeGroupAndStartIdx(next_edge_batch, edge_group, edge_start_idx);
  678. if (edge_group < num_groups)
  679. {
  680. bool non_parallel_group = edge_group == num_groups - 1; // Last group is the non-parallel group and goes as a whole
  681. edge_group_size = mSettings->GetEdgeGroupSize(edge_group);
  682. if (non_parallel_group? edge_start_idx == 0 : edge_start_idx < edge_group_size)
  683. {
  684. // Process edges
  685. uint num_edges_to_process = non_parallel_group? edge_group_size : min(SoftBodyUpdateContext::cEdgeConstraintBatch, edge_group_size - edge_start_idx);
  686. if (edge_group > 0)
  687. edge_start_idx += mSettings->mEdgeGroupEndIndices[edge_group - 1];
  688. ApplyEdgeConstraints(ioContext, edge_start_idx, edge_start_idx + num_edges_to_process);
  689. // Test if we're at the end of this group
  690. uint edge_constraints_processed = ioContext.mNumEdgeConstraintsProcessed.fetch_add(num_edges_to_process, memory_order_relaxed) + num_edges_to_process;
  691. if (edge_constraints_processed >= edge_group_size)
  692. {
  693. // Non parallel group is the last group (which is also the only group that can be empty)
  694. if (non_parallel_group || mSettings->GetEdgeGroupSize(edge_group + 1) == 0)
  695. {
  696. // Finish the iteration
  697. ApplyLRAConstraints();
  698. ApplyCollisionConstraintsAndUpdateVelocities(ioContext);
  699. ApplySkinConstraints(ioContext);
  700. uint iteration = ioContext.mNextIteration.fetch_add(1, memory_order_relaxed);
  701. if (iteration < mNumIterations)
  702. {
  703. // Start a new iteration
  704. StartNextIteration(ioContext);
  705. // Reset next edge to process
  706. ioContext.mNumEdgeConstraintsProcessed.store(0, memory_order_relaxed);
  707. ioContext.mNextEdgeConstraint.store(0, memory_order_release);
  708. }
  709. else
  710. {
  711. // On final iteration we update the state
  712. UpdateSoftBodyState(ioContext, inPhysicsSettings);
  713. ioContext.mState.store(SoftBodyUpdateContext::EState::Done, memory_order_release);
  714. return EStatus::Done;
  715. }
  716. }
  717. else
  718. {
  719. // Next group
  720. ioContext.mNumEdgeConstraintsProcessed.store(0, memory_order_relaxed);
  721. ioContext.mNextEdgeConstraint.store(SoftBodyUpdateContext::sGetEdgeGroupStart(edge_group + 1), memory_order_release);
  722. }
  723. }
  724. return EStatus::DidWork;
  725. }
  726. }
  727. }
  728. }
  729. return EStatus::NoWork;
  730. }
  731. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  732. {
  733. switch (ioContext.mState.load(memory_order_relaxed))
  734. {
  735. case SoftBodyUpdateContext::EState::DetermineCollisionPlanes:
  736. return ParallelDetermineCollisionPlanes(ioContext);
  737. case SoftBodyUpdateContext::EState::ApplyEdgeConstraints:
  738. return ParallelApplyEdgeConstraints(ioContext, inPhysicsSettings);
  739. case SoftBodyUpdateContext::EState::Done:
  740. return EStatus::Done;
  741. default:
  742. JPH_ASSERT(false);
  743. return EStatus::NoWork;
  744. }
  745. }
  746. void SoftBodyMotionProperties::SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, [[maybe_unused]] uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator)
  747. {
  748. // Calculate the skin matrices
  749. uint num_skin_matrices = uint(mSettings->mInvBindMatrices.size());
  750. uint skin_matrices_size = num_skin_matrices * sizeof(Mat44);
  751. Mat44 *skin_matrices = (Mat44 *)ioTempAllocator.Allocate(skin_matrices_size);
  752. const Mat44 *skin_matrices_end = skin_matrices + num_skin_matrices;
  753. const InvBind *inv_bind_matrix = mSettings->mInvBindMatrices.data();
  754. for (Mat44 *s = skin_matrices; s < skin_matrices_end; ++s, ++inv_bind_matrix)
  755. *s = inJointMatrices[inv_bind_matrix->mJointIndex] * inv_bind_matrix->mInvBind;
  756. // Skin the vertices
  757. mSkinStateTransform = inCenterOfMassTransform;
  758. JPH_IF_ENABLE_ASSERTS(uint num_vertices = uint(mSettings->mVertices.size());)
  759. JPH_ASSERT(mSkinState.size() == num_vertices);
  760. const SoftBodySharedSettings::Vertex *in_vertices = mSettings->mVertices.data();
  761. for (const Skinned &s : mSettings->mSkinnedConstraints)
  762. {
  763. // Get bind pose
  764. JPH_ASSERT(s.mVertex < num_vertices);
  765. Vec3 bind_pos = Vec3::sLoadFloat3Unsafe(in_vertices[s.mVertex].mPosition);
  766. // Skin vertex
  767. Vec3 pos = Vec3::sZero();
  768. for (const SkinWeight &w : s.mWeights)
  769. {
  770. // We assume that the first zero weight is the end of the list
  771. if (w.mWeight == 0.0f)
  772. break;
  773. JPH_ASSERT(w.mInvBindIndex < num_skin_matrices);
  774. pos += w.mWeight * (skin_matrices[w.mInvBindIndex] * bind_pos);
  775. }
  776. mSkinState[s.mVertex].mPosition = pos;
  777. }
  778. // Calculate the normals
  779. for (const Skinned &s : mSettings->mSkinnedConstraints)
  780. {
  781. Vec3 normal = Vec3::sZero();
  782. uint32 num_faces = s.mNormalInfo >> 24;
  783. if (num_faces > 0)
  784. {
  785. // Calculate normal
  786. const uint32 *f = &mSettings->mSkinnedConstraintNormals[s.mNormalInfo & 0xffffff];
  787. const uint32 *f_end = f + num_faces;
  788. while (f < f_end)
  789. {
  790. const Face &face = mSettings->mFaces[*f];
  791. Vec3 v0 = mSkinState[face.mVertex[0]].mPosition;
  792. Vec3 v1 = mSkinState[face.mVertex[1]].mPosition;
  793. Vec3 v2 = mSkinState[face.mVertex[2]].mPosition;
  794. normal += (v1 - v0).Cross(v2 - v0).NormalizedOr(Vec3::sZero());
  795. ++f;
  796. }
  797. normal = normal.NormalizedOr(Vec3::sZero());
  798. }
  799. mSkinState[s.mVertex].mNormal = normal;
  800. }
  801. ioTempAllocator.Free(skin_matrices, skin_matrices_size);
  802. if (inHardSkinAll)
  803. {
  804. // Hard skin all vertices and reset their velocities
  805. for (const Skinned &s : mSettings->mSkinnedConstraints)
  806. {
  807. Vertex &vertex = mVertices[s.mVertex];
  808. vertex.mPosition = mSkinState[s.mVertex].mPosition;
  809. vertex.mVelocity = Vec3::sZero();
  810. }
  811. }
  812. else if (!mEnableSkinConstraints)
  813. {
  814. // Hard skin only the kinematic vertices as we will not solve the skin constraints later
  815. for (const Skinned &s : mSettings->mSkinnedConstraints)
  816. if (s.mMaxDistance == 0.0f)
  817. {
  818. Vertex &vertex = mVertices[s.mVertex];
  819. vertex.mPosition = mSkinState[s.mVertex].mPosition;
  820. }
  821. }
  822. }
  823. void SoftBodyMotionProperties::CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem)
  824. {
  825. JPH_PROFILE_FUNCTION();
  826. // Create update context
  827. SoftBodyUpdateContext context;
  828. InitializeUpdateContext(inDeltaTime, ioSoftBody, inSystem, context);
  829. // Determine bodies we're colliding with
  830. DetermineCollidingShapes(context, inSystem, inSystem.GetBodyLockInterface());
  831. // Call the internal update until it finishes
  832. EStatus status;
  833. const PhysicsSettings &settings = inSystem.GetPhysicsSettings();
  834. while ((status = ParallelUpdate(context, settings)) == EStatus::DidWork)
  835. continue;
  836. JPH_ASSERT(status == EStatus::Done);
  837. // Update the state of the bodies we've collided with
  838. UpdateRigidBodyVelocities(context, inSystem.GetBodyInterface());
  839. // Update position of the soft body
  840. if (mUpdatePosition)
  841. inSystem.GetBodyInterface().SetPosition(ioSoftBody.GetID(), ioSoftBody.GetPosition() + context.mDeltaPosition, EActivation::DontActivate);
  842. }
  843. #ifdef JPH_DEBUG_RENDERER
  844. void SoftBodyMotionProperties::DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  845. {
  846. for (const Vertex &v : mVertices)
  847. inRenderer->DrawMarker(inCenterOfMassTransform * v.mPosition, v.mInvMass > 0.0f? Color::sGreen : Color::sRed, 0.05f);
  848. }
  849. void SoftBodyMotionProperties::DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  850. {
  851. for (const Vertex &v : mVertices)
  852. inRenderer->DrawArrow(inCenterOfMassTransform * v.mPosition, inCenterOfMassTransform * (v.mPosition + v.mVelocity), Color::sYellow, 0.01f);
  853. }
  854. void SoftBodyMotionProperties::DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  855. {
  856. for (const Edge &e : mSettings->mEdgeConstraints)
  857. inRenderer->DrawLine(inCenterOfMassTransform * mVertices[e.mVertex[0]].mPosition, inCenterOfMassTransform * mVertices[e.mVertex[1]].mPosition, Color::sWhite);
  858. }
  859. void SoftBodyMotionProperties::DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  860. {
  861. for (const DihedralBend &b : mSettings->mDihedralBendConstraints)
  862. {
  863. RVec3 x0 = inCenterOfMassTransform * mVertices[b.mVertex[0]].mPosition;
  864. RVec3 x1 = inCenterOfMassTransform * mVertices[b.mVertex[1]].mPosition;
  865. RVec3 x2 = inCenterOfMassTransform * mVertices[b.mVertex[2]].mPosition;
  866. RVec3 x3 = inCenterOfMassTransform * mVertices[b.mVertex[3]].mPosition;
  867. RVec3 c_edge = 0.5_r * (x0 + x1);
  868. RVec3 c0 = (x0 + x1 + x2) / 3.0_r;
  869. RVec3 c1 = (x0 + x1 + x3) / 3.0_r;
  870. inRenderer->DrawArrow(0.9_r * x0 + 0.1_r * x1, 0.1_r * x0 + 0.9_r * x1, Color::sDarkGreen, 0.01f);
  871. inRenderer->DrawLine(c_edge, 0.1_r * c_edge + 0.9_r * c0, Color::sGreen);
  872. inRenderer->DrawLine(c_edge, 0.1_r * c_edge + 0.9_r * c1, Color::sGreen);
  873. }
  874. }
  875. void SoftBodyMotionProperties::DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  876. {
  877. for (const Volume &v : mSettings->mVolumeConstraints)
  878. {
  879. RVec3 x1 = inCenterOfMassTransform * mVertices[v.mVertex[0]].mPosition;
  880. RVec3 x2 = inCenterOfMassTransform * mVertices[v.mVertex[1]].mPosition;
  881. RVec3 x3 = inCenterOfMassTransform * mVertices[v.mVertex[2]].mPosition;
  882. RVec3 x4 = inCenterOfMassTransform * mVertices[v.mVertex[3]].mPosition;
  883. inRenderer->DrawTriangle(x1, x3, x2, Color::sYellow, DebugRenderer::ECastShadow::On);
  884. inRenderer->DrawTriangle(x2, x3, x4, Color::sYellow, DebugRenderer::ECastShadow::On);
  885. inRenderer->DrawTriangle(x1, x4, x3, Color::sYellow, DebugRenderer::ECastShadow::On);
  886. inRenderer->DrawTriangle(x1, x2, x4, Color::sYellow, DebugRenderer::ECastShadow::On);
  887. }
  888. }
  889. void SoftBodyMotionProperties::DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  890. {
  891. for (const Skinned &s : mSettings->mSkinnedConstraints)
  892. {
  893. const SkinState &skin_state = mSkinState[s.mVertex];
  894. DebugRenderer::sInstance->DrawArrow(mSkinStateTransform * skin_state.mPosition, mSkinStateTransform * (skin_state.mPosition + 0.1f * skin_state.mNormal), Color::sOrange, 0.01f);
  895. DebugRenderer::sInstance->DrawLine(mSkinStateTransform * skin_state.mPosition, inCenterOfMassTransform * mVertices[s.mVertex].mPosition, Color::sBlue);
  896. }
  897. }
  898. void SoftBodyMotionProperties::DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  899. {
  900. for (const LRA &l : mSettings->mLRAConstraints)
  901. inRenderer->DrawLine(inCenterOfMassTransform * mVertices[l.mVertex[0]].mPosition, inCenterOfMassTransform * mVertices[l.mVertex[1]].mPosition, Color::sGrey);
  902. }
  903. void SoftBodyMotionProperties::DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  904. {
  905. inRenderer->DrawWireBox(inCenterOfMassTransform, mLocalPredictedBounds, Color::sRed);
  906. }
  907. #endif // JPH_DEBUG_RENDERER
  908. void SoftBodyMotionProperties::SaveState(StateRecorder &inStream) const
  909. {
  910. MotionProperties::SaveState(inStream);
  911. for (const Vertex &v : mVertices)
  912. {
  913. inStream.Write(v.mPreviousPosition);
  914. inStream.Write(v.mPosition);
  915. inStream.Write(v.mVelocity);
  916. }
  917. inStream.Write(mLocalBounds.mMin);
  918. inStream.Write(mLocalBounds.mMax);
  919. inStream.Write(mLocalPredictedBounds.mMin);
  920. inStream.Write(mLocalPredictedBounds.mMax);
  921. }
  922. void SoftBodyMotionProperties::RestoreState(StateRecorder &inStream)
  923. {
  924. MotionProperties::RestoreState(inStream);
  925. for (Vertex &v : mVertices)
  926. {
  927. inStream.Read(v.mPreviousPosition);
  928. inStream.Read(v.mPosition);
  929. inStream.Read(v.mVelocity);
  930. }
  931. inStream.Read(mLocalBounds.mMin);
  932. inStream.Read(mLocalBounds.mMax);
  933. inStream.Read(mLocalPredictedBounds.mMin);
  934. inStream.Read(mLocalPredictedBounds.mMax);
  935. }
  936. JPH_NAMESPACE_END