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
15
17
18class PhysicsSystem;
19class BodyInterface;
21struct PhysicsSettings;
22class Body;
23class Shape;
25class TempAllocator;
26#ifdef JPH_DEBUG_RENDERER
27class DebugRenderer;
29#endif // JPH_DEBUG_RENDERER
30
32//
33// Based on: XPBD, Extended Position Based Dynamics, Matthias Muller, Ten Minute Physics
34// See: https://matthias-research.github.io/pages/tenMinutePhysics/09-xpbd.pdf
36{
37public:
49
51 void Initialize(const SoftBodyCreationSettings &inSettings);
52
54 const SoftBodySharedSettings * GetSettings() const { return mSettings; }
55
57 const Array<Vertex> & GetVertices() const { return mVertices; }
58 Array<Vertex> & GetVertices() { return mVertices; }
59
61 const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; }
62 Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; }
63
65 Quat GetRodRotation(uint inIndex) const { return mRodStates[inIndex].mRotation; }
66 Vec3 GetRodAngularVelocity(uint inIndex) const { return mRodStates[inIndex].mAngularVelocity; }
67
69 const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; }
70
72 const Array<Face> & GetFaces() const { return mSettings->mFaces; }
73
75 const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; }
76
78 uint32 GetNumIterations() const { return mNumIterations; }
79 void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; }
80
82 float GetPressure() const { return mPressure; }
83 void SetPressure(float inPressure) { mPressure = inPressure; }
84
86 bool GetUpdatePosition() const { return mUpdatePosition; }
87 void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; }
88
90 bool GetFacesDoubleSided() const { return mFacesDoubleSided; }
91 void SetFacesDoubleSided(bool inDoubleSided) { mFacesDoubleSided = inDoubleSided; }
92
94 bool GetEnableSkinConstraints() const { return mEnableSkinConstraints; }
95 void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; }
96
98 float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; }
99 void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
100
102 float GetVertexRadius() const { return mVertexRadius; }
103 void SetVertexRadius(float inVertexRadius) { JPH_ASSERT(inVertexRadius >= 0.0f); mVertexRadius = inVertexRadius; }
104
106 const AABox & GetLocalBounds() const { return mLocalBounds; }
107
109 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
110
113
114#ifdef JPH_DEBUG_RENDERER
116 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
117 void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
118 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
119 void DrawRods(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
120 void DrawRodStates(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
121 void DrawRodBendTwistConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
122 void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
123 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
124 void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
125 void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
126 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
127#endif // JPH_DEBUG_RENDERER
128
130 void SaveState(StateRecorder &inStream) const;
131
133 void RestoreState(StateRecorder &inStream);
134
141 void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator);
142
144 RMat44 GetSkinnedVertexTransform() const { return mSkinStateTransform; }
145
147 StridedPtr<const Vec3> GetSkinnedVertexPreviousPositions() const { return StridedPtr(&mSkinState.data()->mPreviousPosition, sizeof(SkinState)); }
148
150 StridedPtr<const Vec3> GetSkinnedVertexPositions() const { return StridedPtr(&mSkinState.data()->mPosition, sizeof(SkinState)); }
151
153 StridedPtr<const Vec3> GetSkinnedVertexNormals() const { return StridedPtr(&mSkinState.data()->mNormal, sizeof(SkinState)); }
154
166 void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem);
167
169 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
171
173 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
174
176 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface);
177
179 enum class EStatus
180 {
181 NoWork = 1 << 0,
182 DidWork = 1 << 1,
183 Done = 1 << 2,
184 };
185
187 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
188
190 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface);
191
193 inline void RequestContactCallback() { mNeedContactCallback.store(true, memory_order_relaxed); }
194
195private:
196 // SoftBodyManifold needs to have access to CollidingShape
197 friend class SoftBodyManifold;
198
199 // Information about a leaf shape that we're colliding with
200 struct LeafShape
201 {
202 LeafShape() = default;
203 LeafShape(Mat44Arg inTransform, Vec3Arg inScale, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mShape(inShape) { }
204
205 Mat44 mTransform;
206 Vec3 mScale;
207 RefConst<Shape> mShape;
208 };
209
210 // Collect information about the colliding bodies
211 struct CollidingShape
212 {
214 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
215 {
216 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
217 }
218
219 Mat44 mCenterOfMassTransform;
220 Array<LeafShape> mShapes;
221 BodyID mBodyID;
222 EMotionType mMotionType;
223 float mInvMass;
224 float mFriction;
225 float mRestitution;
226 float mSoftBodyInvMassScale;
227 bool mUpdateVelocities;
228 Mat44 mInvInertia;
229 Vec3 mLinearVelocity;
230 Vec3 mAngularVelocity;
231 Vec3 mOriginalLinearVelocity;
232 Vec3 mOriginalAngularVelocity;
233 };
234
235 // Collect information about the colliding sensors
236 struct CollidingSensor
237 {
238 Mat44 mCenterOfMassTransform;
239 Array<LeafShape> mShapes;
240 BodyID mBodyID;
241 bool mHasContact;
242 };
243
244 // Information about the current state of a rod.
245 struct RodState
246 {
247 Quat mRotation;
248 union
249 {
250 Vec3 mAngularVelocity;
251 Quat mPreviousRotationInternal;
252 };
253 };
254
255 // Information about the state of all skinned vertices
256 struct SkinState
257 {
258 Vec3 mPreviousPosition = Vec3::sZero();
259 Vec3 mPosition = Vec3::sNaN();
260 Vec3 mNormal = Vec3::sNaN();
261 };
262
264 void DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices);
265
267 void DetermineSensorCollisions(CollidingSensor &ioSensor);
268
270 void ApplyPressure(const SoftBodyUpdateContext &inContext);
271
273 void IntegratePositions(const SoftBodyUpdateContext &inContext);
274
276 void ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
277
279 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
280
282 void ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
283
285 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
286
288 void ApplyRodStretchShearConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
289 void ApplyRodBendTwistConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
290
292 void ApplyLRAConstraints(uint inStartIndex, uint inEndIndex);
293
295 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
296
298 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
299
301 void StartFirstIteration(SoftBodyUpdateContext &ioContext);
302
304 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
305
307 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
308
310 EStatus ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext);
311
313 EStatus ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
314
316 void ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex);
317
319 float GetVolumeTimesSix() const;
320
321#ifdef JPH_DEBUG_RENDERER
323 template <typename GetEndIndex, typename DrawConstraint>
324 inline void DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const;
325#endif // JPH_DEBUG_RENDERER
326
327 RefConst<SoftBodySharedSettings> mSettings;
328 Array<Vertex> mVertices;
329 Array<RodState> mRodStates;
330 Array<CollidingShape> mCollidingShapes;
331 Array<CollidingSensor> mCollidingSensors;
332 Array<SkinState> mSkinState;
333 RMat44 mSkinStateTransform = RMat44::sIdentity();
334 AABox mLocalBounds;
335 AABox mLocalPredictedBounds;
336 uint32 mNumIterations;
337 uint mNumSensors;
338 float mPressure;
339 float mSkinnedMaxDistanceMultiplier = 1.0f;
340 float mVertexRadius = 0.0f;
341 bool mUpdatePosition;
342 bool mFacesDoubleSided;
343 atomic<bool> mNeedContactCallback = false;
344 bool mEnableSkinConstraints = true;
345 bool mSkinStatePreviousPositionValid = false;
346};
347
@ CalculateMassAndInertia
Tells the system to calculate the mass and inertia based on density.
Definition BodyCreationSettings.h:24
ESoftBodyConstraintColor
Defines how to color soft body constraints.
Definition BodyManager.h:28
Color ColorArg
Type to use for passing arguments to a function.
Definition Color.h:12
#define JPH_EXPORT
Definition Core.h:303
unsigned int uint
Definition Core.h:564
#define JPH_NAMESPACE_END
Definition Core.h:487
std::uint32_t uint32
Definition Core.h:567
#define JPH_NAMESPACE_BEGIN
Definition Core.h:481
#define JPH_ASSERT(...)
Definition IssueReporting.h:28
const Vec3 Vec3Arg
Definition MathTypes.h:19
const Mat44 & Mat44Arg
Definition MathTypes.h:29
EMotionType
Motion type of a physics body.
Definition MotionType.h:11
Array< RefConst< PhysicsMaterial > > PhysicsMaterialList
Definition PhysicsMaterial.h:55
Mat44Arg RMat44Arg
Definition Real.h:32
Mat44 RMat44
Definition Real.h:31
@ Array
Used in attribute declaration, indicates that this is an array of objects.
Definition SerializableAttribute.h:23
Axis aligned box.
Definition AABox.h:16
Definition Array.h:22
Definition Body.h:39
Definition BodyInterface.h:36
Base class interface for locking a body. Usually you will use BodyLockRead / BodyLockWrite / BodyLock...
Definition BodyLockInterface.h:17
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:30
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
This class contains the runtime information of a soft body.
Definition SoftBodyMotionProperties.h:36
bool GetEnableSkinConstraints() const
Global setting to turn on/off skin constraints.
Definition SoftBodyMotionProperties.h:94
Vertex & GetVertex(uint inIndex)
Definition SoftBodyMotionProperties.h:62
EStatus
Return code for ParallelUpdate.
Definition SoftBodyMotionProperties.h:180
Array< Vertex > & GetVertices()
Definition SoftBodyMotionProperties.h:58
SoftBodySharedSettings::Skinned Skinned
Definition SoftBodyMotionProperties.h:47
SoftBodySharedSettings::SkinWeight SkinWeight
Definition SoftBodyMotionProperties.h:46
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier)
Definition SoftBodyMotionProperties.h:99
const PhysicsMaterialList & GetMaterials() const
Get the materials of the soft body.
Definition SoftBodyMotionProperties.h:69
SoftBodySharedSettings::Edge Edge
Definition SoftBodyMotionProperties.h:39
SoftBodySharedSettings::Face Face
Definition SoftBodyMotionProperties.h:42
const Array< Vertex > & GetVertices() const
Get the vertices of the soft body.
Definition SoftBodyMotionProperties.h:57
SoftBodySharedSettings::DihedralBend DihedralBend
Definition SoftBodyMotionProperties.h:43
StridedPtr< const Vec3 > GetSkinnedVertexPositions() const
Access to the current skinned position. One entry per simulated vertex.
Definition SoftBodyMotionProperties.h:150
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:90
void SetUpdatePosition(bool inUpdatePosition)
Definition SoftBodyMotionProperties.h:87
SoftBodySharedSettings::RodStretchShear RodStretchShear
Definition SoftBodyMotionProperties.h:40
SoftBodySharedSettings::LRA LRA
Definition SoftBodyMotionProperties.h:48
float GetSkinnedMaxDistanceMultiplier() const
Multiplier applied to Skinned::mMaxDistance to allow tightening or loosening of the skin constraints....
Definition SoftBodyMotionProperties.h:98
void SetPressure(float inPressure)
Definition SoftBodyMotionProperties.h:83
friend class SoftBodyManifold
Definition SoftBodyMotionProperties.h:197
SoftBodyVertex Vertex
Definition SoftBodyMotionProperties.h:38
void SetFacesDoubleSided(bool inDoubleSided)
Definition SoftBodyMotionProperties.h:91
const Array< Face > & GetFaces() const
Get the faces of the soft body.
Definition SoftBodyMotionProperties.h:72
void Initialize(const SoftBodyCreationSettings &inSettings)
Initialize the soft body motion properties.
Definition SoftBodyMotionProperties.cpp:59
const AABox & GetLocalBounds() const
Get local bounding box.
Definition SoftBodyMotionProperties.h:106
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition SoftBodyMotionProperties.h:61
StridedPtr< const Vec3 > GetSkinnedVertexPreviousPositions() const
Access to the previous skinned position. During the simulation the skin constraints move from this po...
Definition SoftBodyMotionProperties.h:147
void SetNumIterations(uint32 inNumIterations)
Definition SoftBodyMotionProperties.h:79
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:102
SoftBodySharedSettings::RodBendTwist RodBendTwist
Definition SoftBodyMotionProperties.h:41
Quat GetRodRotation(uint inIndex) const
Access to the state of rods.
Definition SoftBodyMotionProperties.h:65
SoftBodySharedSettings::Volume Volume
Definition SoftBodyMotionProperties.h:44
Vec3 GetRodAngularVelocity(uint inIndex) const
Definition SoftBodyMotionProperties.h:66
void SetVertexRadius(float inVertexRadius)
Definition SoftBodyMotionProperties.h:103
bool GetUpdatePosition() const
Update the position of the body while simulating (set to false for something that is attached to the ...
Definition SoftBodyMotionProperties.h:86
SoftBodySharedSettings::InvBind InvBind
Definition SoftBodyMotionProperties.h:45
float GetVolume() const
Get the volume of the soft body. Note can become negative if the shape is inside out!
Definition SoftBodyMotionProperties.h:109
RMat44 GetSkinnedVertexTransform() const
After calling SkinVertices, this gives access to the vertex positions and normals....
Definition SoftBodyMotionProperties.h:144
uint32 GetNumIterations() const
Get the number of solver iterations.
Definition SoftBodyMotionProperties.h:78
float GetPressure() const
Get the pressure of the soft body.
Definition SoftBodyMotionProperties.h:82
void SetEnableSkinConstraints(bool inEnableSkinConstraints)
Definition SoftBodyMotionProperties.h:95
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition SoftBodyMotionProperties.h:54
void RequestContactCallback()
Set a flag to indicate that the ContactListener::OnSoftBodyContactAdded should be called.
Definition SoftBodyMotionProperties.h:193
const Face & GetFace(uint inIndex) const
Access to an individual face.
Definition SoftBodyMotionProperties.h:75
StridedPtr< const Vec3 > GetSkinnedVertexNormals() const
Access to the current skinned normal. One entry per simulated vertex.
Definition SoftBodyMotionProperties.h:153
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 StridedPtr.h:14
Definition TempAllocator.h:16
Definition Vec3.h:17
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
float4 Quat
Definition HLSLToCPP.h:485
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