| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Geometry/AABox.h> |
| 8 |
#include <Jolt/Physics/Body/BodyID.h> |
| 9 |
#include <Jolt/Physics/Body/MotionProperties.h> |
| 10 |
#include <Jolt/Physics/Collision/TransformedShape.h> |
| 11 |
#include <Jolt/Physics/SoftBody/SoftBodySharedSettings.h> |
| 12 |
#include <Jolt/Physics/SoftBody/SoftBodyVertex.h> |
| 13 |
#include <Jolt/Physics/SoftBody/SoftBodyUpdateContext.h> |
| 14 |
|
| 15 |
JPH_NAMESPACE_BEGIN |
| 16 |
|
| 17 |
class PhysicsSystem; |
| 18 |
class BodyInterface; |
| 19 |
class BodyLockInterface; |
| 20 |
struct PhysicsSettings; |
| 21 |
class Body; |
| 22 |
class Shape; |
| 23 |
class SoftBodyCreationSettings; |
| 24 |
class TempAllocator; |
| 25 |
#ifdef JPH_DEBUG_RENDERER |
| 26 |
class DebugRenderer; |
| 27 |
enum class ESoftBodyConstraintColor; |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class JPH_EXPORT SoftBodyMotionProperties : public MotionProperties |
| 35 |
{ |
| 36 |
public: |
| 37 |
using Vertex = SoftBodyVertex; |
| 38 |
using Edge = SoftBodySharedSettings::Edge; |
| 39 |
using RodStretchShear = SoftBodySharedSettings::RodStretchShear; |
| 40 |
using RodBendTwist = SoftBodySharedSettings::RodBendTwist; |
| 41 |
using Face = SoftBodySharedSettings::Face; |
| 42 |
using DihedralBend = SoftBodySharedSettings::DihedralBend; |
| 43 |
using Volume = SoftBodySharedSettings::Volume; |
| 44 |
using InvBind = SoftBodySharedSettings::InvBind; |
| 45 |
using SkinWeight = SoftBodySharedSettings::SkinWeight; |
| 46 |
using Skinned = SoftBodySharedSettings::Skinned; |
| 47 |
using LRA = SoftBodySharedSettings::LRA; |
| 48 |
|
| 49 |
|
| 50 |
void Initialize(const SoftBodyCreationSettings &inSettings); |
| 51 |
|
| 52 |
|
| 53 |
const SoftBodySharedSettings * GetSettings() const { return mSettings; } |
| 54 |
|
| 55 |
|
| 56 |
const Array<Vertex> & GetVertices() const { return mVertices; } |
| 57 |
Array<Vertex> & GetVertices() { return mVertices; } |
| 58 |
|
| 59 |
|
| 60 |
const Vertex & GetVertex(uint inIndex) const { return mVertices[inIndex]; } |
| 61 |
Vertex & GetVertex(uint inIndex) { return mVertices[inIndex]; } |
| 62 |
|
| 63 |
|
| 64 |
Quat GetRodRotation(uint inIndex) const { return mRodStates[inIndex].mRotation; } |
| 65 |
Vec3 GetRodAngularVelocity(uint inIndex) const { return mRodStates[inIndex].mAngularVelocity; } |
| 66 |
|
| 67 |
|
| 68 |
const PhysicsMaterialList & GetMaterials() const { return mSettings->mMaterials; } |
| 69 |
|
| 70 |
|
| 71 |
const Array<Face> & GetFaces() const { return mSettings->mFaces; } |
| 72 |
|
| 73 |
|
| 74 |
const Face & GetFace(uint inIndex) const { return mSettings->mFaces[inIndex]; } |
| 75 |
|
| 76 |
|
| 77 |
uint32 GetNumIterations() const { return mNumIterations; } |
| 78 |
void SetNumIterations(uint32 inNumIterations) { mNumIterations = inNumIterations; } |
| 79 |
|
| 80 |
|
| 81 |
float GetPressure() const { return mPressure; } |
| 82 |
void SetPressure(float inPressure) { mPressure = inPressure; } |
| 83 |
|
| 84 |
|
| 85 |
bool GetUpdatePosition() const { return mUpdatePosition; } |
| 86 |
void SetUpdatePosition(bool inUpdatePosition) { mUpdatePosition = inUpdatePosition; } |
| 87 |
|
| 88 |
|
| 89 |
bool GetFacesDoubleSided() const { return mFacesDoubleSided; } |
| 90 |
void SetFacesDoubleSided(bool inDoubleSided) { mFacesDoubleSided = inDoubleSided; } |
| 91 |
|
| 92 |
|
| 93 |
bool GetEnableSkinConstraints() const { return mEnableSkinConstraints; } |
| 94 |
void SetEnableSkinConstraints(bool inEnableSkinConstraints) { mEnableSkinConstraints = inEnableSkinConstraints; } |
| 95 |
|
| 96 |
|
| 97 |
float GetSkinnedMaxDistanceMultiplier() const { return mSkinnedMaxDistanceMultiplier; } |
| 98 |
void SetSkinnedMaxDistanceMultiplier(float inSkinnedMaxDistanceMultiplier) { mSkinnedMaxDistanceMultiplier = inSkinnedMaxDistanceMultiplier; } |
| 99 |
|
| 100 |
|
| 101 |
float GetVertexRadius() const { return mVertexRadius; } |
| 102 |
void SetVertexRadius(float inVertexRadius) { JPH_ASSERT(mVertexRadius >= 0.0f); mVertexRadius = inVertexRadius; } |
| 103 |
|
| 104 |
|
| 105 |
const AABox & GetLocalBounds() const { return mLocalBounds; } |
| 106 |
|
| 107 |
|
| 108 |
float GetVolume() const { return GetVolumeTimesSix() / 6.0f; } |
| 109 |
|
| 110 |
|
| 111 |
void CalculateMassAndInertia(); |
| 112 |
|
| 113 |
#ifdef JPH_DEBUG_RENDERER |
| 114 |
|
| 115 |
void DrawVertices(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const; |
| 116 |
void DrawVertexVelocities(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const; |
| 117 |
void DrawEdgeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 118 |
void DrawRods(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 119 |
void DrawRodStates(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 120 |
void DrawRodBendTwistConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 121 |
void DrawBendConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 122 |
void DrawVolumeConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 123 |
void DrawSkinConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 124 |
void DrawLRAConstraints(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, ESoftBodyConstraintColor inConstraintColor) const; |
| 125 |
void DrawPredictedBounds(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform) const; |
| 126 |
#endif |
| 127 |
|
| 128 |
|
| 129 |
void SaveState(StateRecorder &inStream) const; |
| 130 |
|
| 131 |
|
| 132 |
void RestoreState(StateRecorder &inStream); |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
void SkinVertices(RMat44Arg inCenterOfMassTransform, const Mat44 *inJointMatrices, uint inNumJoints, bool inHardSkinAll, TempAllocator &ioTempAllocator); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void CustomUpdate(float inDeltaTime, Body &ioSoftBody, PhysicsSystem &inSystem); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
void InitializeUpdateContext(float inDeltaTime, Body &inSoftBody, const PhysicsSystem &inSystem, SoftBodyUpdateContext &ioContext); |
| 161 |
|
| 162 |
|
| 163 |
void DetermineCollidingShapes(const SoftBodyUpdateContext &inContext, const PhysicsSystem &inSystem, const BodyLockInterface &inBodyLockInterface); |
| 164 |
|
| 165 |
|
| 166 |
enum class EStatus |
| 167 |
{ |
| 168 |
NoWork = 1 << 0, |
| 169 |
DidWork = 1 << 1, |
| 170 |
Done = 1 << 2, |
| 171 |
}; |
| 172 |
|
| 173 |
|
| 174 |
EStatus ParallelUpdate(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings); |
| 175 |
|
| 176 |
|
| 177 |
void UpdateRigidBodyVelocities(const SoftBodyUpdateContext &inContext, BodyInterface &inBodyInterface); |
| 178 |
|
| 179 |
private: |
| 180 |
|
| 181 |
friend class SoftBodyManifold; |
| 182 |
|
| 183 |
|
| 184 |
struct LeafShape |
| 185 |
{ |
| 186 |
LeafShape() = default; |
| 187 |
LeafShape(Mat44Arg inTransform, Vec3Arg inScale, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mShape(inShape) { } |
| 188 |
|
| 189 |
Mat44 mTransform; |
| 190 |
Vec3 mScale; |
| 191 |
RefConst<Shape> mShape; |
| 192 |
}; |
| 193 |
|
| 194 |
|
| 195 |
struct CollidingShape |
| 196 |
{ |
| 197 |
|
| 198 |
Vec3 GetPointVelocity(Vec3Arg inPointRelativeToCOM) const |
| 199 |
{ |
| 200 |
return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM); |
| 201 |
} |
| 202 |
|
| 203 |
Mat44 mCenterOfMassTransform; |
| 204 |
Array<LeafShape> mShapes; |
| 205 |
BodyID mBodyID; |
| 206 |
EMotionType mMotionType; |
| 207 |
float mInvMass; |
| 208 |
float mFriction; |
| 209 |
float mRestitution; |
| 210 |
float mSoftBodyInvMassScale; |
| 211 |
bool mUpdateVelocities; |
| 212 |
Mat44 mInvInertia; |
| 213 |
Vec3 mLinearVelocity; |
| 214 |
Vec3 mAngularVelocity; |
| 215 |
Vec3 mOriginalLinearVelocity; |
| 216 |
Vec3 mOriginalAngularVelocity; |
| 217 |
}; |
| 218 |
|
| 219 |
|
| 220 |
struct CollidingSensor |
| 221 |
{ |
| 222 |
Mat44 mCenterOfMassTransform; |
| 223 |
Array<LeafShape> mShapes; |
| 224 |
BodyID mBodyID; |
| 225 |
bool mHasContact; |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
struct RodState |
| 230 |
{ |
| 231 |
Quat mRotation; |
| 232 |
union |
| 233 |
{ |
| 234 |
Vec3 mAngularVelocity; |
| 235 |
Quat mPreviousRotationInternal; |
| 236 |
}; |
| 237 |
}; |
| 238 |
|
| 239 |
|
| 240 |
struct SkinState |
| 241 |
{ |
| 242 |
Vec3 mPreviousPosition = Vec3::sZero(); |
| 243 |
Vec3 mPosition = Vec3::sNaN(); |
| 244 |
Vec3 mNormal = Vec3::sNaN(); |
| 245 |
}; |
| 246 |
|
| 247 |
|
| 248 |
void DetermineCollisionPlanes(uint inVertexStart, uint inNumVertices); |
| 249 |
|
| 250 |
|
| 251 |
void DetermineSensorCollisions(CollidingSensor &ioSensor); |
| 252 |
|
| 253 |
|
| 254 |
void ApplyPressure(const SoftBodyUpdateContext &inContext); |
| 255 |
|
| 256 |
|
| 257 |
void IntegratePositions(const SoftBodyUpdateContext &inContext); |
| 258 |
|
| 259 |
|
| 260 |
void ApplyDihedralBendConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex); |
| 261 |
|
| 262 |
|
| 263 |
void ApplyVolumeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex); |
| 264 |
|
| 265 |
|
| 266 |
void ApplySkinConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex); |
| 267 |
|
| 268 |
|
| 269 |
void ApplyEdgeConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex); |
| 270 |
|
| 271 |
|
| 272 |
void ApplyRodStretchShearConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex); |
| 273 |
void ApplyRodBendTwistConstraints(const SoftBodyUpdateContext &inContext, uint inStartIndex, uint inEndIndex); |
| 274 |
|
| 275 |
|
| 276 |
void ApplyLRAConstraints(uint inStartIndex, uint inEndIndex); |
| 277 |
|
| 278 |
|
| 279 |
void ApplyCollisionConstraintsAndUpdateVelocities(const SoftBodyUpdateContext &inContext); |
| 280 |
|
| 281 |
|
| 282 |
void UpdateSoftBodyState(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings); |
| 283 |
|
| 284 |
|
| 285 |
void StartFirstIteration(SoftBodyUpdateContext &ioContext); |
| 286 |
|
| 287 |
|
| 288 |
void StartNextIteration(const SoftBodyUpdateContext &ioContext); |
| 289 |
|
| 290 |
|
| 291 |
EStatus ParallelDetermineCollisionPlanes(SoftBodyUpdateContext &ioContext); |
| 292 |
|
| 293 |
|
| 294 |
EStatus ParallelDetermineSensorCollisions(SoftBodyUpdateContext &ioContext); |
| 295 |
|
| 296 |
|
| 297 |
EStatus ParallelApplyConstraints(SoftBodyUpdateContext &ioContext, const PhysicsSettings &inPhysicsSettings); |
| 298 |
|
| 299 |
|
| 300 |
void ProcessGroup(const SoftBodyUpdateContext &ioContext, uint inGroupIndex); |
| 301 |
|
| 302 |
|
| 303 |
float GetVolumeTimesSix() const; |
| 304 |
|
| 305 |
#ifdef JPH_DEBUG_RENDERER |
| 306 |
|
| 307 |
template <typename GetEndIndex, typename DrawConstraint> |
| 308 |
inline void DrawConstraints(ESoftBodyConstraintColor inConstraintColor, const GetEndIndex &inGetEndIndex, const DrawConstraint &inDrawConstraint, ColorArg inBaseColor) const; |
| 309 |
|
| 310 |
RMat44 mSkinStateTransform = RMat44::sIdentity(); |
| 311 |
#endif |
| 312 |
|
| 313 |
RefConst<SoftBodySharedSettings> mSettings; |
| 314 |
Array<Vertex> mVertices; |
| 315 |
Array<RodState> mRodStates; |
| 316 |
Array<CollidingShape> mCollidingShapes; |
| 317 |
Array<CollidingSensor> mCollidingSensors; |
| 318 |
Array<SkinState> mSkinState; |
| 319 |
AABox mLocalBounds; |
| 320 |
AABox mLocalPredictedBounds; |
| 321 |
uint32 mNumIterations; |
| 322 |
uint mNumSensors; |
| 323 |
float mPressure; |
| 324 |
float mSkinnedMaxDistanceMultiplier = 1.0f; |
| 325 |
float mVertexRadius = 0.0f; |
| 326 |
bool mUpdatePosition; |
| 327 |
bool mFacesDoubleSided; |
| 328 |
atomic<bool> mNeedContactCallback = false; |
| 329 |
bool mEnableSkinConstraints = true; |
| 330 |
bool mSkinStatePreviousPositionValid = false; |
| 331 |
}; |
| 332 |
|
| 333 |
JPH_NAMESPACE_END |