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;
26#endif // JPH_DEBUG_RENDERER
27
29//
30// Based on: XPBD, Extended Position Based Dynamics, Matthias Muller, Ten Minute Physics
31// See: https://matthias-research.github.io/pages/tenMinutePhysics/09-xpbd.pdf
33{
34public:
44
46 void Initialize(const SoftBodyCreationSettings &inSettings);
47
49 const SoftBodySharedSettings * GetSettings() const { return mSettings; }
50
52 const Array<Vertex> & GetVertices() const { return mVertices; }
53 Array<Vertex> & GetVertices() { return mVertices; }
54
56 const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; }
57 Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; }
58
60 const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; }
61
63 const Array<Face> & GetFaces() const { return mSettings->mFaces; }
64
66 const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; }
67
69 uint32 GetNumIterations() const { return mNumIterations; }
70 void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; }
71
73 float GetPressure() const { return mPressure; }
74 void SetPressure(float inPressure) { mPressure = inPressure; }
75
77 bool GetUpdatePosition() const { return mUpdatePosition; }
78 void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; }
79
81 bool GetEnableSkinConstraints() const { return mEnableSkinConstraints; }
82 void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; }
83
85 float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; }
86 void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; }
87
89 const AABox & GetLocalBounds() const { return mLocalBounds; }
90
92 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
93
96
97#ifdef JPH_DEBUG_RENDERER
99 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
100 void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
101 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
102 void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
103 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
104 void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
105 void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
106 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
107#endif // JPH_DEBUG_RENDERER
108
110 void SaveState(StateRecorder &inStream) const;
111
113 void RestoreState(StateRecorder &inStream);
114
121 void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator);
122
134 void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem);
135
137 // FUNCTIONS BELOW THIS LINE ARE FOR INTERNAL USE ONLY
139
141 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
142
144 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface);
145
147 enum class EStatus
148 {
149 NoWork = 1 << 0,
150 DidWork = 1 << 1,
151 Done = 1 << 2,
152 };
153
155 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
156
158 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface);
159
160private:
161 // SoftBodyManifold needs to have access to CollidingShape
162 friend class SoftBodyManifold;
163
164 // Collect information about the colliding bodies
165 struct CollidingShape
166 {
168 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
169 {
170 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
171 }
172
173 Mat44 mCenterOfMassTransform;
174 RefConst<Shape> mShape;
175 BodyID mBodyID;
176 EMotionType mMotionType;
177 bool mIsSensor;
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 // Information about the state of all skinned vertices
191 struct SkinState
192 {
193 Vec3 mPosition = Vec3::sNaN();
194 Vec3 mNormal = Vec3::sNaN();
195 };
196
198 void DetermineCollisionPlanes(const SoftBodyUpdateContext &inContext, uint inVertexStart, uint inNumVertices);
199
201 void ApplyPressure(const SoftBodyUpdateContext &inContext);
202
204 void IntegratePositions(const SoftBodyUpdateContext &inContext);
205
207 void ApplyBendConstraints(const SoftBodyUpdateContext &inContext);
208
210 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext);
211
213 void ApplySkinConstraints(const SoftBodyUpdateContext &inContext);
214
216 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
217
219 void ApplyLRAConstraints();
220
222 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
223
225 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
226
228 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
229
231 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
232
234 EStatus ParallelApplyEdgeConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
235
237 float GetVolumeTimesSix() const;
238
239 RMat44 mSkinStateTransform = RMat44::sIdentity();
241 Array<Vertex> mVertices;
242 Array<CollidingShape> mCollidingShapes;
243 Array<SkinState> mSkinState;
244 AABox mLocalBounds;
245 AABox mLocalPredictedBounds;
246 uint32 mNumIterations;
247 float mPressure;
248 float mSkinnedMaxDistanceMultiplier = 1.0f;
249 bool mUpdatePosition;
250 bool mHasContact = false;
251 bool mEnableSkinConstraints = true;
252};
253
@ CalculateMassAndInertia
Tells the system to calculate the mass and inertia based on density.
#define JPH_EXPORT
Definition: Core.h:227
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
EMotionType
Motion type of a physics body.
Definition: MotionType.h:11
Array< RefConst< PhysicsMaterial > > PhysicsMaterialList
Definition: PhysicsMaterial.h:50
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
Axis aligned box.
Definition: AABox.h:16
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
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:76
void SaveState(StateRecorder &inStream) const
Saving state for replay.
Definition: MotionProperties.cpp:61
Definition: PhysicsSystem.h:29
Definition: Reference.h:151
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:178
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:33
bool GetEnableSkinConstraints() const
Global setting to turn on/off skin constraints.
Definition: SoftBodyMotionProperties.h:81
Vertex & GetVertex(uint inIndex)
Definition: SoftBodyMotionProperties.h:57
EStatus
Return code for ParallelUpdate.
Definition: SoftBodyMotionProperties.h:148
Array< Vertex > & GetVertices()
Definition: SoftBodyMotionProperties.h:53
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier)
Definition: SoftBodyMotionProperties.h:86
const PhysicsMaterialList & GetMaterials() const
Get the materials of the soft body.
Definition: SoftBodyMotionProperties.h:60
const Array< Vertex > & GetVertices() const
Get the vertices of the soft body.
Definition: SoftBodyMotionProperties.h:52
void SetUpdatePosition(bool inUpdatePosition)
Definition: SoftBodyMotionProperties.h:78
float GetSkinnedMaxDistanceMultiplier() const
Multiplier applied to Skinned::mMaxDistance to allow tightening or loosening of the skin constraints....
Definition: SoftBodyMotionProperties.h:85
void SetPressure(float inPressure)
Definition: SoftBodyMotionProperties.h:74
const Array< Face > & GetFaces() const
Get the faces of the soft body.
Definition: SoftBodyMotionProperties.h:63
const AABox & GetLocalBounds() const
Get local bounding box.
Definition: SoftBodyMotionProperties.h:89
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition: SoftBodyMotionProperties.h:56
void SetNumIterations(uint32 inNumIterations)
Definition: SoftBodyMotionProperties.h:70
bool GetUpdatePosition() const
Update the position of the body while simulating (set to false for something that is attached to the ...
Definition: SoftBodyMotionProperties.h:77
float GetVolume() const
Get the volume of the soft body. Note can become negative if the shape is inside out!
Definition: SoftBodyMotionProperties.h:92
uint32 GetNumIterations() const
Get the number of solver iterations.
Definition: SoftBodyMotionProperties.h:69
float GetPressure() const
Get the pressure of the soft body.
Definition: SoftBodyMotionProperties.h:73
void SetEnableSkinConstraints(bool inEnableSkinConstraints)
Definition: SoftBodyMotionProperties.h:82
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition: SoftBodyMotionProperties.h:49
const Face & GetFace(uint inIndex) const
Access to an individual face.
Definition: SoftBodyMotionProperties.h:66
An inverse bind matrix take a skinned vertex from its bind pose into joint local space.
Definition: SoftBodySharedSettings.h:211
Definition: SoftBodySharedSettings.h:272
A joint and its skin weight.
Definition: SoftBodySharedSettings.h:225
A constraint that skins a vertex to joints and limits the distance that the simulated vertex can trav...
Definition: SoftBodySharedSettings.h:239
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:48
Definition: TempAllocator.h:16
Definition: Vec3.h:16
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:582
static JPH_INLINE Vec3 sNaN()
Vector with all NaN's.
Definition: Vec3.inl:129
Definition: PhysicsSettings.h:28
Definition: SoftBodySharedSettings.h:177
An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length.
Definition: SoftBodySharedSettings.h:143
A face defines the surface of the body.
Definition: SoftBodySharedSettings.h:127
Volume constraint, keeps the volume of a tetrahedron constant.
Definition: SoftBodySharedSettings.h:194