Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
SoftBodySharedSettings.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
10
12
15class JPH_EXPORT SoftBodySharedSettings : public RefTarget<SoftBodySharedSettings>
16{
18
19public:
21 enum class EBendType
22 {
25 Dihedral,
26 };
27
29 enum class ELRAType
30 {
32 EuclideanDistance,
33 GeodesicDistance,
34 };
35
40 {
42 VertexAttributes() = default;
43 VertexAttributes(float inCompliance, float inShearCompliance, float inBendCompliance, ELRAType inLRAType = ELRAType::None, float inLRAMaxDistanceMultiplier = 1.0f) : mCompliance(inCompliance), mShearCompliance(inShearCompliance), mBendCompliance(inBendCompliance), mLRAType(inLRAType), mLRAMaxDistanceMultiplier(inLRAMaxDistanceMultiplier) { }
44
45 float mCompliance = 0.0f;
46 float mShearCompliance = 0.0f;
47 float mBendCompliance = FLT_MAX;
50 };
51
57 void CreateConstraints(const VertexAttributes *inVertexAttributes, uint inVertexAttributesLength, EBendType inBendType = EBendType::Distance, float inAngleTolerance = DegreesToRadians(8.0f));
58
61
65
68 void CalculateLRALengths(float inMaxDistanceMultiplier = 1.0f);
69
72
75
78
91
93 void Optimize(OptimizationResults &outResults);
94
96 void Optimize() { OptimizationResults results; Optimize(results); }
97
99 Ref<SoftBodySharedSettings> Clone() const;
100
102 void SaveBinaryState(StreamOut &inStream) const;
103
105 void RestoreBinaryState(StreamIn &inStream);
106
111
113 void SaveWithMaterials(StreamOut &inStream, SharedSettingsToIDMap &ioSettingsMap, MaterialToIDMap &ioMaterialMap) const;
114
116
118 static SettingsResult sRestoreWithMaterials(StreamIn &inStream, IDToSharedSettingsMap &ioSettingsMap, IDToMaterialMap &ioMaterialMap);
119
124 static Ref<SoftBodySharedSettings> sCreateCube(uint inGridSize, float inGridSpacing);
125
128 {
130
131
132 Vertex() = default;
133 explicit Vertex(const Float3 &inPosition, const Float3 &inVelocity = Float3(0, 0, 0), float inInvMass = 1.0f) : mPosition(inPosition), mVelocity(inVelocity), mInvMass(inInvMass) { }
134
135 Float3 mPosition { 0, 0, 0 };
136 Float3 mVelocity { 0, 0, 0 };
137 float mInvMass = 1.0f;
138 };
139
142 {
144
145
146 Face() = default;
147 Face(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inMaterialIndex = 0) : mVertex { inVertex1, inVertex2, inVertex3 }, mMaterialIndex(inMaterialIndex) { }
148
150 bool IsDegenerate() const { return mVertex[0] == mVertex[1] || mVertex[0] == mVertex[2] || mVertex[1] == mVertex[2]; }
151
154 };
155
158 {
160
161
162 Edge() = default;
163 Edge(uint32 inVertex1, uint32 inVertex2, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2 }, mCompliance(inCompliance) { }
164
166 uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }
167
169 float mRestLength = 1.0f;
170 float mCompliance = 0.0f;
171 };
172
192 {
194
195
196 DihedralBend() = default;
197 DihedralBend(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inVertex4, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2, inVertex3, inVertex4 }, mCompliance(inCompliance) { }
198
200 uint32 GetMinVertexIndex() const { return min(min(mVertex[0], mVertex[1]), min(mVertex[2], mVertex[3])); }
201
203 float mCompliance = 0.0f;
204 float mInitialAngle = 0.0f;
205 };
206
209 {
211
212
213 Volume() = default;
214 Volume(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inVertex4, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2, inVertex3, inVertex4 }, mCompliance(inCompliance) { }
215
217 uint32 GetMinVertexIndex() const { return min(min(mVertex[0], mVertex[1]), min(mVertex[2], mVertex[3])); }
218
220 float mSixRestVolume = 1.0f;
221 float mCompliance = 0.0f;
222 };
223
226 {
228
229 public:
231 InvBind() = default;
232 InvBind(uint32 inJointIndex, Mat44Arg inInvBind) : mJointIndex(inJointIndex), mInvBind(inInvBind) { }
233
236 };
237
240 {
242
243 public:
245 SkinWeight() = default;
246 SkinWeight(uint32 inInvBindIndex, float inWeight) : mInvBindIndex(inInvBindIndex), mWeight(inWeight) { }
247
249 float mWeight = 0.0f;
250 };
251
254 {
256
257 public:
259 Skinned() = default;
260 Skinned(uint32 inVertex, float inMaxDistance, float inBackStopDistance, float inBackStopRadius) : mVertex(inVertex), mMaxDistance(inMaxDistance), mBackStopDistance(inBackStopDistance), mBackStopRadius(inBackStopRadius) { }
261
264 {
265 // Get the total weight
266 float total = 0.0f;
267 for (const SkinWeight &w : mWeights)
268 total += w.mWeight;
269
270 // Normalize
271 if (total > 0.0f)
272 for (SkinWeight &w : mWeights)
273 w.mWeight /= total;
274 }
275
277 static constexpr uint cMaxSkinWeights = 4;
278
281 float mMaxDistance = FLT_MAX;
282 float mBackStopDistance = FLT_MAX;
283 float mBackStopRadius = 40.0f;
285 };
286
290 {
292
293 public:
295 LRA() = default;
296 LRA(uint32 inVertex1, uint32 inVertex2, float inMaxDistance) : mVertex { inVertex1, inVertex2 }, mMaxDistance(inMaxDistance) { }
297
299 uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }
300
302 float mMaxDistance = 0.0f;
303 };
304
313 {
315
316
317 RodStretchShear() = default;
318 RodStretchShear(uint32 inVertex1, uint32 inVertex2, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2 }, mCompliance(inCompliance) { }
319
321 uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }
322
324 float mLength = 1.0f;
325 float mInvMass = 1.0f;
326 float mCompliance = 0.0f;
328 };
329
332 {
334
335
336 RodBendTwist() = default;
337 RodBendTwist(uint32 inRod1, uint32 inRod2, float inCompliance = 0.0f) : mRod { inRod1, inRod2 }, mCompliance(inCompliance) { }
338
340 float mCompliance = 0.0f;
342 };
343
345 void AddFace(const Face &inFace) { JPH_ASSERT(!inFace.IsDegenerate()); mFaces.push_back(inFace); }
346
358
359private:
361
363 void CalculateClosestKinematic();
364
366 struct ClosestKinematic
367 {
368 uint32 mVertex = 0xffffffff;
369 uint32 mHops = 0xffffffff;
370 float mDistance = FLT_MAX;
371 };
372
374 struct UpdateGroup
375 {
376 uint mEdgeEndIndex;
377 uint mLRAEndIndex;
378 uint mRodStretchShearEndIndex;
379 uint mRodBendTwistEndIndex;
380 uint mDihedralBendEndIndex;
381 uint mVolumeEndIndex;
382 uint mSkinnedEndIndex;
383 };
384
385 Array<ClosestKinematic> mClosestKinematic;
386 Array<UpdateGroup> mUpdateGroups;
387 Array<uint32> mSkinnedConstraintNormals;
388};
389
@ None
No degrees of freedom are allowed. Note that this is not valid and will crash. Use a static body inst...
Definition AllowedDOFs.h:12
@ Distance
Definition Constraint.h:39
#define JPH_EXPORT
Definition Core.h:275
unsigned int uint
Definition Core.h:500
#define JPH_NAMESPACE_END
Definition Core.h:425
std::uint32_t uint32
Definition Core.h:503
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
JPH_INLINE constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition Math.h:16
const Mat44 & Mat44Arg
Definition MathTypes.h:29
Array< RefConst< PhysicsMaterial > > PhysicsMaterialList
Definition PhysicsMaterial.h:55
#define JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(linkage, class_name)
Definition SerializableObject.h:80
Definition Array.h:36
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
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
static RefConst< PhysicsMaterial > sDefault
Default material that is used when a shape has no materials defined.
Definition PhysicsMaterial.h:32
Definition Quat.h:33
static JPH_INLINE Quat sZero()
Definition Quat.h:101
Definition Reference.h:107
Helper class that either contains a valid result or an error.
Definition Result.h:12
Mat44 mInvBind
The inverse bind matrix, this takes a vertex in its bind pose (Vertex::mPosition) to joint local spac...
Definition SoftBodySharedSettings.h:235
InvBind()=default
Constructor.
InvBind(uint32 inJointIndex, Mat44Arg inInvBind)
Definition SoftBodySharedSettings.h:232
uint32 mJointIndex
Joint index to which this is attached.
Definition SoftBodySharedSettings.h:234
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition SoftBodySharedSettings.h:299
uint32 mVertex[2]
The vertices that are connected. The first vertex should be kinematic, the 2nd dynamic.
Definition SoftBodySharedSettings.h:301
LRA()=default
Constructor.
float mMaxDistance
The maximum distance between the vertices, calculated by CalculateLRALengths.
Definition SoftBodySharedSettings.h:302
LRA(uint32 inVertex1, uint32 inVertex2, float inMaxDistance)
Definition SoftBodySharedSettings.h:296
Information about the optimization of the soft body, the indices of certain elements may have changed...
Definition SoftBodySharedSettings.h:81
Array< uint > mLRARemap
Maps old LRA index to new LRA index.
Definition SoftBodySharedSettings.h:84
Array< uint > mRodStretchShearConstraintRemap
Maps old rod stretch shear constraint index to new stretch shear rod constraint index.
Definition SoftBodySharedSettings.h:85
Array< uint > mVolumeRemap
Maps old volume constraint index to new volume constraint index.
Definition SoftBodySharedSettings.h:88
Array< uint > mDihedralBendRemap
Maps old dihedral bend index to new dihedral bend index.
Definition SoftBodySharedSettings.h:87
Array< uint > mSkinnedRemap
Maps old skinned constraint index to new skinned constraint index.
Definition SoftBodySharedSettings.h:89
Array< uint > mEdgeRemap
Maps old edge index to new edge index.
Definition SoftBodySharedSettings.h:83
Array< uint > mRodBendTwistConstraintRemap
Maps old rod bend twist constraint index to new bend twist rod constraint index.
Definition SoftBodySharedSettings.h:86
A joint and its skin weight.
Definition SoftBodySharedSettings.h:240
uint32 mInvBindIndex
Index in mInvBindMatrices.
Definition SoftBodySharedSettings.h:248
float mWeight
Weight with which it is skinned.
Definition SoftBodySharedSettings.h:249
SkinWeight()=default
Constructor.
SkinWeight(uint32 inInvBindIndex, float inWeight)
Definition SoftBodySharedSettings.h:246
float mBackStopRadius
Radius of the backstop sphere. By default this is a fairly large radius so the sphere approximates a ...
Definition SoftBodySharedSettings.h:283
SkinWeight mWeights[cMaxSkinWeights]
Skin weights, the bind pose of the vertex is assumed to be stored in Vertex::mPosition....
Definition SoftBodySharedSettings.h:280
float mMaxDistance
Maximum distance that this vertex can reach from the skinned vertex, disabled when FLT_MAX....
Definition SoftBodySharedSettings.h:281
float mBackStopDistance
Disabled if mBackStopDistance >= mMaxDistance. The faces surrounding mVertex determine an average nor...
Definition SoftBodySharedSettings.h:282
void NormalizeWeights()
Normalize the weights so that they add up to 1.
Definition SoftBodySharedSettings.h:263
Skinned()=default
Constructor.
uint32 mVertex
Index in mVertices which indicates which vertex is being skinned.
Definition SoftBodySharedSettings.h:279
Skinned(uint32 inVertex, float inMaxDistance, float inBackStopDistance, float inBackStopRadius)
Definition SoftBodySharedSettings.h:260
static constexpr uint cMaxSkinWeights
Maximum number of skin weights.
Definition SoftBodySharedSettings.h:277
uint32 mNormalInfo
Information needed to calculate the normal of this vertex, lowest 24 bit is start index in mSkinnedCo...
Definition SoftBodySharedSettings.h:284
Definition SoftBodySharedSettings.h:16
void SaveWithMaterials(StreamOut &inStream, SharedSettingsToIDMap &ioSettingsMap, MaterialToIDMap &ioMaterialMap) const
Save this shared settings and its materials. Pass in an empty map ioSettingsMap / ioMaterialMap or re...
Definition SoftBodySharedSettings.cpp:1272
Array< RodStretchShear > mRodStretchShearConstraints
The list of Cosserat rod constraints that connect two vertices and that limit stretch and shear.
Definition SoftBodySharedSettings.h:355
Array< Vertex > mVertices
The list of vertices or particles of the body.
Definition SoftBodySharedSettings.h:347
ELRAType
The type of long range attachment constraint to create.
Definition SoftBodySharedSettings.h:30
@ None
Don't create a LRA constraint.
Definition SoftBodySharedSettings.h:31
StreamUtils::ObjectToIDMap< PhysicsMaterial > MaterialToIDMap
Definition SoftBodySharedSettings.h:109
Array< LRA > mLRAConstraints
The list of long range attachment constraints.
Definition SoftBodySharedSettings.h:354
EBendType
Which type of bend constraint should be created.
Definition SoftBodySharedSettings.h:22
@ Distance
A simple distance constraint.
Definition SoftBodySharedSettings.h:24
Array< RodBendTwist > mRodBendTwistConstraints
The list of Cosserat rod constraints that connect two rods and limit the bend and twist.
Definition SoftBodySharedSettings.h:356
Array< Skinned > mSkinnedConstraints
The list of vertices that are constrained to a skinned vertex.
Definition SoftBodySharedSettings.h:352
void CalculateSkinnedConstraintNormals()
Calculate information needed to be able to calculate the skinned constraint normals at run-time.
Definition SoftBodySharedSettings.cpp:559
void Optimize()
Optimize the soft body settings without results.
Definition SoftBodySharedSettings.h:96
void CalculateEdgeLengths()
Calculate the initial lengths of all springs of the edges of this soft body (if you use CreateConstra...
Definition SoftBodySharedSettings.cpp:373
void CalculateVolumeConstraintVolumes()
Calculates the initial volume of all tetrahedra of this soft body.
Definition SoftBodySharedSettings.cpp:538
void CalculateLRALengths(float inMaxDistanceMultiplier=1.0f)
Definition SoftBodySharedSettings.cpp:485
static Ref< SoftBodySharedSettings > sCreateCube(uint inGridSize, float inGridSpacing)
Definition SoftBodySharedSettings.cpp:1344
StreamUtils::IDToObjectMap< SoftBodySharedSettings > IDToSharedSettingsMap
Definition SoftBodySharedSettings.h:108
Array< Face > mFaces
The list of faces of the body.
Definition SoftBodySharedSettings.h:348
Array< DihedralBend > mDihedralBendConstraints
The list of dihedral bend constraints of the body.
Definition SoftBodySharedSettings.h:350
void CalculateBendConstraintConstants()
Calculate the constants for the bend constraints (if you use CreateConstraints, this is already done)...
Definition SoftBodySharedSettings.cpp:495
Array< Volume > mVolumeConstraints
The list of volume constraints of the body that keep the volume of tetrahedra in the soft body consta...
Definition SoftBodySharedSettings.h:351
void CreateConstraints(const VertexAttributes *inVertexAttributes, uint inVertexAttributesLength, EBendType inBendType=EBendType::Distance, float inAngleTolerance=DegreesToRadians(8.0f))
Definition SoftBodySharedSettings.cpp:191
StreamUtils::IDToObjectMap< PhysicsMaterial > IDToMaterialMap
Definition SoftBodySharedSettings.h:110
friend class SoftBodyMotionProperties
Definition SoftBodySharedSettings.h:360
void CalculateRodProperties()
Definition SoftBodySharedSettings.cpp:383
Array< Edge > mEdgeConstraints
The list of edges or springs of the body.
Definition SoftBodySharedSettings.h:349
Array< InvBind > mInvBindMatrices
The list of inverse bind matrices for skinning vertices.
Definition SoftBodySharedSettings.h:353
void Optimize(OptimizationResults &outResults)
Optimize the soft body settings for simulation. This will reorder constraints so they can be executed...
Definition SoftBodySharedSettings.cpp:615
void AddFace(const Face &inFace)
Add a face to this soft body.
Definition SoftBodySharedSettings.h:345
static SettingsResult sRestoreWithMaterials(StreamIn &inStream, IDToSharedSettingsMap &ioSettingsMap, IDToMaterialMap &ioMaterialMap)
Restore a shape and materials. Pass in an empty map in ioSettingsMap / ioMaterialMap or reuse the sam...
Definition SoftBodySharedSettings.cpp:1295
Result< Ref< SoftBodySharedSettings > > SettingsResult
Definition SoftBodySharedSettings.h:115
PhysicsMaterialList mMaterials
The materials of the faces of the body, referenced by Face::mMaterialIndex.
Definition SoftBodySharedSettings.h:357
StreamUtils::ObjectToIDMap< SoftBodySharedSettings > SharedSettingsToIDMap
Definition SoftBodySharedSettings.h:107
Simple binary input stream.
Definition StreamIn.h:13
Simple binary output stream.
Definition StreamOut.h:13
UnorderedMap< const Type *, uint32 > ObjectToIDMap
Definition StreamUtils.h:18
Array< Ref< Type > > IDToObjectMap
Definition StreamUtils.h:21
float mInitialAngle
Initial angle between the normals of the triangles (pi - dihedral angle), calculated by CalculateBend...
Definition SoftBodySharedSettings.h:204
uint32 mVertex[4]
Indices of the vertices of the 2 triangles that share an edge (the first 2 vertices are the shared ed...
Definition SoftBodySharedSettings.h:202
DihedralBend()=default
Constructor.
float mCompliance
Inverse of the stiffness of the constraint.
Definition SoftBodySharedSettings.h:203
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition SoftBodySharedSettings.h:200
uint32 mVertex[2]
Indices of the vertices that form the edge.
Definition SoftBodySharedSettings.h:168
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition SoftBodySharedSettings.h:166
float mCompliance
Inverse of the stiffness of the spring.
Definition SoftBodySharedSettings.h:170
float mRestLength
Rest length of the spring, calculated by CalculateEdgeLengths.
Definition SoftBodySharedSettings.h:169
Edge()=default
Constructor.
A face defines the surface of the body.
Definition SoftBodySharedSettings.h:142
Face()=default
Constructor.
uint32 mMaterialIndex
Index of the material of the face in SoftBodySharedSettings::mMaterials.
Definition SoftBodySharedSettings.h:153
bool IsDegenerate() const
Check if this is a degenerate face (a face which points to the same vertex twice).
Definition SoftBodySharedSettings.h:150
uint32 mVertex[3]
Indices of the vertices that form the face.
Definition SoftBodySharedSettings.h:152
RodBendTwist()=default
Constructor.
Quat mOmega0
The initial rotation between the rods: rod1.mBishop.Conjugated() * rod2.mBishop, calculated by Calcul...
Definition SoftBodySharedSettings.h:341
uint32 mRod[2]
Indices of rods that are constrained (index in mRodStretchShearConstraints).
Definition SoftBodySharedSettings.h:339
float mCompliance
Inverse of the stiffness of the rod.
Definition SoftBodySharedSettings.h:340
float mCompliance
Inverse of the stiffness of the rod.
Definition SoftBodySharedSettings.h:326
float mLength
Fixed length of the rod, calculated by CalculateRodProperties.
Definition SoftBodySharedSettings.h:324
Quat mBishop
The Bishop frame of the rod (the rotation of the rod in its rest pose so that it has zero twist towar...
Definition SoftBodySharedSettings.h:327
uint32 mVertex[2]
Indices of the vertices that form the rod.
Definition SoftBodySharedSettings.h:323
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition SoftBodySharedSettings.h:321
float mInvMass
Inverse of the mass of the rod (0 for static rods), calculated by CalculateRodProperties but can be o...
Definition SoftBodySharedSettings.h:325
Definition SoftBodySharedSettings.h:40
float mCompliance
The compliance of the normal edges. Set to FLT_MAX to disable regular edges for any edge involving th...
Definition SoftBodySharedSettings.h:45
float mBendCompliance
The compliance of the bend edges. Set to FLT_MAX to disable bend edges for any bend constraint involv...
Definition SoftBodySharedSettings.h:47
float mLRAMaxDistanceMultiplier
Multiplier for the max distance of the LRA constraint, e.g. 1.01 means the max distance is 1% longer ...
Definition SoftBodySharedSettings.h:49
float mShearCompliance
The compliance of the shear edges. Set to FLT_MAX to disable shear edges for any edge involving this ...
Definition SoftBodySharedSettings.h:46
ELRAType mLRAType
The type of long range attachment constraint to create.
Definition SoftBodySharedSettings.h:48
VertexAttributes(float inCompliance, float inShearCompliance, float inBendCompliance, ELRAType inLRAType=ELRAType::None, float inLRAMaxDistanceMultiplier=1.0f)
Definition SoftBodySharedSettings.h:43
Float3 mVelocity
Initial velocity of the vertex.
Definition SoftBodySharedSettings.h:136
float mInvMass
Initial inverse of the mass of the vertex.
Definition SoftBodySharedSettings.h:137
Vertex()=default
Constructor.
Float3 mPosition
Initial position of the vertex.
Definition SoftBodySharedSettings.h:135
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition SoftBodySharedSettings.h:217
float mCompliance
Inverse of the stiffness of the constraint.
Definition SoftBodySharedSettings.h:221
uint32 mVertex[4]
Indices of the vertices that form the tetrahedron.
Definition SoftBodySharedSettings.h:219
float mSixRestVolume
6 times the rest volume of the tetrahedron, calculated by CalculateVolumeConstraintVolumes
Definition SoftBodySharedSettings.h:220
Volume()=default
Constructor.