Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SoftBodyMotionProperties.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2023 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
14
16
17class PhysicsSystem;
18class BodyInterface;
20struct PhysicsSettings;
21class Body;
22class Shape;
24class TempAllocator;
25#ifdef JPH_DEBUG_RENDERER
26class DebugRenderer;
28#endif // JPH_DEBUG_RENDERER
29
31//
32// Based on: XPBD, Extended Position Based Dynamics, Matthias Muller, Ten Minute Physics
33// See: https://matthias-research.github.io/pages/tenMinutePhysics/09-xpbd.pdf
35{
36public:
48
50 void Initialize(const SoftBodyCreationSettings &inSettings);
51
53 const SoftBodySharedSettings * GetSettings() const { return mSettings; }
54
56 const Array<Vertex> & GetVertices() const { return mVertices; }
57 Array<Vertex> & GetVertices() { return mVertices; }
58
60 const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; }
61 Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; }
62
64 Quat GetRodRotation(uint inIndex) const { return mRodStates[inIndex].mRotation; }
65 Vec3 GetRodAngularVelocity(uint inIndex) const { return mRodStates[inIndex].mAngularVelocity; }
66
68 const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; }
69
71 const Array<Face> & GetFaces() const { return mSettings->mFaces; }
72
74 const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; }
75
77 uint32 GetNumIterations() const { return mNumIterations; }
78 void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; }
79
81 float GetPressure() const { return mPressure; }
82 void SetPressure(float inPressure) { mPressure = inPressure; }
83
85 bool GetUpdatePosition() const { return mUpdatePosition; }
86 void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; }
87
89 bool GetFacesDoubleSided() const { return mFacesDoubleSided; }
90 void SetFacesDoubleSided(bool inDoubleSided) { mFacesDoubleSided = inDoubleSided; }
91
93 bool GetEnableSkinConstraints() const { return mEnableSkinConstraints; }
94 void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; }
95
97 float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; }
98 void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
99
101 float GetVertexRadius() const { return mVertexRadius; }
102 void SetVertexRadius(float inVertexRadius) { JPH_ASSERT(mVertexRadius >= 0.0f); mVertexRadius = inVertexRadius; }
103
105 const AABox & GetLocalBounds() const { return mLocalBounds; }
106
108 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
109
112
113#ifdef JPH_DEBUG_RENDERER
115 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
116 void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
117 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
118 void DrawRods(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
119 void DrawRodStates(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
120 void DrawRodBendTwistConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
121 void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
122 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
123 void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
124 void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
125 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
126#endif // JPH_DEBUG_RENDERER
127
129 void SaveState(StateRecorder &inStream) const;
130
132 void RestoreState(StateRecorder &inStream);
133
140 void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator);
141
153 void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem);
154
156 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
158
160 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
161
163 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface);
164
166 enum class EStatus
167 {
168 NoWork = 1 << 0,
169 DidWork = 1 << 1,
170 Done = 1 << 2,
171 };
172
174 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
175
177 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface);
178
180 inline void RequestContactCallback() { mNeedContactCallback.store(true, memory_order_relaxed); }
181
182private:
183 // SoftBodyManifold needs to have access to CollidingShape
184 friend class SoftBodyManifold;
185
186 // Information about a leaf shape that we're colliding with
187 struct LeafShape
188 {
189 LeafShape() = default;
190 LeafShape(Mat44Arg inTransform, Vec3Arg inScale, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mShape(inShape) { }
191
192 Mat44 mTransform;
193 Vec3 mScale;
194 RefConst<Shape> mShape;
195 };
196
197 // Collect information about the colliding bodies
198 struct CollidingShape
199 {
201 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
202 {
203 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
204 }
205
206 Mat44 mCenterOfMassTransform;
207 Array<LeafShape> mShapes;
208 BodyID mBodyID;
209 EMotionType mMotionType;
210 float mInvMass;
211 float mFriction;
212 float mRestitution;
213 float mSoftBodyInvMassScale;
214 bool mUpdateVelocities;
215 Mat44 mInvInertia;
216 Vec3 mLinearVelocity;
217 Vec3 mAngularVelocity;
218 Vec3 mOriginalLinearVelocity;
219 Vec3 mOriginalAngularVelocity;
220 };
221
222 // Collect information about the colliding sensors
223 struct CollidingSensor
224 {
225 Mat44 mCenterOfMassTransform;
226 Array<LeafShape> mShapes;
227 BodyID mBodyID;
228 bool mHasContact;
229 };
230
231 // Information about the current state of a rod.
232 struct RodState
233 {
234 Quat mRotation;
235 union
236 {
237 Vec3 mAngularVelocity;
238 Quat mPreviousRotationInternal;
239 };
240 };
241
242 // Information about the state of all skinned vertices
243 struct SkinState
244 {
245 Vec3 mPreviousPosition = Vec3::sZero();
246 Vec3 mPosition = Vec3::sNaN();
247 Vec3 mNormal = Vec3::sNaN();
248 };
249
251 void DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices);
252
254 void DetermineSensorCollisions(CollidingSensor &ioSensor);
255
257 void ApplyPressure(const SoftBodyUpdateContext &inContext);
258
260 void IntegratePositions(const SoftBodyUpdateContext &inContext);
261
263 void ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
264
266 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
267
269 void ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
270
272 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
273
275 void ApplyRodStretchShearConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
276 void ApplyRodBendTwistConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
277
279 void ApplyLRAConstraints(uint inStartIndex, uint inEndIndex);
280
282 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
283
285 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
286
288 void StartFirstIteration(SoftBodyUpdateContext &ioContext);
289
291 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
292
294 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
295
297 EStatus ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext);
298
300 EStatus ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
301
303 void ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex);
304
306 float GetVolumeTimesSix() const;
307
308#ifdef JPH_DEBUG_RENDERER
310 template <typename GetEndIndex, typename DrawConstraint>
311 inline void DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const;
312
313 RMat44 mSkinStateTransform = RMat44::sIdentity();
314#endif // JPH_DEBUG_RENDERER
315
317 Array<Vertex> mVertices;
318 Array<RodState> mRodStates;
319 Array<CollidingShape> mCollidingShapes;
320 Array<CollidingSensor> mCollidingSensors;
321 Array<SkinState> mSkinState;
322 AABox mLocalBounds;
323 AABox mLocalPredictedBounds;
324 uint32 mNumIterations;
325 uint mNumSensors;
326 float mPressure;
327 float mSkinnedMaxDistanceMultiplier = 1.0f;
328 float mVertexRadius = 0.0f;
329 bool mUpdatePosition;
330 bool mFacesDoubleSided;
331 atomic<bool> mNeedContactCallback = false;
332 bool mEnableSkinConstraints = true;
333 bool mSkinStatePreviousPositionValid = false;
334};
335
@ CalculateMassAndInertia
Tells the system to calculate the mass and inertia based on density.
ESoftBodyConstraintColor
Defines how to color soft body constraints.
Definition BodyManager.h:28
#define JPH_EXPORT
Definition Core.h:278
unsigned int uint
Definition Core.h:504
#define JPH_NAMESPACE_END
Definition Core.h:428
std::uint32_t uint32
Definition Core.h:507
#define JPH_NAMESPACE_BEGIN
Definition Core.h:422
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
EMotionType
Motion type of a physics body.
Definition MotionType.h:11
Axis aligned box.
Definition AABox.h:16
Definition Array.h:36
Definition Body.h:39
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition BodyID.h:13
Definition BodyInterface.h:36
Base class interface for locking a body. Usually you will use BodyLockRead / BodyLockWrite / BodyLock...
Definition BodyLockInterface.h:17
Class that holds an RGBA color with 8-bits per component.
Definition Color.h:16
Definition DebugRenderer.h:47
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
static JPH_INLINE Mat44 sIdentity()
Identity matrix.
Definition Mat44.inl:35
The Body class only keeps track of state for static bodies, the MotionProperties class keeps the addi...
Definition MotionProperties.h:29
void RestoreState(StateRecorder &inStream)
Restoring state for replay.
Definition MotionProperties.cpp:78
void SaveState(StateRecorder &inStream) const
Saving state for replay.
Definition MotionProperties.cpp:63
Definition PhysicsSystem.h:30
Definition Quat.h:33
Definition Reference.h:166
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition Shape.h:185
Definition SoftBodyCreationSettings.h:18
An interface to query which vertices of a soft body are colliding with other bodies.
Definition SoftBodyManifold.h:13
This class contains the runtime information of a soft body.
Definition SoftBodyMotionProperties.h:35
bool GetEnableSkinConstraints() const
Global setting to turn on/off skin constraints.
Definition SoftBodyMotionProperties.h:93
Vertex & GetVertex(uint inIndex)
Definition SoftBodyMotionProperties.h:61
EStatus
Return code for ParallelUpdate.
Definition SoftBodyMotionProperties.h:167
Array< Vertex > & GetVertices()
Definition SoftBodyMotionProperties.h:57
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier)
Definition SoftBodyMotionProperties.h:98
const PhysicsMaterialList & GetMaterials() const
Get the materials of the soft body.
Definition SoftBodyMotionProperties.h:68
const Array< Vertex > & GetVertices() const
Get the vertices of the soft body.
Definition SoftBodyMotionProperties.h:56
bool GetFacesDoubleSided() const
If the faces in this soft body should be treated as double sided for the purpose of collision detecti...
Definition SoftBodyMotionProperties.h:89
void SetUpdatePosition(bool inUpdatePosition)
Definition SoftBodyMotionProperties.h:86
float GetSkinnedMaxDistanceMultiplier() const
Multiplier applied to Skinned::mMaxDistance to allow tightening or loosening of the skin constraints....
Definition SoftBodyMotionProperties.h:97
void SetPressure(float inPressure)
Definition SoftBodyMotionProperties.h:82
void SetFacesDoubleSided(bool inDoubleSided)
Definition SoftBodyMotionProperties.h:90
const Array< Face > & GetFaces() const
Get the faces of the soft body.
Definition SoftBodyMotionProperties.h:71
const AABox & GetLocalBounds() const
Get local bounding box.
Definition SoftBodyMotionProperties.h:105
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition SoftBodyMotionProperties.h:60
void SetNumIterations(uint32 inNumIterations)
Definition SoftBodyMotionProperties.h:78
float GetVertexRadius() const
How big the particles are, can be used to push the vertices a little bit away from the surface of oth...
Definition SoftBodyMotionProperties.h:101
Quat GetRodRotation(uint inIndex) const
Access to the state of rods.
Definition SoftBodyMotionProperties.h:64
Vec3 GetRodAngularVelocity(uint inIndex) const
Definition SoftBodyMotionProperties.h:65
void SetVertexRadius(float inVertexRadius)
Definition SoftBodyMotionProperties.h:102
bool GetUpdatePosition() const
Update the position of the body while simulating (set to false for something that is attached to the ...
Definition SoftBodyMotionProperties.h:85
float GetVolume() const
Get the volume of the soft body. Note can become negative if the shape is inside out!
Definition SoftBodyMotionProperties.h:108
uint32 GetNumIterations() const
Get the number of solver iterations.
Definition SoftBodyMotionProperties.h:77
float GetPressure() const
Get the pressure of the soft body.
Definition SoftBodyMotionProperties.h:81
void SetEnableSkinConstraints(bool inEnableSkinConstraints)
Definition SoftBodyMotionProperties.h:94
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition SoftBodyMotionProperties.h:53
void RequestContactCallback()
Set a flag to indicate that the ContactListener::OnSoftBodyContactAdded should be called.
Definition SoftBodyMotionProperties.h:180
const Face & GetFace(uint inIndex) const
Access to an individual face.
Definition SoftBodyMotionProperties.h:74
An inverse bind matrix take a skinned vertex from its bind pose into joint local space.
Definition SoftBodySharedSettings.h:226
Definition SoftBodySharedSettings.h:290
A joint and its skin weight.
Definition SoftBodySharedSettings.h:240
A constraint that skins a vertex to joints and limits the distance that the simulated vertex can trav...
Definition SoftBodySharedSettings.h:254
Definition SoftBodySharedSettings.h:16
Definition SoftBodyVertex.h:16
Definition StateRecorder.h:110
Definition TempAllocator.h:16
Definition Vec3.h:17
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition Vec3.inl:841
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:125
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition Vec3.inl:162
Definition PhysicsSettings.h:31
Definition SoftBodySharedSettings.h:192
An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length.
Definition SoftBodySharedSettings.h:158
A face defines the surface of the body.
Definition SoftBodySharedSettings.h:142
A constraint that connects two Cosserat rods and limits bend and twist between the rods.
Definition SoftBodySharedSettings.h:332
Definition SoftBodySharedSettings.h:313
Volume constraint, keeps the volume of a tetrahedron constant.
Definition SoftBodySharedSettings.h:209