| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Body/Body.h> |
| 8 |
#include <Jolt/Core/Mutex.h> |
| 9 |
#include <Jolt/Core/MutexArray.h> |
| 10 |
|
| 11 |
JPH_NAMESPACE_BEGIN |
| 12 |
|
| 13 |
|
| 14 |
class BodyCreationSettings; |
| 15 |
class SoftBodyCreationSettings; |
| 16 |
class BodyActivationListener; |
| 17 |
class StateRecorderFilter; |
| 18 |
struct PhysicsSettings; |
| 19 |
#ifdef JPH_DEBUG_RENDERER |
| 20 |
class DebugRenderer; |
| 21 |
class BodyDrawFilter; |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifdef JPH_DEBUG_RENDERER |
| 25 |
|
| 26 |
|
| 27 |
enum class ESoftBodyConstraintColor |
| 28 |
{ |
| 29 |
ConstraintType, |
| 30 |
ConstraintGroup, |
| 31 |
ConstraintOrder, |
| 32 |
}; |
| 33 |
|
| 34 |
#endif |
| 35 |
|
| 36 |
|
| 37 |
using BodyVector = Array<Body *>; |
| 38 |
|
| 39 |
|
| 40 |
using BodyIDVector = Array<BodyID>; |
| 41 |
|
| 42 |
|
| 43 |
class JPH_EXPORT BodyManager : public NonCopyable |
| 44 |
{ |
| 45 |
public: |
| 46 |
JPH_OVERRIDE_NEW_DELETE |
| 47 |
|
| 48 |
|
| 49 |
~BodyManager(); |
| 50 |
|
| 51 |
|
| 52 |
void Init(uint inMaxBodies, uint inNumBodyMutexes, const BroadPhaseLayerInterface &inLayerInterface); |
| 53 |
|
| 54 |
|
| 55 |
uint GetNumBodies() const; |
| 56 |
|
| 57 |
|
| 58 |
uint GetMaxBodies() const { return uint(mBodies.capacity()); } |
| 59 |
|
| 60 |
|
| 61 |
struct BodyStats |
| 62 |
{ |
| 63 |
uint mNumBodies = 0; |
| 64 |
uint mMaxBodies = 0; |
| 65 |
|
| 66 |
uint mNumBodiesStatic = 0; |
| 67 |
|
| 68 |
uint mNumBodiesDynamic = 0; |
| 69 |
uint mNumActiveBodiesDynamic = 0; |
| 70 |
|
| 71 |
uint mNumBodiesKinematic = 0; |
| 72 |
uint mNumActiveBodiesKinematic = 0; |
| 73 |
|
| 74 |
uint mNumSoftBodies = 0; |
| 75 |
uint mNumActiveSoftBodies = 0; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
BodyStats GetBodyStats() const; |
| 80 |
|
| 81 |
|
| 82 |
Body * AllocateBody(const BodyCreationSettings &inBodyCreationSettings) const; |
| 83 |
|
| 84 |
|
| 85 |
Body * AllocateSoftBody(const SoftBodyCreationSettings &inSoftBodyCreationSettings) const; |
| 86 |
|
| 87 |
|
| 88 |
void FreeBody(Body *inBody) const; |
| 89 |
|
| 90 |
|
| 91 |
bool AddBody(Body *ioBody); |
| 92 |
|
| 93 |
|
| 94 |
bool AddBodyWithCustomID(Body *ioBody, const BodyID &inBodyID); |
| 95 |
|
| 96 |
|
| 97 |
void RemoveBodies(const BodyID *inBodyIDs, int inNumber, Body **outBodies); |
| 98 |
|
| 99 |
|
| 100 |
void DestroyBodies(const BodyID *inBodyIDs, int inNumber); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
void ActivateBodies(const BodyID *inBodyIDs, int inNumber); |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
void DeactivateBodies(const BodyID *inBodyIDs, int inNumber); |
| 109 |
|
| 110 |
|
| 111 |
void SetMotionQuality(Body &ioBody, EMotionQuality inMotionQuality); |
| 112 |
|
| 113 |
|
| 114 |
void GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const; |
| 115 |
|
| 116 |
|
| 117 |
const BodyID * GetActiveBodiesUnsafe(EBodyType inType) const { return mActiveBodies[int(inType)]; } |
| 118 |
|
| 119 |
|
| 120 |
uint32 GetNumActiveBodies(EBodyType inType) const { return mNumActiveBodies[int(inType)].load(memory_order_acquire); } |
| 121 |
|
| 122 |
|
| 123 |
uint32 GetNumActiveCCDBodies() const { return mNumActiveCCDBodies; } |
| 124 |
|
| 125 |
|
| 126 |
void SetBodyActivationListener(BodyActivationListener *inListener); |
| 127 |
BodyActivationListener * GetBodyActivationListener() const { return mActivationListener; } |
| 128 |
|
| 129 |
|
| 130 |
static inline bool sIsValidBodyPointer(const Body *inBody) { return (uintptr_t(inBody) & cIsFreedBody) == 0; } |
| 131 |
|
| 132 |
|
| 133 |
const BodyVector & GetBodies() const { return mBodies; } |
| 134 |
|
| 135 |
|
| 136 |
BodyVector & GetBodies() { return mBodies; } |
| 137 |
|
| 138 |
|
| 139 |
void GetBodyIDs(BodyIDVector &outBodies) const; |
| 140 |
|
| 141 |
|
| 142 |
const Body & GetBody(const BodyID &inID) const { return *mBodies[inID.GetIndex()]; } |
| 143 |
|
| 144 |
|
| 145 |
Body & GetBody(const BodyID &inID) { return *mBodies[inID.GetIndex()]; } |
| 146 |
|
| 147 |
|
| 148 |
const Body * TryGetBody(const BodyID &inID) const |
| 149 |
{ |
| 150 |
uint32 idx = inID.GetIndex(); |
| 151 |
if (idx >= mBodies.size()) |
| 152 |
return nullptr; |
| 153 |
|
| 154 |
const Body *body = mBodies[idx]; |
| 155 |
if (sIsValidBodyPointer(body) && body->GetID() == inID) |
| 156 |
return body; |
| 157 |
|
| 158 |
return nullptr; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
Body * TryGetBody(const BodyID &inID) |
| 163 |
{ |
| 164 |
uint32 idx = inID.GetIndex(); |
| 165 |
if (idx >= mBodies.size()) |
| 166 |
return nullptr; |
| 167 |
|
| 168 |
Body *body = mBodies[idx]; |
| 169 |
if (sIsValidBodyPointer(body) && body->GetID() == inID) |
| 170 |
return body; |
| 171 |
|
| 172 |
return nullptr; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
SharedMutex & GetMutexForBody(const BodyID &inID) const { return mBodyMutexes.GetMutexByObjectIndex(inID.GetIndex()); } |
| 177 |
|
| 178 |
|
| 179 |
using MutexMask = uint64; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
MutexMask GetAllBodiesMutexMask() const { return mBodyMutexes.GetNumMutexes() == sizeof(MutexMask) * 8? ~MutexMask(0) : (MutexMask(1) << mBodyMutexes.GetNumMutexes()) - 1; } |
| 184 |
MutexMask GetMutexMask(const BodyID *inBodies, int inNumber) const; |
| 185 |
void LockRead(MutexMask inMutexMask) const; |
| 186 |
void UnlockRead(MutexMask inMutexMask) const; |
| 187 |
void LockWrite(MutexMask inMutexMask) const; |
| 188 |
void UnlockWrite(MutexMask inMutexMask) const; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
void LockAllBodies() const; |
| 193 |
|
| 194 |
|
| 195 |
void UnlockAllBodies() const; |
| 196 |
|
| 197 |
|
| 198 |
inline void SetBodyObjectLayerInternal(Body &ioBody, ObjectLayer inLayer) const { ioBody.mObjectLayer = inLayer; ioBody.mBroadPhaseLayer = mBroadPhaseLayerInterface->GetBroadPhaseLayer(inLayer); } |
| 199 |
|
| 200 |
|
| 201 |
void InvalidateContactCacheForBody(Body &ioBody); |
| 202 |
|
| 203 |
|
| 204 |
void ValidateContactCacheForAllBodies(); |
| 205 |
|
| 206 |
|
| 207 |
void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const; |
| 208 |
|
| 209 |
|
| 210 |
bool RestoreState(StateRecorder &inStream); |
| 211 |
|
| 212 |
|
| 213 |
void SaveBodyState(const Body &inBody, StateRecorder &inStream) const; |
| 214 |
|
| 215 |
|
| 216 |
void RestoreBodyState(Body &inBody, StateRecorder &inStream); |
| 217 |
|
| 218 |
#ifdef JPH_DEBUG_RENDERER |
| 219 |
enum class EShapeColor |
| 220 |
{ |
| 221 |
InstanceColor, |
| 222 |
ShapeTypeColor, |
| 223 |
MotionTypeColor, |
| 224 |
SleepColor, |
| 225 |
IslandColor, |
| 226 |
MaterialColor, |
| 227 |
}; |
| 228 |
|
| 229 |
|
| 230 |
struct DrawSettings |
| 231 |
{ |
| 232 |
bool mDrawGetSupportFunction = false; |
| 233 |
bool mDrawSupportDirection = false; |
| 234 |
bool mDrawGetSupportingFace = false; |
| 235 |
bool mDrawShape = true; |
| 236 |
bool mDrawShapeWireframe = false; |
| 237 |
EShapeColor mDrawShapeColor = EShapeColor::MotionTypeColor; |
| 238 |
bool mDrawBoundingBox = false; |
| 239 |
bool mDrawCenterOfMassTransform = false; |
| 240 |
bool mDrawWorldTransform = false; |
| 241 |
bool mDrawVelocity = false; |
| 242 |
bool mDrawMassAndInertia = false; |
| 243 |
bool mDrawSleepStats = false; |
| 244 |
bool mDrawSoftBodyVertices = false; |
| 245 |
bool mDrawSoftBodyVertexVelocities = false; |
| 246 |
bool mDrawSoftBodyEdgeConstraints = false; |
| 247 |
bool mDrawSoftBodyBendConstraints = false; |
| 248 |
bool mDrawSoftBodyVolumeConstraints = false; |
| 249 |
bool mDrawSoftBodySkinConstraints = false; |
| 250 |
bool mDrawSoftBodyLRAConstraints = false; |
| 251 |
bool mDrawSoftBodyRods = false; |
| 252 |
bool mDrawSoftBodyRodStates = false; |
| 253 |
bool mDrawSoftBodyRodBendTwistConstraints = false; |
| 254 |
bool mDrawSoftBodyPredictedBounds = false; |
| 255 |
ESoftBodyConstraintColor mDrawSoftBodyConstraintColor = ESoftBodyConstraintColor::ConstraintType; |
| 256 |
}; |
| 257 |
|
| 258 |
|
| 259 |
void Draw(const DrawSettings &inSettings, const PhysicsSettings &inPhysicsSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter = nullptr); |
| 260 |
#endif |
| 261 |
|
| 262 |
#ifdef JPH_ENABLE_ASSERTS |
| 263 |
|
| 264 |
void SetActiveBodiesLocked(bool inLocked) { mActiveBodiesLocked = inLocked; } |
| 265 |
|
| 266 |
|
| 267 |
class GrantActiveBodiesAccess |
| 268 |
{ |
| 269 |
public: |
| 270 |
inline GrantActiveBodiesAccess(bool inAllowActivation, bool inAllowDeactivation) |
| 271 |
{ |
| 272 |
JPH_ASSERT(!sGetOverrideAllowActivation()); |
| 273 |
sSetOverrideAllowActivation(inAllowActivation); |
| 274 |
|
| 275 |
JPH_ASSERT(!sGetOverrideAllowDeactivation()); |
| 276 |
sSetOverrideAllowDeactivation(inAllowDeactivation); |
| 277 |
} |
| 278 |
|
| 279 |
inline ~GrantActiveBodiesAccess() |
| 280 |
{ |
| 281 |
sSetOverrideAllowActivation(false); |
| 282 |
sSetOverrideAllowDeactivation(false); |
| 283 |
} |
| 284 |
}; |
| 285 |
#endif |
| 286 |
|
| 287 |
#ifdef JPH_DEBUG |
| 288 |
|
| 289 |
void ValidateActiveBodyBounds(); |
| 290 |
#endif |
| 291 |
|
| 292 |
private: |
| 293 |
|
| 294 |
#ifdef JPH_COMPILER_CLANG |
| 295 |
__attribute__((no_sanitize("implicit-conversion"))) |
| 296 |
#endif |
| 297 |
inline uint8 GetNextSequenceNumber(int inBodyIndex) { return ++mBodySequenceNumbers[inBodyIndex]; } |
| 298 |
|
| 299 |
|
| 300 |
inline void AddBodyToActiveBodies(Body &ioBody); |
| 301 |
|
| 302 |
|
| 303 |
inline void RemoveBodyFromActiveBodies(Body &ioBody); |
| 304 |
|
| 305 |
|
| 306 |
JPH_INLINE Body * RemoveBodyInternal(const BodyID &inBodyID); |
| 307 |
|
| 308 |
|
| 309 |
inline static void sDeleteBody(Body *inBody); |
| 310 |
|
| 311 |
#if defined(JPH_DEBUG) && defined(JPH_ENABLE_ASSERTS) |
| 312 |
|
| 313 |
void ValidateFreeList() const; |
| 314 |
#endif |
| 315 |
|
| 316 |
|
| 317 |
BodyVector mBodies; |
| 318 |
|
| 319 |
|
| 320 |
uint mNumBodies = 0; |
| 321 |
|
| 322 |
|
| 323 |
static constexpr uintptr_t cBodyIDFreeListEnd = ~uintptr_t(0); |
| 324 |
|
| 325 |
|
| 326 |
static constexpr uintptr_t cIsFreedBody = uintptr_t(1); |
| 327 |
|
| 328 |
|
| 329 |
static constexpr uint cFreedBodyIndexShift = 1; |
| 330 |
|
| 331 |
|
| 332 |
uintptr_t mBodyIDFreeListStart = cBodyIDFreeListEnd; |
| 333 |
|
| 334 |
|
| 335 |
mutable Mutex mBodiesMutex; |
| 336 |
|
| 337 |
|
| 338 |
using BodyMutexes = MutexArray<SharedMutex>; |
| 339 |
mutable BodyMutexes mBodyMutexes; |
| 340 |
|
| 341 |
|
| 342 |
Array<uint8> mBodySequenceNumbers; |
| 343 |
|
| 344 |
|
| 345 |
mutable Mutex mActiveBodiesMutex; |
| 346 |
|
| 347 |
|
| 348 |
BodyID * mActiveBodies[cBodyTypeCount] = { }; |
| 349 |
|
| 350 |
|
| 351 |
atomic<uint32> mNumActiveBodies[cBodyTypeCount] = { }; |
| 352 |
|
| 353 |
|
| 354 |
uint32 mNumActiveCCDBodies = 0; |
| 355 |
|
| 356 |
|
| 357 |
mutable Mutex mBodiesCacheInvalidMutex; |
| 358 |
|
| 359 |
|
| 360 |
BodyIDVector mBodiesCacheInvalid; |
| 361 |
|
| 362 |
|
| 363 |
BodyActivationListener * mActivationListener = nullptr; |
| 364 |
|
| 365 |
|
| 366 |
const BroadPhaseLayerInterface *mBroadPhaseLayerInterface = nullptr; |
| 367 |
|
| 368 |
#ifdef JPH_ENABLE_ASSERTS |
| 369 |
static bool sGetOverrideAllowActivation(); |
| 370 |
static void sSetOverrideAllowActivation(bool inValue); |
| 371 |
|
| 372 |
static bool sGetOverrideAllowDeactivation(); |
| 373 |
static void sSetOverrideAllowDeactivation(bool inValue); |
| 374 |
|
| 375 |
|
| 376 |
bool mActiveBodiesLocked = false; |
| 377 |
#endif |
| 378 |
}; |
| 379 |
|
| 380 |
JPH_NAMESPACE_END |