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
13
15
16class PhysicsSystem;
17class BodyInterface;
19struct PhysicsSettings;
20class Body;
21class Shape;
23class TempAllocator;
24#ifdef JPH_DEBUG_RENDERER
25class DebugRenderer;
27#endif // JPH_DEBUG_RENDERER
28
30//
31// Based on: XPBD, Extended Position Based Dynamics, Matthias Muller, Ten Minute Physics
32// See: https://matthias-research.github.io/pages/tenMinutePhysics/09-xpbd.pdf
34{
35public:
45
47 void Initialize(const SoftBodyCreationSettings &inSettings);
48
50 const SoftBodySharedSettings * GetSettings() const { return mSettings; }
51
53 const Array<Vertex> & GetVertices() const { return mVertices; }
54 Array<Vertex> & GetVertices() { return mVertices; }
55
57 const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; }
58 Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; }
59
61 const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; }
62
64 const Array<Face> & GetFaces() const { return mSettings->mFaces; }
65
67 const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; }
68
70 uint32 GetNumIterations() const { return mNumIterations; }
71 void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; }
72
74 float GetPressure() const { return mPressure; }
75 void SetPressure(float inPressure) { mPressure = inPressure; }
76
78 bool GetUpdatePosition() const { return mUpdatePosition; }
79 void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; }
80
82 bool GetEnableSkinConstraints() const { return mEnableSkinConstraints; }
83 void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; }
84
86 float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; }
87 void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
88
90 const AABox & GetLocalBounds() const { return mLocalBounds; }
91
93 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
94
97
98#ifdef JPH_DEBUG_RENDERER
100 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
101 void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
102 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
103 void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
104 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
105 void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
106 void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const;
107 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
108#endif // JPH_DEBUG_RENDERER
109
111 void SaveState(StateRecorder &inStream) const;
112
114 void RestoreState(StateRecorder &inStream);
115
122 void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator);
123
135 void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem);
136
138 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
140
142 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
143
145 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface);
146
148 enum class EStatus
149 {
150 NoWork = 1 << 0,
151 DidWork = 1 << 1,
152 Done = 1 << 2,
153 };
154
156 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
157
159 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface);
160
161private:
162 // SoftBodyManifold needs to have access to CollidingShape
163 friend class SoftBodyManifold;
164
165 // Collect information about the colliding bodies
166 struct CollidingShape
167 {
169 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
170 {
171 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
172 }
173
174 Mat44 mCenterOfMassTransform;
175 RefConst<Shape> mShape;
176 BodyID mBodyID;
177 EMotionType mMotionType;
178 float mInvMass;
179 float mFriction;
180 float mRestitution;
181 float mSoftBodyInvMassScale;
182 bool mUpdateVelocities;
183 Mat44 mInvInertia;
184 Vec3 mLinearVelocity;
185 Vec3 mAngularVelocity;
186 Vec3 mOriginalLinearVelocity;
187 Vec3 mOriginalAngularVelocity;
188 };
189
190 // Collect information about the colliding sensors
191 struct CollidingSensor
192 {
193 Mat44 mCenterOfMassTransform;
194 RefConst<Shape> mShape;
195 BodyID mBodyID;
196 bool mHasContact;
197 };
198
199 // Information about the state of all skinned vertices
200 struct SkinState
201 {
202 Vec3 mPreviousPosition = Vec3::sZero();
203 Vec3 mPosition = Vec3::sNaN();
204 Vec3 mNormal = Vec3::sNaN();
205 };
206
208 void DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices);
209
211 void DetermineSensorCollisions(CollidingSensor &ioSensor);
212
214 void ApplyPressure(const SoftBodyUpdateContext &inContext);
215
217 void IntegratePositions(const SoftBodyUpdateContext &inContext);
218
220 void ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
221
223 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
224
226 void ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
227
229 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
230
232 void ApplyLRAConstraints(uint inStartIndex, uint inEndIndex);
233
235 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
236
238 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
239
241 void StartFirstIteration(SoftBodyUpdateContext &ioContext);
242
244 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
245
247 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
248
250 EStatus ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext);
251
253 EStatus ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
254
256 void ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex);
257
259 float GetVolumeTimesSix() const;
260
261#ifdef JPH_DEBUG_RENDERER
263 template <typename GetEndIndex, typename DrawConstraint>
264 inline void DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const;
265
266 RMat44 mSkinStateTransform = RMat44::sIdentity();
267#endif // JPH_DEBUG_RENDERER
268
270 Array<Vertex> mVertices;
271 Array<CollidingShape> mCollidingShapes;
272 Array<CollidingSensor> mCollidingSensors;
273 Array<SkinState> mSkinState;
274 AABox mLocalBounds;
275 AABox mLocalPredictedBounds;
276 uint32 mNumIterations;
277 float mPressure;
278 float mSkinnedMaxDistanceMultiplier = 1.0f;
279 bool mUpdatePosition;
280 bool mNeedContactCallback = false;
281 bool mEnableSkinConstraints = true;
282 bool mSkinStatePreviousPositionValid = false;
283};
284
@ 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:236
unsigned int uint
Definition: Core.h:452
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
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:29
Definition: Reference.h:157
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:34
bool GetEnableSkinConstraints() const
Global setting to turn on/off skin constraints.
Definition: SoftBodyMotionProperties.h:82
Vertex & GetVertex(uint inIndex)
Definition: SoftBodyMotionProperties.h:58
EStatus
Return code for ParallelUpdate.
Definition: SoftBodyMotionProperties.h:149
Array< Vertex > & GetVertices()
Definition: SoftBodyMotionProperties.h:54
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier)
Definition: SoftBodyMotionProperties.h:87
const PhysicsMaterialList & GetMaterials() const
Get the materials of the soft body.
Definition: SoftBodyMotionProperties.h:61
const Array< Vertex > & GetVertices() const
Get the vertices of the soft body.
Definition: SoftBodyMotionProperties.h:53
void SetUpdatePosition(bool inUpdatePosition)
Definition: SoftBodyMotionProperties.h:79
float GetSkinnedMaxDistanceMultiplier() const
Multiplier applied to Skinned::mMaxDistance to allow tightening or loosening of the skin constraints....
Definition: SoftBodyMotionProperties.h:86
void SetPressure(float inPressure)
Definition: SoftBodyMotionProperties.h:75
const Array< Face > & GetFaces() const
Get the faces of the soft body.
Definition: SoftBodyMotionProperties.h:64
const AABox & GetLocalBounds() const
Get local bounding box.
Definition: SoftBodyMotionProperties.h:90
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition: SoftBodyMotionProperties.h:57
void SetNumIterations(uint32 inNumIterations)
Definition: SoftBodyMotionProperties.h:71
bool GetUpdatePosition() const
Update the position of the body while simulating (set to false for something that is attached to the ...
Definition: SoftBodyMotionProperties.h:78
float GetVolume() const
Get the volume of the soft body. Note can become negative if the shape is inside out!
Definition: SoftBodyMotionProperties.h:93
uint32 GetNumIterations() const
Get the number of solver iterations.
Definition: SoftBodyMotionProperties.h:70
float GetPressure() const
Get the pressure of the soft body.
Definition: SoftBodyMotionProperties.h:74
void SetEnableSkinConstraints(bool inEnableSkinConstraints)
Definition: SoftBodyMotionProperties.h:83
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition: SoftBodyMotionProperties.h:50
const Face & GetFace(uint inIndex) const
Access to an individual face.
Definition: SoftBodyMotionProperties.h:67
An inverse bind matrix take a skinned vertex from its bind pose into joint local space.
Definition: SoftBodySharedSettings.h:214
Definition: SoftBodySharedSettings.h:278
A joint and its skin weight.
Definition: SoftBodySharedSettings.h:228
A constraint that skins a vertex to joints and limits the distance that the simulated vertex can trav...
Definition: SoftBodySharedSettings.h:242
Definition: SoftBodySharedSettings.h:16
Temporary data used by the update of a soft body.
Definition: SoftBodyUpdateContext.h:18
Definition: SoftBodyVertex.h:16
Definition: StateRecorder.h:105
Definition: TempAllocator.h:16
Definition: Vec3.h:17
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:594
static JPH_INLINE Vec3 sZero()
Vector with all zeros.
Definition: Vec3.inl:107
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition: Vec3.inl:129
Definition: PhysicsSettings.h:28
Definition: SoftBodySharedSettings.h:180
An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length.
Definition: SoftBodySharedSettings.h:146
A face defines the surface of the body.
Definition: SoftBodySharedSettings.h:130
Volume constraint, keeps the volume of a tetrahedron constant.
Definition: SoftBodySharedSettings.h:197