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 GetEnableSkinConstraints() const { return mEnableSkinConstraints; }
90 void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; }
91
93 float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; }
94 void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
95
97 const AABox & GetLocalBounds() const { return mLocalBounds; }
98
100 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
101
104
105#ifdef JPH_DEBUG_RENDERER
107 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
108 void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
109 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
110 void DrawRods(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
111 void DrawRodStates(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
112 void DrawRodBendTwistConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
113 void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
114 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
115 void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
116 void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
117 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
118#endif // JPH_DEBUG_RENDERER
119
121 void SaveState(StateRecorder &inStream) const;
122
124 void RestoreState(StateRecorder &inStream);
125
132 void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator);
133
145 void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem);
146
148 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
150
152 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
153
155 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface);
156
158 enum class EStatus
159 {
160 NoWork = 1 << 0,
161 DidWork = 1 << 1,
162 Done = 1 << 2,
163 };
164
166 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
167
169 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface);
170
171private:
172 // SoftBodyManifold needs to have access to CollidingShape
173 friend class SoftBodyManifold;
174
175 // Information about a leaf shape that we're colliding with
176 struct LeafShape
177 {
178 LeafShape() = default;
179 LeafShape(Mat44Arg inTransform, Vec3Arg inScale, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mShape(inShape) { }
180
181 Mat44 mTransform;
182 Vec3 mScale;
183 RefConst<Shape> mShape;
184 };
185
186 // Collect information about the colliding bodies
187 struct CollidingShape
188 {
190 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
191 {
192 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
193 }
194
195 Mat44 mCenterOfMassTransform;
196 Array<LeafShape> mShapes;
197 BodyID mBodyID;
198 EMotionType mMotionType;
199 float mInvMass;
200 float mFriction;
201 float mRestitution;
202 float mSoftBodyInvMassScale;
203 bool mUpdateVelocities;
204 Mat44 mInvInertia;
205 Vec3 mLinearVelocity;
206 Vec3 mAngularVelocity;
207 Vec3 mOriginalLinearVelocity;
208 Vec3 mOriginalAngularVelocity;
209 };
210
211 // Collect information about the colliding sensors
212 struct CollidingSensor
213 {
214 Mat44 mCenterOfMassTransform;
215 Array<LeafShape> mShapes;
216 BodyID mBodyID;
217 bool mHasContact;
218 };
219
220 // Information about the current state of a rod.
221 struct RodState
222 {
223 Quat mRotation;
224 union
225 {
226 Vec3 mAngularVelocity;
227 Quat mPreviousRotationInternal;
228 };
229 };
230
231 // Information about the state of all skinned vertices
232 struct SkinState
233 {
234 Vec3 mPreviousPosition = Vec3::sZero();
235 Vec3 mPosition = Vec3::sNaN();
236 Vec3 mNormal = Vec3::sNaN();
237 };
238
240 void DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices);
241
243 void DetermineSensorCollisions(CollidingSensor &ioSensor);
244
246 void ApplyPressure(const SoftBodyUpdateContext &inContext);
247
249 void IntegratePositions(const SoftBodyUpdateContext &inContext);
250
252 void ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
253
255 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
256
258 void ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
259
261 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
262
264 void ApplyRodStretchShearConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
265 void ApplyRodBendTwistConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
266
268 void ApplyLRAConstraints(uint inStartIndex, uint inEndIndex);
269
271 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
272
274 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
275
277 void StartFirstIteration(SoftBodyUpdateContext &ioContext);
278
280 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
281
283 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
284
286 EStatus ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext);
287
289 EStatus ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
290
292 void ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex);
293
295 float GetVolumeTimesSix() const;
296
297#ifdef JPH_DEBUG_RENDERER
299 template <typename GetEndIndex, typename DrawConstraint>
300 inline void DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const;
301
302 RMat44 mSkinStateTransform = RMat44::sIdentity();
303#endif // JPH_DEBUG_RENDERER
304
306 Array<Vertex> mVertices;
307 Array<RodState> mRodStates;
308 Array<CollidingShape> mCollidingShapes;
309 Array<CollidingSensor> mCollidingSensors;
310 Array<SkinState> mSkinState;
311 AABox mLocalBounds;
312 AABox mLocalPredictedBounds;
313 uint32 mNumIterations;
314 uint mNumSensors;
315 float mPressure;
316 float mSkinnedMaxDistanceMultiplier = 1.0f;
317 bool mUpdatePosition;
318 atomic<bool> mNeedContactCallback = false;
319 bool mEnableSkinConstraints = true;
320 bool mSkinStatePreviousPositionValid = false;
321};
322
@ 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:275
unsigned int uint
Definition Core.h:487
#define JPH_NAMESPACE_END
Definition Core.h:419
std::uint32_t uint32
Definition Core.h:490
#define JPH_NAMESPACE_BEGIN
Definition Core.h:413
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:163
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition Shape.h:186
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:89
Vertex & GetVertex(uint inIndex)
Definition SoftBodyMotionProperties.h:61
EStatus
Return code for ParallelUpdate.
Definition SoftBodyMotionProperties.h:159
Array< Vertex > & GetVertices()
Definition SoftBodyMotionProperties.h:57
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier)
Definition SoftBodyMotionProperties.h:94
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
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:93
void SetPressure(float inPressure)
Definition SoftBodyMotionProperties.h:82
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:97
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition SoftBodyMotionProperties.h:60
void SetNumIterations(uint32 inNumIterations)
Definition SoftBodyMotionProperties.h:78
Quat GetRodRotation(uint inIndex) const
Access to the state of rods.
Definition SoftBodyMotionProperties.h:64
Vec3 GetRodAngularVelocity(uint inIndex) const
Definition SoftBodyMotionProperties.h:65
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:100
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:90
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition SoftBodyMotionProperties.h:53
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
Temporary data used by the update of a soft body.
Definition SoftBodyUpdateContext.h:19
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:595
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition Vec3.inl:103
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition Vec3.inl:130
Definition PhysicsSettings.h:28
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