SoftBodyMotionProperties.cpp 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  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/Collision/CollideSoftBodyVertexIterator.h>
  10. #include <Jolt/Physics/Collision/SimShapeFilterWrapper.h>
  11. #include <Jolt/Physics/PhysicsSystem.h>
  12. #include <Jolt/Physics/Body/BodyManager.h>
  13. #include <Jolt/Core/ScopeExit.h>
  14. #ifdef JPH_DEBUG_RENDERER
  15. #include <Jolt/Renderer/DebugRenderer.h>
  16. #endif // JPH_DEBUG_RENDERER
  17. JPH_NAMESPACE_BEGIN
  18. using namespace JPH::literals;
  19. void SoftBodyMotionProperties::CalculateMassAndInertia()
  20. {
  21. MassProperties mp;
  22. for (const Vertex &v : mVertices)
  23. if (v.mInvMass > 0.0f)
  24. {
  25. Vec3 pos = v.mPosition;
  26. // Accumulate mass
  27. float mass = 1.0f / v.mInvMass;
  28. mp.mMass += mass;
  29. // Inertia tensor, diagonal
  30. // See equations https://en.wikipedia.org/wiki/Moment_of_inertia section 'Inertia Tensor'
  31. for (int i = 0; i < 3; ++i)
  32. mp.mInertia(i, i) += mass * (Square(pos[(i + 1) % 3]) + Square(pos[(i + 2) % 3]));
  33. // Inertia tensor off diagonal
  34. for (int i = 0; i < 3; ++i)
  35. for (int j = 0; j < 3; ++j)
  36. if (i != j)
  37. mp.mInertia(i, j) -= mass * pos[i] * pos[j];
  38. }
  39. else
  40. {
  41. // If one vertex is kinematic, the entire body will have infinite mass and inertia
  42. SetInverseMass(0.0f);
  43. SetInverseInertia(Vec3::sZero(), Quat::sIdentity());
  44. return;
  45. }
  46. SetMassProperties(EAllowedDOFs::All, mp);
  47. }
  48. void SoftBodyMotionProperties::Initialize(const SoftBodyCreationSettings &inSettings)
  49. {
  50. // Store settings
  51. mSettings = inSettings.mSettings;
  52. mNumIterations = inSettings.mNumIterations;
  53. mPressure = inSettings.mPressure;
  54. mUpdatePosition = inSettings.mUpdatePosition;
  55. mFacesDoubleSided = inSettings.mFacesDoubleSided;
  56. SetVertexRadius(inSettings.mVertexRadius);
  57. // Initialize vertices
  58. mVertices.resize(inSettings.mSettings->mVertices.size());
  59. Mat44 rotation = inSettings.mMakeRotationIdentity? Mat44::sRotation(inSettings.mRotation) : Mat44::sIdentity();
  60. for (Array<Vertex>::size_type v = 0, s = mVertices.size(); v < s; ++v)
  61. {
  62. const SoftBodySharedSettings::Vertex &in_vertex = inSettings.mSettings->mVertices[v];
  63. Vertex &out_vertex = mVertices[v];
  64. out_vertex.mPreviousPosition = out_vertex.mPosition = rotation * Vec3(in_vertex.mPosition);
  65. out_vertex.mVelocity = rotation.Multiply3x3(Vec3(in_vertex.mVelocity));
  66. out_vertex.ResetCollision();
  67. out_vertex.mInvMass = in_vertex.mInvMass;
  68. mLocalBounds.Encapsulate(out_vertex.mPosition);
  69. }
  70. // Initialize rods
  71. if (!inSettings.mSettings->mRodStretchShearConstraints.empty())
  72. {
  73. mRodStates.resize(inSettings.mSettings->mRodStretchShearConstraints.size());
  74. Quat rotation_q = rotation.GetQuaternion();
  75. for (Array<RodState>::size_type r = 0, s = mRodStates.size(); r < s; ++r)
  76. {
  77. const SoftBodySharedSettings::RodStretchShear &in_rod = inSettings.mSettings->mRodStretchShearConstraints[r];
  78. RodState &out_rod = mRodStates[r];
  79. out_rod.mRotation = rotation_q * in_rod.mBishop;
  80. out_rod.mAngularVelocity = Vec3::sZero();
  81. }
  82. }
  83. // Allocate space for skinned vertices
  84. if (!inSettings.mSettings->mSkinnedConstraints.empty())
  85. mSkinState.resize(mVertices.size());
  86. // We don't know delta time yet, so we can't predict the bounds and use the local bounds as the predicted bounds
  87. mLocalPredictedBounds = mLocalBounds;
  88. CalculateMassAndInertia();
  89. }
  90. float SoftBodyMotionProperties::GetVolumeTimesSix() const
  91. {
  92. float six_volume = 0.0f;
  93. for (const Face &f : mSettings->mFaces)
  94. {
  95. Vec3 x1 = mVertices[f.mVertex[0]].mPosition;
  96. Vec3 x2 = mVertices[f.mVertex[1]].mPosition;
  97. Vec3 x3 = mVertices[f.mVertex[2]].mPosition;
  98. 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
  99. }
  100. return six_volume;
  101. }
  102. void SoftBodyMotionProperties::DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface)
  103. {
  104. JPH_PROFILE_FUNCTION();
  105. // Reset flag prior to collision detection
  106. mNeedContactCallback.store(false, memory_order_relaxed);
  107. struct Collector : public CollideShapeBodyCollector
  108. {
  109. Collector(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface, const AABox &inLocalBounds, SimShapeFilterWrapper &inShapeFilter, Array<CollidingShape> &ioHits, Array<CollidingSensor> &ioSensors) :
  110. mContext(inContext),
  111. mInverseTransform(inContext.mCenterOfMassTransform.InversedRotationTranslation()),
  112. mLocalBounds(inLocalBounds),
  113. mBodyLockInterface(inBodyLockInterface),
  114. mCombineFriction(inSystem.GetCombineFriction()),
  115. mCombineRestitution(inSystem.GetCombineRestitution()),
  116. mShapeFilter(inShapeFilter),
  117. mHits(ioHits),
  118. mSensors(ioSensors)
  119. {
  120. }
  121. virtual void AddHit(const BodyID &inResult) override
  122. {
  123. BodyLockRead lock(mBodyLockInterface, inResult);
  124. if (lock.Succeeded())
  125. {
  126. const Body &soft_body = *mContext.mBody;
  127. const Body &body = lock.GetBody();
  128. if (body.IsRigidBody() // TODO: We should support soft body vs soft body
  129. && soft_body.GetCollisionGroup().CanCollide(body.GetCollisionGroup()))
  130. {
  131. SoftBodyContactSettings settings;
  132. settings.mIsSensor = body.IsSensor();
  133. if (mContext.mContactListener == nullptr)
  134. {
  135. // If we have no contact listener, we can ignore sensors
  136. if (settings.mIsSensor)
  137. return;
  138. }
  139. else
  140. {
  141. // Call the contact listener to see if we should accept this contact
  142. if (mContext.mContactListener->OnSoftBodyContactValidate(soft_body, body, settings) != SoftBodyValidateResult::AcceptContact)
  143. return;
  144. // Check if there will be any interaction
  145. if (!settings.mIsSensor
  146. && settings.mInvMassScale1 == 0.0f
  147. && (body.GetMotionType() != EMotionType::Dynamic || settings.mInvMassScale2 == 0.0f))
  148. return;
  149. }
  150. // Calculate transform of this body relative to the soft body
  151. Mat44 com = (mInverseTransform * body.GetCenterOfMassTransform()).ToMat44();
  152. // Collect leaf shapes
  153. mShapeFilter.SetBody2(&body);
  154. struct LeafShapeCollector : public TransformedShapeCollector
  155. {
  156. virtual void AddHit(const TransformedShape &inResult) override
  157. {
  158. mHits.emplace_back(Mat44::sRotationTranslation(inResult.mShapeRotation, Vec3(inResult.mShapePositionCOM)), inResult.GetShapeScale(), inResult.mShape);
  159. }
  160. Array<LeafShape> mHits;
  161. };
  162. LeafShapeCollector collector;
  163. body.GetShape()->CollectTransformedShapes(mLocalBounds, com.GetTranslation(), com.GetQuaternion(), Vec3::sOne(), SubShapeIDCreator(), collector, mShapeFilter.GetFilter());
  164. if (collector.mHits.empty())
  165. return;
  166. if (settings.mIsSensor)
  167. {
  168. CollidingSensor cs;
  169. cs.mCenterOfMassTransform = com;
  170. cs.mShapes = std::move(collector.mHits);
  171. cs.mBodyID = inResult;
  172. mSensors.push_back(cs);
  173. }
  174. else
  175. {
  176. CollidingShape cs;
  177. cs.mCenterOfMassTransform = com;
  178. cs.mShapes = std::move(collector.mHits);
  179. cs.mBodyID = inResult;
  180. cs.mMotionType = body.GetMotionType();
  181. cs.mUpdateVelocities = false;
  182. cs.mFriction = mCombineFriction(soft_body, SubShapeID(), body, SubShapeID());
  183. cs.mRestitution = mCombineRestitution(soft_body, SubShapeID(), body, SubShapeID());
  184. cs.mSoftBodyInvMassScale = settings.mInvMassScale1;
  185. if (cs.mMotionType == EMotionType::Dynamic)
  186. {
  187. const MotionProperties *mp = body.GetMotionProperties();
  188. cs.mInvMass = settings.mInvMassScale2 * mp->GetInverseMass();
  189. cs.mInvInertia = settings.mInvInertiaScale2 * mp->GetInverseInertiaForRotation(cs.mCenterOfMassTransform.GetRotation());
  190. cs.mOriginalLinearVelocity = cs.mLinearVelocity = mInverseTransform.Multiply3x3(mp->GetLinearVelocity());
  191. cs.mOriginalAngularVelocity = cs.mAngularVelocity = mInverseTransform.Multiply3x3(mp->GetAngularVelocity());
  192. }
  193. mHits.push_back(cs);
  194. }
  195. }
  196. }
  197. }
  198. private:
  199. const SoftBodyUpdateContext &mContext;
  200. RMat44 mInverseTransform;
  201. AABox mLocalBounds;
  202. const BodyLockInterface & mBodyLockInterface;
  203. ContactConstraintManager::CombineFunction mCombineFriction;
  204. ContactConstraintManager::CombineFunction mCombineRestitution;
  205. SimShapeFilterWrapper & mShapeFilter;
  206. Array<CollidingShape> & mHits;
  207. Array<CollidingSensor> & mSensors;
  208. };
  209. // Calculate local bounding box
  210. AABox local_bounds = mLocalBounds;
  211. local_bounds.Encapsulate(mLocalPredictedBounds);
  212. local_bounds.ExpandBy(Vec3::sReplicate(mVertexRadius));
  213. // Calculate world space bounding box
  214. AABox world_bounds = local_bounds.Transformed(inContext.mCenterOfMassTransform);
  215. // Create shape filter
  216. SimShapeFilterWrapper shape_filter(inContext.mSimShapeFilter, inContext.mBody);
  217. Collector collector(inContext, inSystem, inBodyLockInterface, local_bounds, shape_filter, mCollidingShapes, mCollidingSensors);
  218. ObjectLayer layer = inContext.mBody->GetObjectLayer();
  219. DefaultBroadPhaseLayerFilter broadphase_layer_filter = inSystem.GetDefaultBroadPhaseLayerFilter(layer);
  220. DefaultObjectLayerFilter object_layer_filter = inSystem.GetDefaultLayerFilter(layer);
  221. inSystem.GetBroadPhaseQuery().CollideAABox(world_bounds, collector, broadphase_layer_filter, object_layer_filter);
  222. mNumSensors = uint(mCollidingSensors.size()); // Workaround for TSAN false positive: store mCollidingSensors.size() in a separate variable.
  223. }
  224. void SoftBodyMotionProperties::DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices)
  225. {
  226. JPH_PROFILE_FUNCTION();
  227. // Generate collision planes
  228. for (const CollidingShape &cs : mCollidingShapes)
  229. for (const LeafShape &shape : cs.mShapes)
  230. shape.mShape->CollideSoftBodyVertices(shape.mTransform, shape.mScale, CollideSoftBodyVertexIterator(mVertices.data() + inVertexStart), inNumVertices, int(&cs - mCollidingShapes.data()));
  231. }
  232. void SoftBodyMotionProperties::DetermineSensorCollisions(CollidingSensor &ioSensor)
  233. {
  234. JPH_PROFILE_FUNCTION();
  235. Plane collision_plane;
  236. float largest_penetration = -FLT_MAX;
  237. int colliding_shape_idx = -1;
  238. // Collide sensor against all vertices
  239. CollideSoftBodyVertexIterator vertex_iterator(
  240. StridedPtr<const Vec3>(&mVertices[0].mPosition, sizeof(SoftBodyVertex)), // The position and mass come from the soft body vertex
  241. StridedPtr<const float>(&mVertices[0].mInvMass, sizeof(SoftBodyVertex)),
  242. StridedPtr<Plane>(&collision_plane, 0), // We want all vertices to result in a single collision so we pass stride 0
  243. StridedPtr<float>(&largest_penetration, 0),
  244. StridedPtr<int>(&colliding_shape_idx, 0));
  245. for (const LeafShape &shape : ioSensor.mShapes)
  246. shape.mShape->CollideSoftBodyVertices(shape.mTransform, shape.mScale, vertex_iterator, uint(mVertices.size()), 0);
  247. ioSensor.mHasContact = largest_penetration > 0.0f;
  248. // We need a contact callback if one of the sensors collided
  249. if (ioSensor.mHasContact)
  250. mNeedContactCallback.store(true, memory_order_relaxed);
  251. }
  252. void SoftBodyMotionProperties::ApplyPressure(const SoftBodyUpdateContext &inContext)
  253. {
  254. JPH_PROFILE_FUNCTION();
  255. float dt = inContext.mSubStepDeltaTime;
  256. float pressure_coefficient = mPressure;
  257. if (pressure_coefficient > 0.0f)
  258. {
  259. // Calculate total volume
  260. float six_volume = GetVolumeTimesSix();
  261. if (six_volume > 0.0f)
  262. {
  263. // Apply pressure
  264. // p = F / A = n R T / V (see https://en.wikipedia.org/wiki/Pressure)
  265. // Our pressure coefficient is n R T so the impulse is:
  266. // P = F dt = pressure_coefficient / V * A * dt
  267. float coefficient = pressure_coefficient * dt / six_volume; // Need to still multiply by 6 for the volume
  268. for (const Face &f : mSettings->mFaces)
  269. {
  270. Vec3 x1 = mVertices[f.mVertex[0]].mPosition;
  271. Vec3 x2 = mVertices[f.mVertex[1]].mPosition;
  272. Vec3 x3 = mVertices[f.mVertex[2]].mPosition;
  273. Vec3 impulse = coefficient * (x2 - x1).Cross(x3 - x1); // Area is half the cross product so need to still divide by 2
  274. for (uint32 i : f.mVertex)
  275. {
  276. Vertex &v = mVertices[i];
  277. v.mVelocity += v.mInvMass * impulse; // Want to divide by 3 because we spread over 3 vertices
  278. }
  279. }
  280. }
  281. }
  282. }
  283. void SoftBodyMotionProperties::IntegratePositions(const SoftBodyUpdateContext &inContext)
  284. {
  285. JPH_PROFILE_FUNCTION();
  286. float dt = inContext.mSubStepDeltaTime;
  287. float linear_damping = max(0.0f, 1.0f - GetLinearDamping() * dt); // See: MotionProperties::ApplyForceTorqueAndDragInternal
  288. // Integrate
  289. Vec3 sub_step_gravity = inContext.mGravity * dt;
  290. Vec3 sub_step_impulse = GetAccumulatedForce() * dt / max(float(mVertices.size()), 1.0f);
  291. for (Vertex &v : mVertices)
  292. {
  293. if (v.mInvMass > 0.0f)
  294. {
  295. // Gravity
  296. v.mVelocity += sub_step_gravity + sub_step_impulse * v.mInvMass;
  297. // Damping
  298. v.mVelocity *= linear_damping;
  299. }
  300. // Integrate
  301. Vec3 position = v.mPosition;
  302. v.mPreviousPosition = position;
  303. v.mPosition = position + v.mVelocity * dt;
  304. }
  305. // Integrate rod orientations
  306. float half_dt = 0.5f * dt;
  307. for (RodState &r : mRodStates)
  308. {
  309. // Damping
  310. r.mAngularVelocity *= linear_damping;
  311. // Integrate
  312. Quat rotation = r.mRotation;
  313. Quat delta_rotation = half_dt * Quat::sMultiplyImaginary(r.mAngularVelocity, rotation);
  314. r.mPreviousRotationInternal = rotation; // Overwrites mAngularVelocity
  315. r.mRotation = (rotation + delta_rotation).Normalized();
  316. }
  317. }
  318. void SoftBodyMotionProperties::ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  319. {
  320. JPH_PROFILE_FUNCTION();
  321. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  322. for (const DihedralBend *b = mSettings->mDihedralBendConstraints.data() + inStartIndex, *b_end = mSettings->mDihedralBendConstraints.data() + inEndIndex; b < b_end; ++b)
  323. {
  324. Vertex &v0 = mVertices[b->mVertex[0]];
  325. Vertex &v1 = mVertices[b->mVertex[1]];
  326. Vertex &v2 = mVertices[b->mVertex[2]];
  327. Vertex &v3 = mVertices[b->mVertex[3]];
  328. // Get positions
  329. Vec3 x0 = v0.mPosition;
  330. Vec3 x1 = v1.mPosition;
  331. Vec3 x2 = v2.mPosition;
  332. Vec3 x3 = v3.mPosition;
  333. /*
  334. x2
  335. e1/ \e3
  336. / \
  337. x0----x1
  338. \ e0 /
  339. e2\ /e4
  340. x3
  341. */
  342. // Calculate the shared edge of the triangles
  343. Vec3 e = x1 - x0;
  344. float e_len = e.Length();
  345. if (e_len < 1.0e-6f)
  346. continue;
  347. // Calculate the normals of the triangles
  348. Vec3 x1x2 = x2 - x1;
  349. Vec3 x1x3 = x3 - x1;
  350. Vec3 n1 = (x2 - x0).Cross(x1x2);
  351. Vec3 n2 = x1x3.Cross(x3 - x0);
  352. float n1_len_sq = n1.LengthSq();
  353. float n2_len_sq = n2.LengthSq();
  354. float n1_len_sq_n2_len_sq = n1_len_sq * n2_len_sq;
  355. if (n1_len_sq_n2_len_sq < 1.0e-24f)
  356. continue;
  357. // Calculate constraint equation
  358. // 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
  359. float sign = Sign(n2.Cross(n1).Dot(e));
  360. float d = n1.Dot(n2) / sqrt(n1_len_sq_n2_len_sq);
  361. float c = sign * ACosApproximate(d) - b->mInitialAngle;
  362. // Ensure the range is -PI to PI
  363. if (c > JPH_PI)
  364. c -= 2.0f * JPH_PI;
  365. else if (c < -JPH_PI)
  366. c += 2.0f * JPH_PI;
  367. // Calculate gradient of constraint equation
  368. // Taken from "Strain Based Dynamics" - Matthias Muller et al. (Appendix A)
  369. // with p1 = x2, p2 = x3, p3 = x0 and p4 = x1
  370. // which in turn is based on "Simulation of Clothing with Folds and Wrinkles" - R. Bridson et al. (Section 4)
  371. n1 /= n1_len_sq;
  372. n2 /= n2_len_sq;
  373. Vec3 d0c = (x1x2.Dot(e) * n1 + x1x3.Dot(e) * n2) / e_len;
  374. Vec3 d2c = e_len * n1;
  375. Vec3 d3c = e_len * n2;
  376. // The sum of the gradients must be zero (see "Strain Based Dynamics" section 4)
  377. Vec3 d1c = -d0c - d2c - d3c;
  378. // Get masses
  379. float w0 = v0.mInvMass;
  380. float w1 = v1.mInvMass;
  381. float w2 = v2.mInvMass;
  382. float w3 = v3.mInvMass;
  383. // Calculate -lambda
  384. float denom = w0 * d0c.LengthSq() + w1 * d1c.LengthSq() + w2 * d2c.LengthSq() + w3 * d3c.LengthSq() + b->mCompliance * inv_dt_sq;
  385. if (denom < 1.0e-12f)
  386. continue;
  387. float minus_lambda = c / denom;
  388. // Apply correction
  389. v0.mPosition = x0 - minus_lambda * w0 * d0c;
  390. v1.mPosition = x1 - minus_lambda * w1 * d1c;
  391. v2.mPosition = x2 - minus_lambda * w2 * d2c;
  392. v3.mPosition = x3 - minus_lambda * w3 * d3c;
  393. }
  394. }
  395. void SoftBodyMotionProperties::ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  396. {
  397. JPH_PROFILE_FUNCTION();
  398. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  399. // Satisfy volume constraints
  400. for (const Volume *v = mSettings->mVolumeConstraints.data() + inStartIndex, *v_end = mSettings->mVolumeConstraints.data() + inEndIndex; v < v_end; ++v)
  401. {
  402. Vertex &v1 = mVertices[v->mVertex[0]];
  403. Vertex &v2 = mVertices[v->mVertex[1]];
  404. Vertex &v3 = mVertices[v->mVertex[2]];
  405. Vertex &v4 = mVertices[v->mVertex[3]];
  406. Vec3 x1 = v1.mPosition;
  407. Vec3 x2 = v2.mPosition;
  408. Vec3 x3 = v3.mPosition;
  409. Vec3 x4 = v4.mPosition;
  410. // Calculate constraint equation
  411. Vec3 x1x2 = x2 - x1;
  412. Vec3 x1x3 = x3 - x1;
  413. Vec3 x1x4 = x4 - x1;
  414. float c = abs(x1x2.Cross(x1x3).Dot(x1x4)) - v->mSixRestVolume;
  415. // Calculate gradient of constraint equation
  416. Vec3 d1c = (x4 - x2).Cross(x3 - x2);
  417. Vec3 d2c = x1x3.Cross(x1x4);
  418. Vec3 d3c = x1x4.Cross(x1x2);
  419. Vec3 d4c = x1x2.Cross(x1x3);
  420. // Get masses
  421. float w1 = v1.mInvMass;
  422. float w2 = v2.mInvMass;
  423. float w3 = v3.mInvMass;
  424. float w4 = v4.mInvMass;
  425. // Calculate -lambda
  426. float denom = w1 * d1c.LengthSq() + w2 * d2c.LengthSq() + w3 * d3c.LengthSq() + w4 * d4c.LengthSq() + v->mCompliance * inv_dt_sq;
  427. if (denom < 1.0e-12f)
  428. continue;
  429. float minus_lambda = c / denom;
  430. // Apply correction
  431. v1.mPosition = x1 - minus_lambda * w1 * d1c;
  432. v2.mPosition = x2 - minus_lambda * w2 * d2c;
  433. v3.mPosition = x3 - minus_lambda * w3 * d3c;
  434. v4.mPosition = x4 - minus_lambda * w4 * d4c;
  435. }
  436. }
  437. void SoftBodyMotionProperties::ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  438. {
  439. // Early out if nothing to do
  440. if (mSettings->mSkinnedConstraints.empty() || !mEnableSkinConstraints)
  441. return;
  442. JPH_PROFILE_FUNCTION();
  443. // We're going to iterate multiple times over the skin constraints, update the skinned position accordingly.
  444. // If we don't do this, the simulation will see a big jump and the first iteration will cause a big velocity change in the system.
  445. float factor = mSkinStatePreviousPositionValid? inContext.mNextIteration.load(std::memory_order_relaxed) / float(mNumIterations) : 1.0f;
  446. float prev_factor = 1.0f - factor;
  447. // Apply the constraints
  448. Vertex *vertices = mVertices.data();
  449. const SkinState *skin_states = mSkinState.data();
  450. for (const Skinned *s = mSettings->mSkinnedConstraints.data() + inStartIndex, *s_end = mSettings->mSkinnedConstraints.data() + inEndIndex; s < s_end; ++s)
  451. {
  452. Vertex &vertex = vertices[s->mVertex];
  453. const SkinState &skin_state = skin_states[s->mVertex];
  454. float max_distance = s->mMaxDistance * mSkinnedMaxDistanceMultiplier;
  455. // Calculate the skinned position by interpolating from previous to current position
  456. Vec3 skin_pos = prev_factor * skin_state.mPreviousPosition + factor * skin_state.mPosition;
  457. if (max_distance > 0.0f)
  458. {
  459. // Move vertex if it violated the back stop
  460. if (s->mBackStopDistance < max_distance)
  461. {
  462. // Center of the back stop sphere
  463. Vec3 center = skin_pos - skin_state.mNormal * (s->mBackStopDistance + s->mBackStopRadius);
  464. // Check if we're inside the back stop sphere
  465. Vec3 delta = vertex.mPosition - center;
  466. float delta_len_sq = delta.LengthSq();
  467. if (delta_len_sq < Square(s->mBackStopRadius))
  468. {
  469. // Push the vertex to the surface of the back stop sphere
  470. float delta_len = sqrt(delta_len_sq);
  471. vertex.mPosition = delta_len > 0.0f?
  472. center + delta * (s->mBackStopRadius / delta_len)
  473. : center + skin_state.mNormal * s->mBackStopRadius;
  474. }
  475. }
  476. // Clamp vertex distance to max distance from skinned position
  477. if (max_distance < FLT_MAX)
  478. {
  479. Vec3 delta = vertex.mPosition - skin_pos;
  480. float delta_len_sq = delta.LengthSq();
  481. float max_distance_sq = Square(max_distance);
  482. if (delta_len_sq > max_distance_sq)
  483. vertex.mPosition = skin_pos + delta * sqrt(max_distance_sq / delta_len_sq);
  484. }
  485. }
  486. else
  487. {
  488. // Kinematic: Just update the vertex position
  489. vertex.mPosition = skin_pos;
  490. }
  491. }
  492. }
  493. void SoftBodyMotionProperties::ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  494. {
  495. JPH_PROFILE_FUNCTION();
  496. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  497. // Satisfy edge constraints
  498. for (const Edge *e = mSettings->mEdgeConstraints.data() + inStartIndex, *e_end = mSettings->mEdgeConstraints.data() + inEndIndex; e < e_end; ++e)
  499. {
  500. Vertex &v0 = mVertices[e->mVertex[0]];
  501. Vertex &v1 = mVertices[e->mVertex[1]];
  502. // Get positions
  503. Vec3 x0 = v0.mPosition;
  504. Vec3 x1 = v1.mPosition;
  505. // Calculate current length
  506. Vec3 delta = x1 - x0;
  507. float length = delta.Length();
  508. // Apply correction
  509. float denom = length * (v0.mInvMass + v1.mInvMass + e->mCompliance * inv_dt_sq);
  510. if (denom < 1.0e-12f)
  511. continue;
  512. Vec3 correction = delta * (length - e->mRestLength) / denom;
  513. v0.mPosition = x0 + v0.mInvMass * correction;
  514. v1.mPosition = x1 - v1.mInvMass * correction;
  515. }
  516. }
  517. void SoftBodyMotionProperties::ApplyRodStretchShearConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  518. {
  519. JPH_PROFILE_FUNCTION();
  520. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  521. RodState *rod_state = mRodStates.data() + inStartIndex;
  522. for (const RodStretchShear *r = mSettings->mRodStretchShearConstraints.data() + inStartIndex, *r_end = mSettings->mRodStretchShearConstraints.data() + inEndIndex; r < r_end; ++r, ++rod_state)
  523. {
  524. // Get positions
  525. Vertex &v0 = mVertices[r->mVertex[0]];
  526. Vertex &v1 = mVertices[r->mVertex[1]];
  527. // Apply stretch and shear constraint
  528. // Equation 37 from "Position and Orientation Based Cosserat Rods" - Kugelstadt and Schoemer - SIGGRAPH 2016
  529. float denom = v0.mInvMass + v1.mInvMass + 4.0f * r->mInvMass * Square(r->mLength) + r->mCompliance * inv_dt_sq;
  530. if (denom < 1.0e-12f)
  531. continue;
  532. Vec3 x0 = v0.mPosition;
  533. Vec3 x1 = v1.mPosition;
  534. Quat rotation = rod_state->mRotation;
  535. Vec3 d3 = rotation.RotateAxisZ();
  536. Vec3 delta = (x1 - x0 - d3 * r->mLength) / denom;
  537. v0.mPosition = x0 + v0.mInvMass * delta;
  538. v1.mPosition = x1 - v1.mInvMass * delta;
  539. // q * e3_bar = q * (0, 0, -1, 0) = [-qy, qx, -qw, qz]
  540. Quat q_e3_bar(rotation.GetXYZW().Swizzle<SWIZZLE_Y, SWIZZLE_X, SWIZZLE_W, SWIZZLE_Z>().FlipSign<-1, 1, -1, 1>());
  541. rotation += (2.0f * r->mInvMass * r->mLength) * Quat::sMultiplyImaginary(delta, q_e3_bar);
  542. // Renormalize
  543. rod_state->mRotation = rotation.Normalized();
  544. }
  545. }
  546. void SoftBodyMotionProperties::ApplyRodBendTwistConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex)
  547. {
  548. JPH_PROFILE_FUNCTION();
  549. float inv_dt_sq = 1.0f / Square(inContext.mSubStepDeltaTime);
  550. const Array<RodStretchShear> &rods = mSettings->mRodStretchShearConstraints;
  551. for (const RodBendTwist *r = mSettings->mRodBendTwistConstraints.data() + inStartIndex, *r_end = mSettings->mRodBendTwistConstraints.data() + inEndIndex; r < r_end; ++r)
  552. {
  553. uint32 rod1_index = r->mRod[0];
  554. uint32 rod2_index = r->mRod[1];
  555. const RodStretchShear &rod1 = rods[rod1_index];
  556. const RodStretchShear &rod2 = rods[rod2_index];
  557. RodState &rod1_state = mRodStates[rod1_index];
  558. RodState &rod2_state = mRodStates[rod2_index];
  559. // Apply bend and twist constraint
  560. // Equation 40 from "Position and Orientation Based Cosserat Rods" - Kugelstadt and Schoemer - SIGGRAPH 2016
  561. float denom = rod1.mInvMass + rod2.mInvMass + r->mCompliance * inv_dt_sq;
  562. if (denom < 1.0e-12f)
  563. continue;
  564. Quat rotation1 = rod1_state.mRotation;
  565. Quat rotation2 = rod2_state.mRotation;
  566. Quat omega = rotation1.Conjugated() * rotation2;
  567. Quat omega0 = r->mOmega0;
  568. Vec4 omega_min_omega0 = (omega - omega0).GetXYZW();
  569. Vec4 omega_plus_omega0 = (omega + omega0).GetXYZW();
  570. // Take the shortest of the two rotations
  571. Quat delta_omega(Vec4::sSelect(omega_min_omega0, omega_plus_omega0, Vec4::sLess(omega_plus_omega0.DotV(omega_plus_omega0), omega_min_omega0.DotV(omega_min_omega0))));
  572. delta_omega /= denom;
  573. delta_omega.SetW(0.0f); // Scalar part needs to be zero because the real part of the Darboux vector doesn't vanish, see text between eq. 39 and 40.
  574. Quat delta_rod2 = rod2.mInvMass * rotation1 * delta_omega;
  575. rotation1 += rod1.mInvMass * rotation2 * delta_omega;
  576. rotation2 -= delta_rod2;
  577. // Renormalize
  578. rod1_state.mRotation = rotation1.Normalized();
  579. rod2_state.mRotation = rotation2.Normalized();
  580. }
  581. }
  582. void SoftBodyMotionProperties::ApplyLRAConstraints(uint inStartIndex, uint inEndIndex)
  583. {
  584. JPH_PROFILE_FUNCTION();
  585. // Satisfy LRA constraints
  586. Vertex *vertices = mVertices.data();
  587. for (const LRA *lra = mSettings->mLRAConstraints.data() + inStartIndex, *lra_end = mSettings->mLRAConstraints.data() + inEndIndex; lra < lra_end; ++lra)
  588. {
  589. JPH_ASSERT(lra->mVertex[0] < mVertices.size());
  590. JPH_ASSERT(lra->mVertex[1] < mVertices.size());
  591. const Vertex &vertex0 = vertices[lra->mVertex[0]];
  592. Vertex &vertex1 = vertices[lra->mVertex[1]];
  593. Vec3 x0 = vertex0.mPosition;
  594. Vec3 delta = vertex1.mPosition - x0;
  595. float delta_len_sq = delta.LengthSq();
  596. if (delta_len_sq > Square(lra->mMaxDistance))
  597. vertex1.mPosition = x0 + delta * lra->mMaxDistance / sqrt(delta_len_sq);
  598. }
  599. }
  600. void SoftBodyMotionProperties::ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext)
  601. {
  602. JPH_PROFILE_FUNCTION();
  603. float dt = inContext.mSubStepDeltaTime;
  604. float restitution_threshold = -2.0f * inContext.mGravity.Length() * dt;
  605. float vertex_radius = mVertexRadius;
  606. for (Vertex &v : mVertices)
  607. if (v.mInvMass > 0.0f)
  608. {
  609. // Remember previous velocity for restitution calculations
  610. Vec3 prev_v = v.mVelocity;
  611. // XPBD velocity update
  612. v.mVelocity = (v.mPosition - v.mPreviousPosition) / dt;
  613. // Satisfy collision constraint
  614. if (v.mCollidingShapeIndex >= 0)
  615. {
  616. // Check if there is a collision
  617. float projected_distance = -v.mCollisionPlane.SignedDistance(v.mPosition) + vertex_radius;
  618. if (projected_distance > 0.0f)
  619. {
  620. // Remember that there was a collision
  621. v.mHasContact = true;
  622. // We need a contact callback if one of the vertices collided
  623. mNeedContactCallback.store(true, memory_order_relaxed);
  624. // Note that we already calculated the velocity, so this does not affect the velocity (next iteration starts by setting previous position to current position)
  625. CollidingShape &cs = mCollidingShapes[v.mCollidingShapeIndex];
  626. Vec3 contact_normal = v.mCollisionPlane.GetNormal();
  627. v.mPosition += contact_normal * projected_distance;
  628. // Apply friction as described in Detailed Rigid Body Simulation with Extended Position Based Dynamics - Matthias Muller et al.
  629. // See section 3.6:
  630. // Inverse mass: w1 = 1 / m1, w2 = 1 / m2 + (r2 x n)^T I^-1 (r2 x n) = 0 for a static object
  631. // r2 are the contact point relative to the center of mass of body 2
  632. // Lagrange multiplier for contact: lambda = -c / (w1 + w2)
  633. // Where c is the constraint equation (the distance to the plane, negative because penetrating)
  634. // Contact normal force: fn = lambda / dt^2
  635. // Delta velocity due to friction dv = -vt / |vt| * min(dt * friction * fn * (w1 + w2), |vt|) = -vt * min(-friction * c / (|vt| * dt), 1)
  636. // 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
  637. // Relative velocity: vr = v1 - v2 - omega2 x r2
  638. // Normal velocity: vn = vr . contact_normal
  639. // Tangential velocity: vt = vr - contact_normal * vn
  640. // Impulse: p = dv / (w1 + w2)
  641. // Changes in particle velocities:
  642. // v1 = v1 + p / m1
  643. // v2 = v2 - p / m2 (no change when colliding with a static body)
  644. // w2 = w2 - I^-1 (r2 x p) (no change when colliding with a static body)
  645. if (cs.mMotionType == EMotionType::Dynamic)
  646. {
  647. // Calculate normal and tangential velocity (equation 30)
  648. Vec3 r2 = v.mPosition - cs.mCenterOfMassTransform.GetTranslation();
  649. Vec3 v2 = cs.GetPointVelocity(r2);
  650. Vec3 relative_velocity = v.mVelocity - v2;
  651. Vec3 v_normal = contact_normal * contact_normal.Dot(relative_velocity);
  652. Vec3 v_tangential = relative_velocity - v_normal;
  653. float v_tangential_length = v_tangential.Length();
  654. // Calculate resulting inverse mass of vertex
  655. float vertex_inv_mass = cs.mSoftBodyInvMassScale * v.mInvMass;
  656. // Calculate inverse effective mass
  657. Vec3 r2_cross_n = r2.Cross(contact_normal);
  658. float w2 = cs.mInvMass + r2_cross_n.Dot(cs.mInvInertia * r2_cross_n);
  659. float w1_plus_w2 = vertex_inv_mass + w2;
  660. if (w1_plus_w2 > 0.0f)
  661. {
  662. // Calculate delta relative velocity due to friction (modified equation 31)
  663. Vec3 dv;
  664. if (v_tangential_length > 0.0f)
  665. dv = v_tangential * min(cs.mFriction * projected_distance / (v_tangential_length * dt), 1.0f);
  666. else
  667. dv = Vec3::sZero();
  668. // Calculate delta relative velocity due to restitution (equation 35)
  669. dv += v_normal;
  670. float prev_v_normal = (prev_v - v2).Dot(contact_normal);
  671. if (prev_v_normal < restitution_threshold)
  672. dv += cs.mRestitution * prev_v_normal * contact_normal;
  673. // Calculate impulse
  674. Vec3 p = dv / w1_plus_w2;
  675. // Apply impulse to particle
  676. v.mVelocity -= p * vertex_inv_mass;
  677. // Apply impulse to rigid body
  678. cs.mLinearVelocity += p * cs.mInvMass;
  679. cs.mAngularVelocity += cs.mInvInertia * r2.Cross(p);
  680. // Mark that the velocities of the body we hit need to be updated
  681. cs.mUpdateVelocities = true;
  682. }
  683. }
  684. else if (cs.mSoftBodyInvMassScale > 0.0f)
  685. {
  686. // Body is not movable, equations are simpler
  687. // Calculate normal and tangential velocity (equation 30)
  688. Vec3 v_normal = contact_normal * contact_normal.Dot(v.mVelocity);
  689. Vec3 v_tangential = v.mVelocity - v_normal;
  690. float v_tangential_length = v_tangential.Length();
  691. // Apply friction (modified equation 31)
  692. if (v_tangential_length > 0.0f)
  693. v.mVelocity -= v_tangential * min(cs.mFriction * projected_distance / (v_tangential_length * dt), 1.0f);
  694. // Apply restitution (equation 35)
  695. v.mVelocity -= v_normal;
  696. float prev_v_normal = prev_v.Dot(contact_normal);
  697. if (prev_v_normal < restitution_threshold)
  698. v.mVelocity -= cs.mRestitution * prev_v_normal * contact_normal;
  699. }
  700. }
  701. }
  702. }
  703. // Calculate the new angular velocity for all rods
  704. float two_div_dt = 2.0f / dt;
  705. for (RodState &r : mRodStates)
  706. r.mAngularVelocity = two_div_dt * (r.mRotation * r.mPreviousRotationInternal.Conjugated()).GetXYZ(); // Overwrites mPreviousRotationInternal
  707. }
  708. void SoftBodyMotionProperties::UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  709. {
  710. JPH_PROFILE_FUNCTION();
  711. // Contact callback
  712. if (mNeedContactCallback.load(memory_order_relaxed) && ioContext.mContactListener != nullptr)
  713. {
  714. // Remove non-colliding sensors from the list
  715. for (int i = int(mCollidingSensors.size()) - 1; i >= 0; --i)
  716. if (!mCollidingSensors[i].mHasContact)
  717. {
  718. mCollidingSensors[i] = std::move(mCollidingSensors.back());
  719. mCollidingSensors.pop_back();
  720. }
  721. ioContext.mContactListener->OnSoftBodyContactAdded(*ioContext.mBody, SoftBodyManifold(this));
  722. }
  723. // Loop through vertices once more to update the global state
  724. float dt = ioContext.mDeltaTime;
  725. float max_linear_velocity_sq = Square(GetMaxLinearVelocity());
  726. float max_v_sq = 0.0f;
  727. Vec3 linear_velocity = Vec3::sZero(), angular_velocity = Vec3::sZero();
  728. mLocalPredictedBounds = mLocalBounds = { };
  729. for (Vertex &v : mVertices)
  730. {
  731. // Calculate max square velocity
  732. float v_sq = v.mVelocity.LengthSq();
  733. max_v_sq = max(max_v_sq, v_sq);
  734. // Clamp if velocity is too high
  735. if (v_sq > max_linear_velocity_sq)
  736. v.mVelocity *= sqrt(max_linear_velocity_sq / v_sq);
  737. // Calculate local linear/angular velocity
  738. linear_velocity += v.mVelocity;
  739. angular_velocity += v.mPosition.Cross(v.mVelocity);
  740. // Update local bounding box
  741. mLocalBounds.Encapsulate(v.mPosition);
  742. // Create predicted position for the next frame in order to detect collisions before they happen
  743. mLocalPredictedBounds.Encapsulate(v.mPosition + v.mVelocity * dt + ioContext.mDisplacementDueToGravity);
  744. // Reset collision data for the next iteration
  745. v.ResetCollision();
  746. }
  747. // Calculate linear/angular velocity of the body by averaging all vertices and bringing the value to world space
  748. float num_vertices_divider = float(max(int(mVertices.size()), 1));
  749. SetLinearVelocityClamped(ioContext.mCenterOfMassTransform.Multiply3x3(linear_velocity / num_vertices_divider));
  750. SetAngularVelocity(ioContext.mCenterOfMassTransform.Multiply3x3(angular_velocity / num_vertices_divider));
  751. if (mUpdatePosition)
  752. {
  753. // Shift the body so that the position is the center of the local bounds
  754. Vec3 delta = mLocalBounds.GetCenter();
  755. ioContext.mDeltaPosition = ioContext.mCenterOfMassTransform.Multiply3x3(delta);
  756. for (Vertex &v : mVertices)
  757. v.mPosition -= delta;
  758. // Update the skin state too since we will use this position as the previous position in the next update
  759. for (SkinState &s : mSkinState)
  760. s.mPosition -= delta;
  761. JPH_IF_DEBUG_RENDERER(mSkinStateTransform.SetTranslation(mSkinStateTransform.GetTranslation() + ioContext.mDeltaPosition);)
  762. // Offset bounds to match new position
  763. mLocalBounds.Translate(-delta);
  764. mLocalPredictedBounds.Translate(-delta);
  765. }
  766. else
  767. ioContext.mDeltaPosition = Vec3::sZero();
  768. // Test if we should go to sleep
  769. if (GetAllowSleeping())
  770. {
  771. if (max_v_sq > inPhysicsSettings.mPointVelocitySleepThreshold)
  772. {
  773. ResetSleepTestTimer();
  774. ioContext.mCanSleep = ECanSleep::CannotSleep;
  775. }
  776. else
  777. ioContext.mCanSleep = AccumulateSleepTime(dt, inPhysicsSettings.mTimeBeforeSleep);
  778. }
  779. else
  780. ioContext.mCanSleep = ECanSleep::CannotSleep;
  781. // If SkinVertices is not called after this then don't use the previous position as the skin is static
  782. mSkinStatePreviousPositionValid = false;
  783. // Reset force accumulator
  784. ResetForce();
  785. }
  786. void SoftBodyMotionProperties::UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface)
  787. {
  788. JPH_PROFILE_FUNCTION();
  789. // Write back velocity deltas
  790. for (const CollidingShape &cs : mCollidingShapes)
  791. if (cs.mUpdateVelocities)
  792. inBodyInterface.AddLinearAndAngularVelocity(cs.mBodyID, inContext.mCenterOfMassTransform.Multiply3x3(cs.mLinearVelocity - cs.mOriginalLinearVelocity), inContext.mCenterOfMassTransform.Multiply3x3(cs.mAngularVelocity - cs.mOriginalAngularVelocity));
  793. // Clear colliding shapes/sensors to avoid hanging on to references to shapes
  794. mCollidingShapes.clear();
  795. mCollidingSensors.clear();
  796. }
  797. void SoftBodyMotionProperties::InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext)
  798. {
  799. JPH_PROFILE_FUNCTION();
  800. // Store body
  801. ioContext.mBody = &inSoftBody;
  802. ioContext.mMotionProperties = this;
  803. ioContext.mContactListener = inSystem.GetSoftBodyContactListener();
  804. ioContext.mSimShapeFilter = inSystem.GetSimShapeFilter();
  805. // Convert gravity to local space
  806. ioContext.mCenterOfMassTransform = inSoftBody.GetCenterOfMassTransform();
  807. ioContext.mGravity = ioContext.mCenterOfMassTransform.Multiply3x3Transposed(GetGravityFactor() * inSystem.GetGravity());
  808. // Calculate delta time for sub step
  809. ioContext.mDeltaTime = inDeltaTime;
  810. ioContext.mSubStepDeltaTime = inDeltaTime / mNumIterations;
  811. // Calculate total displacement we'll have due to gravity over all sub steps
  812. // The total displacement as produced by our integrator can be written as: Sum(i * g * dt^2, i = 0..mNumIterations).
  813. // This is bigger than 0.5 * g * dt^2 because we first increment the velocity and then update the position
  814. // Using Sum(i, i = 0..n) = n * (n + 1) / 2 we can write this as:
  815. ioContext.mDisplacementDueToGravity = (0.5f * mNumIterations * (mNumIterations + 1) * Square(ioContext.mSubStepDeltaTime)) * ioContext.mGravity;
  816. }
  817. void SoftBodyMotionProperties::StartNextIteration(const SoftBodyUpdateContext &ioContext)
  818. {
  819. ApplyPressure(ioContext);
  820. IntegratePositions(ioContext);
  821. }
  822. void SoftBodyMotionProperties::StartFirstIteration(SoftBodyUpdateContext &ioContext)
  823. {
  824. // Start the first iteration
  825. JPH_IF_ENABLE_ASSERTS(uint iteration =) ioContext.mNextIteration.fetch_add(1, memory_order_relaxed);
  826. JPH_ASSERT(iteration == 0);
  827. StartNextIteration(ioContext);
  828. ioContext.mState.store(SoftBodyUpdateContext::EState::ApplyConstraints, memory_order_release);
  829. }
  830. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext)
  831. {
  832. // 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)
  833. uint num_vertices = (uint)mVertices.size();
  834. if (ioContext.mNextCollisionVertex.load(memory_order_relaxed) < num_vertices)
  835. {
  836. // Fetch next batch of vertices to process
  837. uint next_vertex = ioContext.mNextCollisionVertex.fetch_add(SoftBodyUpdateContext::cVertexCollisionBatch, memory_order_acquire);
  838. if (next_vertex < num_vertices)
  839. {
  840. // Process collision planes
  841. uint num_vertices_to_process = min(SoftBodyUpdateContext::cVertexCollisionBatch, num_vertices - next_vertex);
  842. DetermineCollisionPlanes(next_vertex, num_vertices_to_process);
  843. uint vertices_processed = ioContext.mNumCollisionVerticesProcessed.fetch_add(SoftBodyUpdateContext::cVertexCollisionBatch, memory_order_acq_rel) + num_vertices_to_process;
  844. if (vertices_processed >= num_vertices)
  845. {
  846. // Determine next state
  847. if (mCollidingSensors.empty())
  848. StartFirstIteration(ioContext);
  849. else
  850. ioContext.mState.store(SoftBodyUpdateContext::EState::DetermineSensorCollisions, memory_order_release);
  851. }
  852. return EStatus::DidWork;
  853. }
  854. }
  855. return EStatus::NoWork;
  856. }
  857. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext)
  858. {
  859. // Do a relaxed read to see if there are more sensors to process
  860. if (ioContext.mNextSensorIndex.load(memory_order_relaxed) < mNumSensors)
  861. {
  862. // Fetch next sensor to process
  863. uint sensor_index = ioContext.mNextSensorIndex.fetch_add(1, memory_order_acquire);
  864. if (sensor_index < mNumSensors)
  865. {
  866. // Process this sensor
  867. DetermineSensorCollisions(mCollidingSensors[sensor_index]);
  868. // Determine next state
  869. uint sensors_processed = ioContext.mNumSensorsProcessed.fetch_add(1, memory_order_acq_rel) + 1;
  870. if (sensors_processed >= mNumSensors)
  871. StartFirstIteration(ioContext);
  872. return EStatus::DidWork;
  873. }
  874. }
  875. return EStatus::NoWork;
  876. }
  877. void SoftBodyMotionProperties::ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex)
  878. {
  879. // Determine start and end
  880. SoftBodySharedSettings::UpdateGroup start { 0, 0, 0, 0, 0, 0, 0 };
  881. const SoftBodySharedSettings::UpdateGroup &prev = inGroupIndex > 0? mSettings->mUpdateGroups[inGroupIndex - 1] : start;
  882. const SoftBodySharedSettings::UpdateGroup &current = mSettings->mUpdateGroups[inGroupIndex];
  883. // Process volume constraints
  884. ApplyVolumeConstraints(ioContext, prev.mVolumeEndIndex, current.mVolumeEndIndex);
  885. // Process bend constraints
  886. ApplyDihedralBendConstraints(ioContext, prev.mDihedralBendEndIndex, current.mDihedralBendEndIndex);
  887. // Process skinned constraints
  888. ApplySkinConstraints(ioContext, prev.mSkinnedEndIndex, current.mSkinnedEndIndex);
  889. // Process edges
  890. ApplyEdgeConstraints(ioContext, prev.mEdgeEndIndex, current.mEdgeEndIndex);
  891. // Process rods
  892. ApplyRodStretchShearConstraints(ioContext, prev.mRodStretchShearEndIndex, current.mRodStretchShearEndIndex);
  893. ApplyRodBendTwistConstraints(ioContext, prev.mRodBendTwistEndIndex, current.mRodBendTwistEndIndex);
  894. // Process LRA constraints
  895. ApplyLRAConstraints(prev.mLRAEndIndex, current.mLRAEndIndex);
  896. }
  897. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  898. {
  899. uint num_groups = (uint)mSettings->mUpdateGroups.size();
  900. JPH_ASSERT(num_groups > 0, "SoftBodySharedSettings::Optimize should have been called!");
  901. --num_groups; // Last group is the non-parallel group, we don't want to execute it in parallel
  902. // 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)
  903. uint next_group = ioContext.mNextConstraintGroup.load(memory_order_relaxed);
  904. if (next_group < num_groups || (num_groups == 0 && next_group == 0))
  905. {
  906. // Fetch the next group process
  907. next_group = ioContext.mNextConstraintGroup.fetch_add(1, memory_order_acquire);
  908. if (next_group < num_groups || (num_groups == 0 && next_group == 0))
  909. {
  910. uint num_groups_processed = 0;
  911. if (num_groups > 0)
  912. {
  913. // Process this group
  914. ProcessGroup(ioContext, next_group);
  915. // Increment total number of groups processed
  916. num_groups_processed = ioContext.mNumConstraintGroupsProcessed.fetch_add(1, memory_order_acq_rel) + 1;
  917. }
  918. if (num_groups_processed >= num_groups)
  919. {
  920. // Finish the iteration
  921. JPH_PROFILE("FinishIteration");
  922. // Process non-parallel group
  923. ProcessGroup(ioContext, num_groups);
  924. ApplyCollisionConstraintsAndUpdateVelocities(ioContext);
  925. uint iteration = ioContext.mNextIteration.fetch_add(1, memory_order_relaxed);
  926. if (iteration < mNumIterations)
  927. {
  928. // Start a new iteration
  929. StartNextIteration(ioContext);
  930. // Reset group logic
  931. ioContext.mNumConstraintGroupsProcessed.store(0, memory_order_release);
  932. ioContext.mNextConstraintGroup.store(0, memory_order_release);
  933. }
  934. else
  935. {
  936. // On final iteration we update the state
  937. UpdateSoftBodyState(ioContext, inPhysicsSettings);
  938. ioContext.mState.store(SoftBodyUpdateContext::EState::Done, memory_order_release);
  939. return EStatus::Done;
  940. }
  941. }
  942. return EStatus::DidWork;
  943. }
  944. }
  945. return EStatus::NoWork;
  946. }
  947. SoftBodyMotionProperties::EStatus SoftBodyMotionProperties::ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings)
  948. {
  949. switch (ioContext.mState.load(memory_order_acquire))
  950. {
  951. case SoftBodyUpdateContext::EState::DetermineCollisionPlanes:
  952. return ParallelDetermineCollisionPlanes(ioContext);
  953. case SoftBodyUpdateContext::EState::DetermineSensorCollisions:
  954. return ParallelDetermineSensorCollisions(ioContext);
  955. case SoftBodyUpdateContext::EState::ApplyConstraints:
  956. return ParallelApplyConstraints(ioContext, inPhysicsSettings);
  957. case SoftBodyUpdateContext::EState::Done:
  958. return EStatus::Done;
  959. default:
  960. JPH_ASSERT(false);
  961. return EStatus::NoWork;
  962. }
  963. }
  964. void SoftBodyMotionProperties::SkinVertices([[maybe_unused]] RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, [[maybe_unused]] uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator)
  965. {
  966. // Calculate the skin matrices
  967. uint num_skin_matrices = uint(mSettings->mInvBindMatrices.size());
  968. uint skin_matrices_size = num_skin_matrices * sizeof(Mat44);
  969. Mat44 *skin_matrices = (Mat44 *)ioTempAllocator.Allocate(skin_matrices_size);
  970. JPH_SCOPE_EXIT([&ioTempAllocator, skin_matrices, skin_matrices_size]{ ioTempAllocator.Free(skin_matrices, skin_matrices_size); });
  971. const Mat44 *skin_matrices_end = skin_matrices + num_skin_matrices;
  972. const InvBind *inv_bind_matrix = mSettings->mInvBindMatrices.data();
  973. for (Mat44 *s = skin_matrices; s < skin_matrices_end; ++s, ++inv_bind_matrix)
  974. {
  975. JPH_ASSERT(inv_bind_matrix->mJointIndex < inNumJoints);
  976. *s = inJointMatrices[inv_bind_matrix->mJointIndex] * inv_bind_matrix->mInvBind;
  977. }
  978. // Skin the vertices
  979. JPH_IF_DEBUG_RENDERER(mSkinStateTransform = inCenterOfMassTransform;)
  980. JPH_IF_ENABLE_ASSERTS(uint num_vertices = uint(mSettings->mVertices.size());)
  981. JPH_ASSERT(mSkinState.size() == num_vertices);
  982. const SoftBodySharedSettings::Vertex *in_vertices = mSettings->mVertices.data();
  983. for (const Skinned &s : mSettings->mSkinnedConstraints)
  984. {
  985. // Get bind pose
  986. JPH_ASSERT(s.mVertex < num_vertices);
  987. Vec3 bind_pos = Vec3::sLoadFloat3Unsafe(in_vertices[s.mVertex].mPosition);
  988. // Skin vertex
  989. Vec3 pos = Vec3::sZero();
  990. for (const SkinWeight &w : s.mWeights)
  991. {
  992. // We assume that the first zero weight is the end of the list
  993. if (w.mWeight == 0.0f)
  994. break;
  995. JPH_ASSERT(w.mInvBindIndex < num_skin_matrices);
  996. pos += w.mWeight * (skin_matrices[w.mInvBindIndex] * bind_pos);
  997. }
  998. SkinState &skin_state = mSkinState[s.mVertex];
  999. skin_state.mPreviousPosition = skin_state.mPosition;
  1000. skin_state.mPosition = pos;
  1001. }
  1002. // Calculate the normals
  1003. for (const Skinned &s : mSettings->mSkinnedConstraints)
  1004. {
  1005. Vec3 normal = Vec3::sZero();
  1006. uint32 num_faces = s.mNormalInfo >> 24;
  1007. if (num_faces > 0)
  1008. {
  1009. // Calculate normal
  1010. const uint32 *f = &mSettings->mSkinnedConstraintNormals[s.mNormalInfo & 0xffffff];
  1011. const uint32 *f_end = f + num_faces;
  1012. while (f < f_end)
  1013. {
  1014. const Face &face = mSettings->mFaces[*f];
  1015. Vec3 v0 = mSkinState[face.mVertex[0]].mPosition;
  1016. Vec3 v1 = mSkinState[face.mVertex[1]].mPosition;
  1017. Vec3 v2 = mSkinState[face.mVertex[2]].mPosition;
  1018. normal += (v1 - v0).Cross(v2 - v0).NormalizedOr(Vec3::sZero());
  1019. ++f;
  1020. }
  1021. normal = normal.NormalizedOr(Vec3::sZero());
  1022. }
  1023. mSkinState[s.mVertex].mNormal = normal;
  1024. }
  1025. if (inHardSkinAll)
  1026. {
  1027. // Hard skin all vertices and reset their velocities
  1028. for (const Skinned &s : mSettings->mSkinnedConstraints)
  1029. {
  1030. Vertex &vertex = mVertices[s.mVertex];
  1031. SkinState &skin_state = mSkinState[s.mVertex];
  1032. skin_state.mPreviousPosition = skin_state.mPosition;
  1033. vertex.mPosition = skin_state.mPosition;
  1034. vertex.mVelocity = Vec3::sZero();
  1035. }
  1036. }
  1037. else if (!mEnableSkinConstraints)
  1038. {
  1039. // Hard skin only the kinematic vertices as we will not solve the skin constraints later
  1040. for (const Skinned &s : mSettings->mSkinnedConstraints)
  1041. if (s.mMaxDistance == 0.0f)
  1042. {
  1043. Vertex &vertex = mVertices[s.mVertex];
  1044. vertex.mPosition = mSkinState[s.mVertex].mPosition;
  1045. }
  1046. }
  1047. // Indicate that the previous positions are valid for the coming update
  1048. mSkinStatePreviousPositionValid = true;
  1049. }
  1050. void SoftBodyMotionProperties::CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem)
  1051. {
  1052. JPH_PROFILE_FUNCTION();
  1053. // Create update context
  1054. SoftBodyUpdateContext context;
  1055. InitializeUpdateContext(inDeltaTime, ioSoftBody, inSystem, context);
  1056. // Determine bodies we're colliding with
  1057. DetermineCollidingShapes(context, inSystem, inSystem.GetBodyLockInterface());
  1058. // Call the internal update until it finishes
  1059. EStatus status;
  1060. const PhysicsSettings &settings = inSystem.GetPhysicsSettings();
  1061. while ((status = ParallelUpdate(context, settings)) == EStatus::DidWork)
  1062. continue;
  1063. JPH_ASSERT(status == EStatus::Done);
  1064. // Update the state of the bodies we've collided with
  1065. UpdateRigidBodyVelocities(context, inSystem.GetBodyInterface());
  1066. // Update position of the soft body
  1067. if (mUpdatePosition)
  1068. inSystem.GetBodyInterface().SetPosition(ioSoftBody.GetID(), ioSoftBody.GetPosition() + context.mDeltaPosition, EActivation::DontActivate);
  1069. }
  1070. #ifdef JPH_DEBUG_RENDERER
  1071. void SoftBodyMotionProperties::DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  1072. {
  1073. for (const Vertex &v : mVertices)
  1074. inRenderer->DrawMarker(inCenterOfMassTransform * v.mPosition, v.mInvMass > 0.0f? Color::sGreen : Color::sRed, 0.05f);
  1075. }
  1076. void SoftBodyMotionProperties::DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  1077. {
  1078. for (const Vertex &v : mVertices)
  1079. inRenderer->DrawArrow(inCenterOfMassTransform * v.mPosition, inCenterOfMassTransform * (v.mPosition + v.mVelocity), Color::sYellow, 0.01f);
  1080. }
  1081. template <typename GetEndIndex, typename DrawConstraint>
  1082. inline void SoftBodyMotionProperties::DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const
  1083. {
  1084. uint start = 0;
  1085. for (uint i = 0; i < (uint)mSettings->mUpdateGroups.size(); ++i)
  1086. {
  1087. uint end = inGetEndIndex(mSettings->mUpdateGroups[i]);
  1088. Color base_color;
  1089. if (inConstraintColor != ESoftBodyConstraintColor::ConstraintType)
  1090. base_color = Color::sGetDistinctColor((uint)mSettings->mUpdateGroups.size() - i - 1); // Ensure that color 0 is always the last group
  1091. else
  1092. base_color = inBaseColor;
  1093. for (uint idx = start; idx < end; ++idx)
  1094. {
  1095. Color color = inConstraintColor == ESoftBodyConstraintColor::ConstraintOrder? base_color * (float(idx - start) / (end - start)) : base_color;
  1096. inDrawConstraint(idx, color);
  1097. }
  1098. start = end;
  1099. }
  1100. }
  1101. void SoftBodyMotionProperties::DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1102. {
  1103. DrawConstraints(inConstraintColor,
  1104. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1105. return inGroup.mEdgeEndIndex;
  1106. },
  1107. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1108. const Edge &e = mSettings->mEdgeConstraints[inIndex];
  1109. inRenderer->DrawLine(inCenterOfMassTransform * mVertices[e.mVertex[0]].mPosition, inCenterOfMassTransform * mVertices[e.mVertex[1]].mPosition, inColor);
  1110. },
  1111. Color::sWhite);
  1112. }
  1113. void SoftBodyMotionProperties::DrawRods(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1114. {
  1115. DrawConstraints(inConstraintColor,
  1116. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1117. return inGroup.mRodStretchShearEndIndex;
  1118. },
  1119. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1120. const RodStretchShear &r = mSettings->mRodStretchShearConstraints[inIndex];
  1121. inRenderer->DrawLine(inCenterOfMassTransform * mVertices[r.mVertex[0]].mPosition, inCenterOfMassTransform * mVertices[r.mVertex[1]].mPosition, inColor);
  1122. },
  1123. Color::sWhite);
  1124. }
  1125. void SoftBodyMotionProperties::DrawRodStates(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1126. {
  1127. DrawConstraints(inConstraintColor,
  1128. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1129. return inGroup.mRodStretchShearEndIndex;
  1130. },
  1131. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1132. const RodState &state = mRodStates[inIndex];
  1133. const RodStretchShear &rod = mSettings->mRodStretchShearConstraints[inIndex];
  1134. RVec3 x0 = inCenterOfMassTransform * mVertices[rod.mVertex[0]].mPosition;
  1135. RVec3 x1 = inCenterOfMassTransform * mVertices[rod.mVertex[1]].mPosition;
  1136. RMat44 rod_center = inCenterOfMassTransform;
  1137. rod_center.SetTranslation(0.5_r * (x0 + x1));
  1138. inRenderer->DrawArrow(rod_center.GetTranslation(), rod_center.GetTranslation() + state.mAngularVelocity, inColor, 0.01f * rod.mLength);
  1139. RMat44 rod_frame = rod_center * RMat44::sRotation(state.mRotation);
  1140. inRenderer->DrawCoordinateSystem(rod_frame, 0.3f * rod.mLength);
  1141. },
  1142. Color::sOrange);
  1143. }
  1144. void SoftBodyMotionProperties::DrawRodBendTwistConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1145. {
  1146. DrawConstraints(inConstraintColor,
  1147. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1148. return inGroup.mRodBendTwistEndIndex;
  1149. },
  1150. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1151. uint r1 = mSettings->mRodBendTwistConstraints[inIndex].mRod[0];
  1152. uint r2 = mSettings->mRodBendTwistConstraints[inIndex].mRod[1];
  1153. const RodStretchShear &rod1 = mSettings->mRodStretchShearConstraints[r1];
  1154. const RodStretchShear &rod2 = mSettings->mRodStretchShearConstraints[r2];
  1155. RVec3 x0 = inCenterOfMassTransform * (0.4f * mVertices[rod1.mVertex[0]].mPosition + 0.6f * mVertices[rod1.mVertex[1]].mPosition);
  1156. RVec3 x1 = inCenterOfMassTransform * (0.6f * mVertices[rod2.mVertex[0]].mPosition + 0.4f * mVertices[rod2.mVertex[1]].mPosition);
  1157. inRenderer->DrawLine(x0, x1, inColor);
  1158. },
  1159. Color::sGreen);
  1160. }
  1161. void SoftBodyMotionProperties::DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1162. {
  1163. DrawConstraints(inConstraintColor,
  1164. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1165. return inGroup.mDihedralBendEndIndex;
  1166. },
  1167. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1168. const DihedralBend &b = mSettings->mDihedralBendConstraints[inIndex];
  1169. RVec3 x0 = inCenterOfMassTransform * mVertices[b.mVertex[0]].mPosition;
  1170. RVec3 x1 = inCenterOfMassTransform * mVertices[b.mVertex[1]].mPosition;
  1171. RVec3 x2 = inCenterOfMassTransform * mVertices[b.mVertex[2]].mPosition;
  1172. RVec3 x3 = inCenterOfMassTransform * mVertices[b.mVertex[3]].mPosition;
  1173. RVec3 c_edge = 0.5_r * (x0 + x1);
  1174. RVec3 c0 = (x0 + x1 + x2) / 3.0_r;
  1175. RVec3 c1 = (x0 + x1 + x3) / 3.0_r;
  1176. inRenderer->DrawArrow(0.9_r * x0 + 0.1_r * x1, 0.1_r * x0 + 0.9_r * x1, inColor, 0.01f);
  1177. inRenderer->DrawLine(c_edge, 0.1_r * c_edge + 0.9_r * c0, inColor);
  1178. inRenderer->DrawLine(c_edge, 0.1_r * c_edge + 0.9_r * c1, inColor);
  1179. },
  1180. Color::sGreen);
  1181. }
  1182. void SoftBodyMotionProperties::DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1183. {
  1184. DrawConstraints(inConstraintColor,
  1185. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1186. return inGroup.mVolumeEndIndex;
  1187. },
  1188. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1189. const Volume &v = mSettings->mVolumeConstraints[inIndex];
  1190. RVec3 x1 = inCenterOfMassTransform * mVertices[v.mVertex[0]].mPosition;
  1191. RVec3 x2 = inCenterOfMassTransform * mVertices[v.mVertex[1]].mPosition;
  1192. RVec3 x3 = inCenterOfMassTransform * mVertices[v.mVertex[2]].mPosition;
  1193. RVec3 x4 = inCenterOfMassTransform * mVertices[v.mVertex[3]].mPosition;
  1194. inRenderer->DrawTriangle(x1, x3, x2, inColor, DebugRenderer::ECastShadow::On);
  1195. inRenderer->DrawTriangle(x2, x3, x4, inColor, DebugRenderer::ECastShadow::On);
  1196. inRenderer->DrawTriangle(x1, x4, x3, inColor, DebugRenderer::ECastShadow::On);
  1197. inRenderer->DrawTriangle(x1, x2, x4, inColor, DebugRenderer::ECastShadow::On);
  1198. },
  1199. Color::sYellow);
  1200. }
  1201. void SoftBodyMotionProperties::DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1202. {
  1203. DrawConstraints(inConstraintColor,
  1204. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1205. return inGroup.mSkinnedEndIndex;
  1206. },
  1207. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1208. const Skinned &s = mSettings->mSkinnedConstraints[inIndex];
  1209. const SkinState &skin_state = mSkinState[s.mVertex];
  1210. inRenderer->DrawArrow(mSkinStateTransform * skin_state.mPosition, mSkinStateTransform * (skin_state.mPosition + 0.1f * skin_state.mNormal), inColor, 0.01f);
  1211. inRenderer->DrawLine(mSkinStateTransform * skin_state.mPosition, inCenterOfMassTransform * mVertices[s.mVertex].mPosition, Color::sBlue);
  1212. },
  1213. Color::sOrange);
  1214. }
  1215. void SoftBodyMotionProperties::DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const
  1216. {
  1217. DrawConstraints(inConstraintColor,
  1218. [](const SoftBodySharedSettings::UpdateGroup &inGroup) {
  1219. return inGroup.mLRAEndIndex;
  1220. },
  1221. [this, inRenderer, &inCenterOfMassTransform](uint inIndex, ColorArg inColor) {
  1222. const LRA &l = mSettings->mLRAConstraints[inIndex];
  1223. inRenderer->DrawLine(inCenterOfMassTransform * mVertices[l.mVertex[0]].mPosition, inCenterOfMassTransform * mVertices[l.mVertex[1]].mPosition, inColor);
  1224. },
  1225. Color::sGrey);
  1226. }
  1227. void SoftBodyMotionProperties::DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const
  1228. {
  1229. inRenderer->DrawWireBox(inCenterOfMassTransform, mLocalPredictedBounds, Color::sRed);
  1230. }
  1231. #endif // JPH_DEBUG_RENDERER
  1232. void SoftBodyMotionProperties::SaveState(StateRecorder &inStream) const
  1233. {
  1234. MotionProperties::SaveState(inStream);
  1235. for (const Vertex &v : mVertices)
  1236. {
  1237. inStream.Write(v.mPosition);
  1238. inStream.Write(v.mVelocity);
  1239. }
  1240. for (const RodState &r : mRodStates)
  1241. {
  1242. inStream.Write(r.mRotation);
  1243. inStream.Write(r.mAngularVelocity);
  1244. }
  1245. for (const SkinState &s : mSkinState)
  1246. {
  1247. inStream.Write(s.mPreviousPosition);
  1248. inStream.Write(s.mPosition);
  1249. inStream.Write(s.mNormal);
  1250. }
  1251. inStream.Write(mLocalBounds.mMin);
  1252. inStream.Write(mLocalBounds.mMax);
  1253. inStream.Write(mLocalPredictedBounds.mMin);
  1254. inStream.Write(mLocalPredictedBounds.mMax);
  1255. }
  1256. void SoftBodyMotionProperties::RestoreState(StateRecorder &inStream)
  1257. {
  1258. MotionProperties::RestoreState(inStream);
  1259. for (Vertex &v : mVertices)
  1260. {
  1261. inStream.Read(v.mPosition);
  1262. inStream.Read(v.mVelocity);
  1263. }
  1264. for (RodState &r : mRodStates)
  1265. {
  1266. inStream.Read(r.mRotation);
  1267. inStream.Read(r.mAngularVelocity);
  1268. }
  1269. for (SkinState &s : mSkinState)
  1270. {
  1271. inStream.Read(s.mPreviousPosition);
  1272. inStream.Read(s.mPosition);
  1273. inStream.Read(s.mNormal);
  1274. }
  1275. inStream.Read(mLocalBounds.mMin);
  1276. inStream.Read(mLocalBounds.mMax);
  1277. inStream.Read(mLocalPredictedBounds.mMin);
  1278. inStream.Read(mLocalPredictedBounds.mMax);
  1279. }
  1280. JPH_NAMESPACE_END