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;
17struct PhysicsSettings;
18class Body;
19class Shape;
21#ifdef JPH_DEBUG_RENDERER
22class DebugRenderer;
23#endif // JPH_DEBUG_RENDERER
24
26//
27// Based on: XPBD, Extended Position Based Dynamics, Matthias Muller, Ten Minute Physics
28// See: https://matthias-research.github.io/pages/tenMinutePhysics/09-xpbd.pdf
30{
31public:
36
38 void Initialize(const SoftBodyCreationSettings &inSettings);
39
41 const SoftBodySharedSettings * GetSettings() const { return mSettings; }
42
44 const Array<Vertex> & GetVertices() const { return mVertices; }
45 Array<Vertex> & GetVertices() { return mVertices; }
46
48 const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; }
49 Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; }
50
52 const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; }
53
55 const Array<Face> & GetFaces() const { return mSettings->mFaces; }
56
58 const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; }
59
61 uint32 GetNumIterations() const { return mNumIterations; }
62 void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; }
63
65 float GetPressure() const { return mPressure; }
66 void SetPressure(float inPressure) { mPressure = inPressure; }
67
69 bool GetUpdatePosition() const { return mUpdatePosition; }
70 void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; }
71
73 const AABox & GetLocalBounds() const { return mLocalBounds; }
74
76 float GetVolume() const { return GetVolumeTimesSix() / 6.0f; }
77
80
81#ifdef JPH_DEBUG_RENDERER
83 void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
84 void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
85 void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
86 void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const;
87#endif // JPH_DEBUG_RENDERER
88
90 void SaveState(StateRecorder &inStream) const;
91
93 void RestoreState(StateRecorder &inStream);
94
96 void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext);
97
99 void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem);
100
102 enum class EStatus
103 {
104 NoWork = 1 << 0,
105 DidWork = 1 << 1,
106 Done = 1 << 2,
107 };
108
110 EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
111
113 void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, PhysicsSystem &inSystem);
114
115private:
116 // Collect information about the colliding bodies
117 struct CollidingShape
118 {
120 Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const
121 {
122 return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM);
123 }
124
125 Mat44 mCenterOfMassTransform;
126 RefConst<Shape> mShape;
127 BodyID mBodyID;
128 EMotionType mMotionType;
129 float mInvMass;
130 float mFriction;
131 float mRestitution;
132 bool mUpdateVelocities;
133 Mat44 mInvInertia;
134 Vec3 mLinearVelocity;
135 Vec3 mAngularVelocity;
136 Vec3 mOriginalLinearVelocity;
137 Vec3 mOriginalAngularVelocity;
138 };
139
141 void DetermineCollisionPlanes(const SoftBodyUpdateContext &inContext, uint inVertexStart, uint inNumVertices);
142
144 void ApplyPressure(const SoftBodyUpdateContext &inContext);
145
147 void IntegratePositions(const SoftBodyUpdateContext &inContext);
148
150 void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext);
151
153 void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex);
154
156 void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext);
157
159 void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
160
162 void StartNextIteration(const SoftBodyUpdateContext &ioContext);
163
165 EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext);
166
168 EStatus ParallelApplyEdgeConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings);
169
171 float GetVolumeTimesSix() const;
172
174 Array<Vertex> mVertices;
175 Array<CollidingShape> mCollidingShapes;
176 AABox mLocalBounds;
177 AABox mLocalPredictedBounds;
178 uint32 mNumIterations;
179 float mPressure;
180 bool mUpdatePosition;
181};
182
@ CalculateMassAndInertia
Tells the system to calculate the mass and inertia based on density.
#define JPH_EXPORT
Definition: Core.h:214
unsigned int uint
Definition: Core.h:426
#define JPH_NAMESPACE_END
Definition: Core.h:354
std::uint32_t uint32
Definition: Core.h:429
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:348
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
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:30
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
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:138
void SaveState(StateRecorder &inStream) const
Saving state for replay.
Definition: MotionProperties.cpp:123
Definition: PhysicsSystem.h:28
Definition: Reference.h:151
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition: Shape.h:174
Definition: SoftBodyCreationSettings.h:18
This class contains the runtime information of a soft body.
Definition: SoftBodyMotionProperties.h:30
Vertex & GetVertex(uint inIndex)
Definition: SoftBodyMotionProperties.h:49
EStatus
Return code for ParallelUpdate.
Definition: SoftBodyMotionProperties.h:103
Array< Vertex > & GetVertices()
Definition: SoftBodyMotionProperties.h:45
const PhysicsMaterialList & GetMaterials() const
Get the materials of the soft body.
Definition: SoftBodyMotionProperties.h:52
const Array< Vertex > & GetVertices() const
Get the vertices of the soft body.
Definition: SoftBodyMotionProperties.h:44
void SetUpdatePosition(bool inUpdatePosition)
Definition: SoftBodyMotionProperties.h:70
void SetPressure(float inPressure)
Definition: SoftBodyMotionProperties.h:66
const Array< Face > & GetFaces() const
Get the faces of the soft body.
Definition: SoftBodyMotionProperties.h:55
const AABox & GetLocalBounds() const
Get local bounding box.
Definition: SoftBodyMotionProperties.h:73
const Vertex & GetVertex(uint inIndex) const
Access an individual vertex.
Definition: SoftBodyMotionProperties.h:48
void SetNumIterations(uint32 inNumIterations)
Definition: SoftBodyMotionProperties.h:62
bool GetUpdatePosition() const
Update the position of the body while simulating (set to false for something that is attached to the ...
Definition: SoftBodyMotionProperties.h:69
float GetVolume() const
Get the volume of the soft body. Note can become negative if the shape is inside out!
Definition: SoftBodyMotionProperties.h:76
uint32 GetNumIterations() const
Get the number of solver iterations.
Definition: SoftBodyMotionProperties.h:61
float GetPressure() const
Get the pressure of the soft body.
Definition: SoftBodyMotionProperties.h:65
const SoftBodySharedSettings * GetSettings() const
Get the shared settings of the soft body.
Definition: SoftBodyMotionProperties.h:41
const Face & GetFace(uint inIndex) const
Access to an individual face.
Definition: SoftBodyMotionProperties.h:58
Definition: SoftBodySharedSettings.h:16
Temporary data used by the update of a soft body.
Definition: SoftBodyUpdateContext.h:17
Definition: SoftBodyVertex.h:16
Definition: StateRecorder.h:48
Definition: Vec3.h:16
JPH_INLINE Vec3 Cross(Vec3Arg inV2) const
Cross product.
Definition: Vec3.inl:582
Definition: PhysicsSettings.h:28
An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length.
Definition: SoftBodySharedSettings.h:90
A face defines the surface of the body.
Definition: SoftBodySharedSettings.h:74
Volume constraint, keeps the volume of a tetrahedron constant.
Definition: SoftBodySharedSettings.h:104