| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Geometry/Sphere.h> |
| 8 |
#include <Jolt/Physics/Body/AllowedDOFs.h> |
| 9 |
#include <Jolt/Physics/Body/MotionQuality.h> |
| 10 |
#include <Jolt/Physics/Body/BodyAccess.h> |
| 11 |
#include <Jolt/Physics/Body/MotionType.h> |
| 12 |
#include <Jolt/Physics/Body/BodyType.h> |
| 13 |
#include <Jolt/Physics/Body/MassProperties.h> |
| 14 |
#include <Jolt/Physics/DeterminismLog.h> |
| 15 |
|
| 16 |
JPH_NAMESPACE_BEGIN |
| 17 |
|
| 18 |
class StateRecorder; |
| 19 |
|
| 20 |
|
| 21 |
enum class ECanSleep |
| 22 |
{ |
| 23 |
CannotSleep = 0, |
| 24 |
CanSleep = 1, |
| 25 |
}; |
| 26 |
|
| 27 |
|
| 28 |
class JPH_EXPORT MotionProperties |
| 29 |
{ |
| 30 |
public: |
| 31 |
JPH_OVERRIDE_NEW_DELETE |
| 32 |
|
| 33 |
|
| 34 |
EMotionQuality GetMotionQuality() const { return mMotionQuality; } |
| 35 |
|
| 36 |
|
| 37 |
inline EAllowedDOFs GetAllowedDOFs() const { return mAllowedDOFs; } |
| 38 |
|
| 39 |
|
| 40 |
inline bool GetAllowSleeping() const { return mAllowSleeping; } |
| 41 |
|
| 42 |
|
| 43 |
inline Vec3 GetLinearVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::Read)); return mLinearVelocity; } |
| 44 |
|
| 45 |
|
| 46 |
void SetLinearVelocity(Vec3Arg inLinearVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inLinearVelocity.Length() <= mMaxLinearVelocity); mLinearVelocity = LockTranslation(inLinearVelocity); } |
| 47 |
|
| 48 |
|
| 49 |
void SetLinearVelocityClamped(Vec3Arg inLinearVelocity) { mLinearVelocity = LockTranslation(inLinearVelocity); ClampLinearVelocity(); } |
| 50 |
|
| 51 |
|
| 52 |
inline Vec3 GetAngularVelocity() const { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::Read)); return mAngularVelocity; } |
| 53 |
|
| 54 |
|
| 55 |
void SetAngularVelocity(Vec3Arg inAngularVelocity) { JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); JPH_ASSERT(inAngularVelocity.Length() <= mMaxAngularVelocity); mAngularVelocity = LockAngular(inAngularVelocity); } |
| 56 |
|
| 57 |
|
| 58 |
void SetAngularVelocityClamped(Vec3Arg inAngularVelocity) { mAngularVelocity = LockAngular(inAngularVelocity); ClampAngularVelocity(); } |
| 59 |
|
| 60 |
|
| 61 |
inline void MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRotation, float inDeltaTime); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
inline float GetMaxLinearVelocity() const { return mMaxLinearVelocity; } |
| 68 |
inline void SetMaxLinearVelocity(float inLinearVelocity) { JPH_ASSERT(inLinearVelocity >= 0.0f); mMaxLinearVelocity = inLinearVelocity; } |
| 69 |
|
| 70 |
|
| 71 |
inline float GetMaxAngularVelocity() const { return mMaxAngularVelocity; } |
| 72 |
inline void SetMaxAngularVelocity(float inAngularVelocity) { JPH_ASSERT(inAngularVelocity >= 0.0f); mMaxAngularVelocity = inAngularVelocity; } |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
inline void ClampLinearVelocity(); |
| 77 |
inline void ClampAngularVelocity(); |
| 78 |
|
| 79 |
|
| 80 |
inline float GetLinearDamping() const { return mLinearDamping; } |
| 81 |
void SetLinearDamping(float inLinearDamping) { JPH_ASSERT(inLinearDamping >= 0.0f); mLinearDamping = inLinearDamping; } |
| 82 |
|
| 83 |
|
| 84 |
inline float GetAngularDamping() const { return mAngularDamping; } |
| 85 |
void SetAngularDamping(float inAngularDamping) { JPH_ASSERT(inAngularDamping >= 0.0f); mAngularDamping = inAngularDamping; } |
| 86 |
|
| 87 |
|
| 88 |
inline float GetGravityFactor() const { return mGravityFactor; } |
| 89 |
void SetGravityFactor(float inGravityFactor) { mGravityFactor = inGravityFactor; } |
| 90 |
|
| 91 |
|
| 92 |
void SetMassProperties(EAllowedDOFs inAllowedDOFs, const MassProperties &inMassProperties); |
| 93 |
|
| 94 |
|
| 95 |
inline float GetInverseMass() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvMass; } |
| 96 |
inline float GetInverseMassUnchecked() const { return mInvMass; } |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
void SetInverseMass(float inInverseMass) { mInvMass = inInverseMass; } |
| 103 |
|
| 104 |
|
| 105 |
inline Vec3 GetInverseInertiaDiagonal() const { JPH_ASSERT(mCachedMotionType == EMotionType::Dynamic); return mInvInertiaDiagonal; } |
| 106 |
|
| 107 |
|
| 108 |
inline Quat GetInertiaRotation() const { return mInertiaRotation; } |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
void SetInverseInertia(Vec3Arg inDiagonal, QuatArg inRot) { mInvInertiaDiagonal = inDiagonal; mInertiaRotation = inRot; } |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
void ScaleToMass(float inMass); |
| 119 |
|
| 120 |
|
| 121 |
inline Mat44 GetLocalSpaceInverseInertia() const; |
| 122 |
|
| 123 |
|
| 124 |
inline Mat44 GetLocalSpaceInverseInertiaUnchecked() const; |
| 125 |
|
| 126 |
|
| 127 |
inline Mat44 GetInverseInertiaForRotation(Mat44Arg inRotation) const; |
| 128 |
|
| 129 |
|
| 130 |
JPH_INLINE Vec3 MultiplyWorldSpaceInverseInertiaByVector(QuatArg inBodyRotation, Vec3Arg inV) const; |
| 131 |
|
| 132 |
|
| 133 |
JPH_INLINE Vec3 GetPointVelocityCOM(Vec3Arg inPointRelativeToCOM) const { return mLinearVelocity + mAngularVelocity.Cross(inPointRelativeToCOM); } |
| 134 |
|
| 135 |
|
| 136 |
JPH_INLINE Vec3 GetAccumulatedForce() const { return Vec3::sLoadFloat3Unsafe(mForce); } |
| 137 |
|
| 138 |
|
| 139 |
JPH_INLINE Vec3 GetAccumulatedTorque() const { return Vec3::sLoadFloat3Unsafe(mTorque); } |
| 140 |
|
| 141 |
|
| 142 |
JPH_INLINE void ResetForce() { mForce = Float3(0, 0, 0); } |
| 143 |
|
| 144 |
|
| 145 |
JPH_INLINE void ResetTorque() { mTorque = Float3(0, 0, 0); } |
| 146 |
|
| 147 |
|
| 148 |
JPH_INLINE void ResetMotion() |
| 149 |
{ |
| 150 |
JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); |
| 151 |
mLinearVelocity = mAngularVelocity = Vec3::sZero(); |
| 152 |
mForce = mTorque = Float3(0, 0, 0); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
JPH_INLINE UVec4 GetLinearDOFsMask() const |
| 157 |
{ |
| 158 |
UVec4 mask(uint32(EAllowedDOFs::TranslationX), uint32(EAllowedDOFs::TranslationY), uint32(EAllowedDOFs::TranslationZ), 0); |
| 159 |
return UVec4::sEquals(UVec4::sAnd(UVec4::sReplicate(uint32(mAllowedDOFs)), mask), mask); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
JPH_INLINE Vec3 LockTranslation(Vec3Arg inV) const |
| 164 |
{ |
| 165 |
return Vec3::sAnd(inV, Vec3(GetLinearDOFsMask().ReinterpretAsFloat())); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
JPH_INLINE UVec4 GetAngularDOFsMask() const |
| 170 |
{ |
| 171 |
UVec4 mask(uint32(EAllowedDOFs::RotationX), uint32(EAllowedDOFs::RotationY), uint32(EAllowedDOFs::RotationZ), 0); |
| 172 |
return UVec4::sEquals(UVec4::sAnd(UVec4::sReplicate(uint32(mAllowedDOFs)), mask), mask); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
JPH_INLINE Vec3 LockAngular(Vec3Arg inV) const |
| 177 |
{ |
| 178 |
return Vec3::sAnd(inV, Vec3(GetAngularDOFsMask().ReinterpretAsFloat())); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
void SetNumVelocityStepsOverride(uint inN) { JPH_ASSERT(inN < 256); mNumVelocityStepsOverride = uint8(inN); } |
| 183 |
uint GetNumVelocityStepsOverride() const { return mNumVelocityStepsOverride; } |
| 184 |
|
| 185 |
|
| 186 |
void SetNumPositionStepsOverride(uint inN) { JPH_ASSERT(inN < 256); mNumPositionStepsOverride = uint8(inN); } |
| 187 |
uint GetNumPositionStepsOverride() const { return mNumPositionStepsOverride; } |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
inline void AddLinearVelocityStep(Vec3Arg inLinearVelocityChange) { JPH_DET_LOG("AddLinearVelocityStep: " << inLinearVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); mLinearVelocity = LockTranslation(mLinearVelocity + inLinearVelocityChange); JPH_ASSERT(!mLinearVelocity.IsNaN()); } |
| 196 |
inline void SubLinearVelocityStep(Vec3Arg inLinearVelocityChange) { JPH_DET_LOG("SubLinearVelocityStep: " << inLinearVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); mLinearVelocity = LockTranslation(mLinearVelocity - inLinearVelocityChange); JPH_ASSERT(!mLinearVelocity.IsNaN()); } |
| 197 |
inline void AddAngularVelocityStep(Vec3Arg inAngularVelocityChange) { JPH_DET_LOG("AddAngularVelocityStep: " << inAngularVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); mAngularVelocity += inAngularVelocityChange; JPH_ASSERT(!mAngularVelocity.IsNaN()); } |
| 198 |
inline void SubAngularVelocityStep(Vec3Arg inAngularVelocityChange) { JPH_DET_LOG("SubAngularVelocityStep: " << inAngularVelocityChange); JPH_ASSERT(BodyAccess::sCheckRights(BodyAccess::sVelocityAccess(), BodyAccess::EAccess::ReadWrite)); mAngularVelocity -= inAngularVelocityChange; JPH_ASSERT(!mAngularVelocity.IsNaN()); } |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
inline void ApplyGyroscopicForceInternal(QuatArg inBodyRotation, float inDeltaTime); |
| 203 |
|
| 204 |
|
| 205 |
inline void ApplyForceTorqueAndDragInternal(QuatArg inBodyRotation, Vec3Arg inGravity, float inDeltaTime); |
| 206 |
|
| 207 |
|
| 208 |
uint32 GetIslandIndexInternal() const { return mIslandIndex; } |
| 209 |
void SetIslandIndexInternal(uint32 inIndex) { mIslandIndex = inIndex; } |
| 210 |
|
| 211 |
|
| 212 |
uint32 GetIndexInActiveBodiesInternal() const { return mIndexInActiveBodies; } |
| 213 |
|
| 214 |
#ifdef JPH_DOUBLE_PRECISION |
| 215 |
inline DVec3 GetSleepTestOffset() const { return DVec3::sLoadDouble3Unsafe(mSleepTestOffset); } |
| 216 |
#endif |
| 217 |
|
| 218 |
|
| 219 |
inline void ResetSleepTestSpheres(const RVec3 *inPoints); |
| 220 |
|
| 221 |
|
| 222 |
inline void ResetSleepTestTimer() { mSleepTestTimer = 0.0f; } |
| 223 |
|
| 224 |
|
| 225 |
inline ECanSleep AccumulateSleepTime(float inDeltaTime, float inTimeBeforeSleep); |
| 226 |
|
| 227 |
|
| 228 |
void SaveState(StateRecorder &inStream) const; |
| 229 |
|
| 230 |
|
| 231 |
void RestoreState(StateRecorder &inStream); |
| 232 |
|
| 233 |
static constexpr uint32 cInactiveIndex = uint32(-1); |
| 234 |
|
| 235 |
private: |
| 236 |
friend class BodyManager; |
| 237 |
friend class Body; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
Vec3 mLinearVelocity { Vec3::sZero() }; |
| 242 |
Vec3 mAngularVelocity { Vec3::sZero() }; |
| 243 |
Vec3 mInvInertiaDiagonal; |
| 244 |
Quat mInertiaRotation; |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
Float3 mForce { 0, 0, 0 }; |
| 249 |
Float3 mTorque { 0, 0, 0 }; |
| 250 |
float mInvMass; |
| 251 |
float mLinearDamping; |
| 252 |
float mAngularDamping; |
| 253 |
float mMaxLinearVelocity; |
| 254 |
float mMaxAngularVelocity; |
| 255 |
float mGravityFactor; |
| 256 |
uint32 mIndexInActiveBodies = cInactiveIndex; |
| 257 |
uint32 mIslandIndex = cInactiveIndex; |
| 258 |
|
| 259 |
|
| 260 |
EMotionQuality mMotionQuality; |
| 261 |
bool mAllowSleeping; |
| 262 |
EAllowedDOFs mAllowedDOFs = EAllowedDOFs::All; |
| 263 |
uint8 mNumVelocityStepsOverride = 0; |
| 264 |
uint8 mNumPositionStepsOverride = 0; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
#ifdef JPH_DOUBLE_PRECISION |
| 269 |
Double3 mSleepTestOffset; |
| 270 |
#endif |
| 271 |
Sphere mSleepTestSpheres[3]; |
| 272 |
float mSleepTestTimer; |
| 273 |
|
| 274 |
#ifdef JPH_ENABLE_ASSERTS |
| 275 |
EBodyType mCachedBodyType; |
| 276 |
EMotionType mCachedMotionType; |
| 277 |
#endif |
| 278 |
}; |
| 279 |
|
| 280 |
JPH_NAMESPACE_END |
| 281 |
|
| 282 |
#include "MotionProperties.inl" |