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{
17public:
19
20
21 enum class EBendType
22 {
23 None,
24 Distance,
25 Dihedral,
26 };
27
29 enum class ELRAType
30 {
31 None,
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;
48 ELRAType mLRAType = ELRAType::None;
49 float mLRAMaxDistanceMultiplier = 1.0f;
50 };
51
57 void CreateConstraints(const VertexAttributes *inVertexAttributes, uint inVertexAttributesLength, EBendType inBendType = EBendType::Distance, float inAngleTolerance = DegreesToRadians(8.0f));
58
60 void CalculateEdgeLengths();
61
64 void CalculateLRALengths(float inMaxDistanceMultiplier = 1.0f);
65
67 void CalculateBendConstraintConstants();
68
70 void CalculateVolumeConstraintVolumes();
71
73 void CalculateSkinnedConstraintNormals();
74
77 {
78 public:
81 };
82
84 void Optimize(OptimizationResults &outResults);
85
87 void Optimize() { OptimizationResults results; Optimize(results); }
88
90 Ref<SoftBodySharedSettings> Clone() const;
91
93 void SaveBinaryState(StreamOut &inStream) const;
94
96 void RestoreBinaryState(StreamIn &inStream);
97
102
104 void SaveWithMaterials(StreamOut &inStream, SharedSettingsToIDMap &ioSettingsMap, MaterialToIDMap &ioMaterialMap) const;
105
107
109 static SettingsResult sRestoreWithMaterials(StreamIn &inStream, IDToSharedSettingsMap &ioSettingsMap, IDToMaterialMap &ioMaterialMap);
110
113 {
115
116
117 Vertex() = default;
118 Vertex(const Float3 &inPosition, const Float3 &inVelocity = Float3(0, 0, 0), float inInvMass = 1.0f) : mPosition(inPosition), mVelocity(inVelocity), mInvMass(inInvMass) { }
119
120 Float3 mPosition { 0, 0, 0 };
121 Float3 mVelocity { 0, 0, 0 };
122 float mInvMass = 1.0f;
123 };
124
127 {
129
130
131 Face() = default;
132 Face(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inMaterialIndex = 0) : mVertex { inVertex1, inVertex2, inVertex3 }, mMaterialIndex(inMaterialIndex) { }
133
135 bool IsDegenerate() const { return mVertex[0] == mVertex[1] || mVertex[0] == mVertex[2] || mVertex[1] == mVertex[2]; }
136
137 uint32 mVertex[3];
138 uint32 mMaterialIndex = 0;
139 };
140
143 {
145
146
147 Edge() = default;
148 Edge(uint32 inVertex1, uint32 inVertex2, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2 }, mCompliance(inCompliance) { }
149
151 uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }
152
153 uint32 mVertex[2];
154 float mRestLength = 1.0f;
155 float mCompliance = 0.0f;
156 };
157
177 {
179
180
181 DihedralBend() = default;
182 DihedralBend(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inVertex4, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2, inVertex3, inVertex4 }, mCompliance(inCompliance) { }
183
185 uint32 GetMinVertexIndex() const { return min(min(mVertex[0], mVertex[1]), min(mVertex[2], mVertex[3])); }
186
187 uint32 mVertex[4];
188 float mCompliance = 0.0f;
189 float mInitialAngle = 0.0f;
190 };
191
194 {
196
197
198 Volume() = default;
199 Volume(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inVertex4, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2, inVertex3, inVertex4 }, mCompliance(inCompliance) { }
200
202 uint32 GetMinVertexIndex() const { return min(min(mVertex[0], mVertex[1]), min(mVertex[2], mVertex[3])); }
203
204 uint32 mVertex[4];
205 float mSixRestVolume = 1.0f;
206 float mCompliance = 0.0f;
207 };
208
211 {
213
214 public:
216 InvBind() = default;
217 InvBind(uint32 inJointIndex, Mat44Arg inInvBind) : mJointIndex(inJointIndex), mInvBind(inInvBind) { }
218
219 uint32 mJointIndex = 0;
220 Mat44 mInvBind = Mat44::sIdentity();
221 };
222
225 {
227
228 public:
230 SkinWeight() = default;
231 SkinWeight(uint32 inInvBindIndex, float inWeight) : mInvBindIndex(inInvBindIndex), mWeight(inWeight) { }
232
233 uint32 mInvBindIndex = 0;
234 float mWeight = 0.0f;
235 };
236
239 {
241
242 public:
244 Skinned() = default;
245 Skinned(uint32 inVertex, float inMaxDistance, float inBackStopDistance, float inBackStopRadius) : mVertex(inVertex), mMaxDistance(inMaxDistance), mBackStopDistance(inBackStopDistance), mBackStopRadius(inBackStopRadius) { }
246
249 {
250 // Get the total weight
251 float total = 0.0f;
252 for (const SkinWeight &w : mWeights)
253 total += w.mWeight;
254
255 // Normalize
256 if (total > 0.0f)
257 for (SkinWeight &w : mWeights)
258 w.mWeight /= total;
259 }
260
261 uint32 mVertex = 0;
262 SkinWeight mWeights[4];
263 float mMaxDistance = FLT_MAX;
264 float mBackStopDistance = FLT_MAX;
265 float mBackStopRadius = 40.0f;
266 uint32 mNormalInfo = 0;
267 };
268
272 {
274
275 public:
277 LRA() = default;
278 LRA(uint32 inVertex1, uint32 inVertex2, float inMaxDistance) : mVertex { inVertex1, inVertex2 }, mMaxDistance(inMaxDistance) { }
279
281 uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }
282
283 uint32 mVertex[2];
284 float mMaxDistance = 0.0f;
285 };
286
288 void AddFace(const Face &inFace) { JPH_ASSERT(!inFace.IsDegenerate()); mFaces.push_back(inFace); }
289
299 float mVertexRadius = 0.0f;
300
301private:
303
305 struct ClosestKinematic
306 {
307 uint32 mVertex = 0xffffffff;
308 float mDistance = FLT_MAX;
309 };
310
312 void CalculateClosestKinematic();
313
315 uint GetEdgeGroupSize(uint inGroupIdx) const { return inGroupIdx == 0? mEdgeGroupEndIndices[0] : mEdgeGroupEndIndices[inGroupIdx] - mEdgeGroupEndIndices[inGroupIdx - 1]; }
316
317 Array<ClosestKinematic> mClosestKinematic;
318 Array<uint> mEdgeGroupEndIndices;
319 Array<uint32> mSkinnedConstraintNormals;
320};
321
@ None
No degrees of freedom are allowed. Note that this is not valid and will crash. Use a static body inst...
#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
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
constexpr float DegreesToRadians(float inV)
Convert a value from degrees to radians.
Definition: Math.h:13
Array< RefConst< PhysicsMaterial > > PhysicsMaterialList
Definition: PhysicsMaterial.h:50
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
#define JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(linkage, class_name)
Definition: SerializableObject.h:71
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:31
Definition: Reference.h:101
Definition: Reference.h:35
Helper class that either contains a valid result or an error.
Definition: Result.h:15
This class contains the runtime information of a soft body.
Definition: SoftBodyMotionProperties.h:33
An inverse bind matrix take a skinned vertex from its bind pose into joint local space.
Definition: SoftBodySharedSettings.h:211
InvBind()=default
Constructor.
InvBind(uint32 inJointIndex, Mat44Arg inInvBind)
Definition: SoftBodySharedSettings.h:217
Definition: SoftBodySharedSettings.h:272
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition: SoftBodySharedSettings.h:281
LRA()=default
Constructor.
LRA(uint32 inVertex1, uint32 inVertex2, float inMaxDistance)
Definition: SoftBodySharedSettings.h:278
Information about the optimization of the soft body, the indices of certain elements may have changed...
Definition: SoftBodySharedSettings.h:77
Array< uint > mDihedralBendRemap
Maps old dihedral bend index to new dihedral bend index.
Definition: SoftBodySharedSettings.h:80
Array< uint > mEdgeRemap
Maps old edge index to new edge index.
Definition: SoftBodySharedSettings.h:79
A joint and its skin weight.
Definition: SoftBodySharedSettings.h:225
SkinWeight()=default
Constructor.
SkinWeight(uint32 inInvBindIndex, float inWeight)
Definition: SoftBodySharedSettings.h:231
A constraint that skins a vertex to joints and limits the distance that the simulated vertex can trav...
Definition: SoftBodySharedSettings.h:239
void NormalizeWeights()
Normalize the weights so that they add up to 1.
Definition: SoftBodySharedSettings.h:248
Skinned()=default
Constructor.
Skinned(uint32 inVertex, float inMaxDistance, float inBackStopDistance, float inBackStopRadius)
Definition: SoftBodySharedSettings.h:245
Definition: SoftBodySharedSettings.h:16
Array< Vertex > mVertices
The list of vertices or particles of the body.
Definition: SoftBodySharedSettings.h:290
ELRAType
The type of long range attachment constraint to create.
Definition: SoftBodySharedSettings.h:30
StreamUtils::ObjectToIDMap< PhysicsMaterial > MaterialToIDMap
Definition: SoftBodySharedSettings.h:100
Array< LRA > mLRAConstraints
The list of long range attachment constraints.
Definition: SoftBodySharedSettings.h:297
EBendType
Which type of bend constraint should be created.
Definition: SoftBodySharedSettings.h:22
Array< Skinned > mSkinnedConstraints
The list of vertices that are constrained to a skinned vertex.
Definition: SoftBodySharedSettings.h:295
void Optimize()
Optimize the soft body settings without results.
Definition: SoftBodySharedSettings.h:87
StreamUtils::IDToObjectMap< SoftBodySharedSettings > IDToSharedSettingsMap
Definition: SoftBodySharedSettings.h:99
Array< Face > mFaces
The list of faces of the body.
Definition: SoftBodySharedSettings.h:291
Array< DihedralBend > mDihedralBendConstraints
The list of dihedral bend constraints of the body.
Definition: SoftBodySharedSettings.h:293
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:294
StreamUtils::IDToObjectMap< PhysicsMaterial > IDToMaterialMap
Definition: SoftBodySharedSettings.h:101
Array< Edge > mEdgeConstraints
The list of edges or springs of the body.
Definition: SoftBodySharedSettings.h:292
Array< InvBind > mInvBindMatrices
The list of inverse bind matrices for skinning vertices.
Definition: SoftBodySharedSettings.h:296
void AddFace(const Face &inFace)
Add a face to this soft body.
Definition: SoftBodySharedSettings.h:288
StreamUtils::ObjectToIDMap< SoftBodySharedSettings > SharedSettingsToIDMap
Definition: SoftBodySharedSettings.h:98
Simple binary input stream.
Definition: StreamIn.h:13
Simple binary output stream.
Definition: StreamOut.h:13
Array< Ref< Type > > IDToObjectMap
Definition: StreamUtils.h:21
UnorderedMap< const Type *, uint32 > ObjectToIDMap
Definition: StreamUtils.h:18
Definition: SoftBodySharedSettings.h:177
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition: SoftBodySharedSettings.h:185
An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length.
Definition: SoftBodySharedSettings.h:143
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition: SoftBodySharedSettings.h:151
A face defines the surface of the body.
Definition: SoftBodySharedSettings.h:127
bool IsDegenerate() const
Check if this is a degenerate face (a face which points to the same vertex twice)
Definition: SoftBodySharedSettings.h:135
Definition: SoftBodySharedSettings.h:40
VertexAttributes(float inCompliance, float inShearCompliance, float inBendCompliance, ELRAType inLRAType=ELRAType::None, float inLRAMaxDistanceMultiplier=1.0f)
Definition: SoftBodySharedSettings.h:43
A vertex is a particle, the data in this structure is only used during creation of the soft body and ...
Definition: SoftBodySharedSettings.h:113
Volume constraint, keeps the volume of a tetrahedron constant.
Definition: SoftBodySharedSettings.h:194
uint32 GetMinVertexIndex() const
Return the lowest vertex index of this constraint.
Definition: SoftBodySharedSettings.h:202