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:
46
48 void Initialize(const SoftBodyCreationSettings &inSettings);
49
51 const SoftBodySharedSettings * GetSettings() const { return mSettings; }
52
54 const Array<Vertex> & GetVertices() const { return mVertices; }
55 Array<Vertex> & GetVertices() { return mVertices; }
56
58 const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; }
59 Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; }
60
62 const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; }
63
65 const Array<Face> & GetFaces() const { return mSettings->mFaces; }
66
68 const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; }
69
71 uint32 GetNumIterations() const { return mNumIterations; }
72 void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; }
73
75 float GetPressure() const { return mPressure; }
76 void SetPressure(float inPressure) { mPressure = inPressure; }
77
79 bool GetUpdatePosition() const { return mUpdatePosition; }
80 void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; }
81
83 bool GetEnableSkinConstraints() const { return mEnableSkinConstraints; }
84 void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; }
85
87 float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; }
88 void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
89
91 const AABox & GetLocalBounds() const { return mLocalBounds; }
92
94 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
95
98
99#ifdef JPH_DEBUG_RENDERER
101 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
102 void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
103 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
104 void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
105 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
106 void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
107 void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
108 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
109#endif // JPH_DEBUG_RENDERER
110
112 void SaveState(StateRecorder &inStream) const;
113
115 void RestoreState(StateRecorder &inStream);
116
123 void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator);
124
136 void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem);
137
139 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
141
143 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
144
146 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface);
147
149 enum class EStatus
150 {
151 NoWork = 1 << 0,
152 DidWork = 1 << 1,
153 Done = 1 << 2,
154 };
155
157 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
158
160 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface);
161
162private:
163 // SoftBodyManifold needs to have access to CollidingShape
164 friend class SoftBodyManifold;
165
166 // Information about a leaf shape that we're colliding with
167 struct LeafShape
168 {
169 LeafShape() = default;
170 LeafShape(Mat44Arg inTransform, Vec3Arg inScale, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mShape(inShape) { }
171
172 Mat44 mTransform;
173 Vec3 mScale;
174 RefConst<Shape> mShape;
175 };
176
177 // Collect information about the colliding bodies
178 struct CollidingShape
179 {
181 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
182 {
183 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
184 }
185
186 Mat44 mCenterOfMassTransform;
187 Array<LeafShape> mShapes;
188 BodyID mBodyID;
189 EMotionType mMotionType;
190 float mInvMass;
191 float mFriction;
192 float mRestitution;
193 float mSoftBodyInvMassScale;
194 bool mUpdateVelocities;
195 Mat44 mInvInertia;
196 Vec3 mLinearVelocity;
197 Vec3 mAngularVelocity;
198 Vec3 mOriginalLinearVelocity;
199 Vec3 mOriginalAngularVelocity;
200 };
201
202 // Collect information about the colliding sensors
203 struct CollidingSensor
204 {
205 Mat44 mCenterOfMassTransform;
206 Array<LeafShape> mShapes;
207 BodyID mBodyID;
208 bool mHasContact;
209 };
210
211 // Information about the state of all skinned vertices
212 struct SkinState
213 {
214 Vec3 mPreviousPosition = Vec3::sZero();
215 Vec3 mPosition = Vec3::sNaN();
216 Vec3 mNormal = Vec3::sNaN();
217 };
218
220 void DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices);
221
223 void DetermineSensorCollisions(CollidingSensor &ioSensor);
224
226 void ApplyPressure(const SoftBodyUpdateContext &inContext);
227
229 void IntegratePositions(const SoftBodyUpdateContext &inContext);
230
232 void ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
233
235 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
236
238 void ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
239
241 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
242
244 void ApplyLRAConstraints(uint inStartIndex, uint inEndIndex);
245
247 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
248
250 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
251
253 void StartFirstIteration(SoftBodyUpdateContext &ioContext);
254
256 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
257
259 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
260
262 EStatus ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext);
263
265 EStatus ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
266
268 void ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex);
269
271 float GetVolumeTimesSix() const;
272
273#ifdef JPH_DEBUG_RENDERER
275 template <typename GetEndIndex, typename DrawConstraint>
276 inline void DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const;
277
278 RMat44 mSkinStateTransform = RMat44::sIdentity();
279#endif // JPH_DEBUG_RENDERER
280
282 Array<Vertex> mVertices;
283 Array<CollidingShape> mCollidingShapes;
284 Array<CollidingSensor> mCollidingSensors;
285 Array<SkinState> mSkinState;
286 AABox mLocalBounds;
287 AABox mLocalPredictedBounds;
288 uint32 mNumIterations;
289 float mPressure;
290 float mSkinnedMaxDistanceMultiplier = 1.0f;
291 bool mUpdatePosition;
292 bool mNeedContactCallback = false;
293 bool mEnableSkinConstraints = true;
294 bool mSkinStatePreviousPositionValid = false;
295};
296
@ 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:271
unsigned int uint
Definition Core.h:481
#define JPH_NAMESPACE_END
Definition Core.h:414
std::uint32_t uint32
Definition Core.h:484
#define JPH_NAMESPACE_BEGIN
Definition Core.h:408
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:35
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:35
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 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:83
Vertex & GetVertex(uint inIndex)
Definition SoftBodyMotionProperties.h:59
EStatus
Return code for ParallelUpdate.
Definition SoftBodyMotionProperties.h:150
Array< Vertex > & GetVertices()
Definition SoftBodyMotionProperties.h:55
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier)
Definition SoftBodyMotionProperties.h:88
const PhysicsMaterialList & GetMaterials() const
Get the materials of the soft body.
Definition SoftBodyMotionProperties.h:62
const Array< Vertex > & GetVertices() const
Get the vertices of the soft body.
Definition SoftBodyMotionProperties.h:54
void SetUpdatePosition(bool inUpdatePosition)
Definition SoftBodyMotionProperties.h:80
float GetSkinnedMaxDistanceMultiplier() const
Multiplier applied to Skinned::mMaxDistance to allow tightening or loosening of the skin constraints....
Definition SoftBodyMotionProperties.h:87
void SetPressure(float inPressure)
Definition SoftBodyMotionProperties.h:76
const Array< Face > & GetFaces() const
Get the faces of the soft body.
Definition SoftBodyMotionProperties.h:65
const AABox & GetLocalBounds() const
Get local bounding box.
Definition SoftBodyMotionProperties.h:91
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition SoftBodyMotionProperties.h:58
void SetNumIterations(uint32 inNumIterations)
Definition SoftBodyMotionProperties.h:72
bool GetUpdatePosition() const
Update the position of the body while simulating (set to false for something that is attached to the ...
Definition SoftBodyMotionProperties.h:79
float GetVolume() const
Get the volume of the soft body. Note can become negative if the shape is inside out!
Definition SoftBodyMotionProperties.h:94
uint32 GetNumIterations() const
Get the number of solver iterations.
Definition SoftBodyMotionProperties.h:71
float GetPressure() const
Get the pressure of the soft body.
Definition SoftBodyMotionProperties.h:75
void SetEnableSkinConstraints(bool inEnableSkinConstraints)
Definition SoftBodyMotionProperties.h:84
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition SoftBodyMotionProperties.h:51
const Face & GetFace(uint inIndex) const
Access to an individual face.
Definition SoftBodyMotionProperties.h:68
An inverse bind matrix take a skinned vertex from its bind pose into joint local space.
Definition SoftBodySharedSettings.h:220
Definition SoftBodySharedSettings.h:284
A joint and its skin weight.
Definition SoftBodySharedSettings.h:234
A constraint that skins a vertex to joints and limits the distance that the simulated vertex can trav...
Definition SoftBodySharedSettings.h:248
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:590
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:125
Definition PhysicsSettings.h:28
Definition SoftBodySharedSettings.h:186
An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length.
Definition SoftBodySharedSettings.h:152
A face defines the surface of the body.
Definition SoftBodySharedSettings.h:136
Volume constraint, keeps the volume of a tetrahedron constant.
Definition SoftBodySharedSettings.h:203