BodyManager.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/PhysicsSettings.h>
  6. #include <Jolt/Physics/Body/BodyManager.h>
  7. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  8. #include <Jolt/Physics/Body/BodyLock.h>
  9. #include <Jolt/Physics/Body/BodyActivationListener.h>
  10. #include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
  11. #include <Jolt/Physics/SoftBody/SoftBodyCreationSettings.h>
  12. #include <Jolt/Physics/SoftBody/SoftBodyShape.h>
  13. #include <Jolt/Physics/StateRecorder.h>
  14. #include <Jolt/Core/StringTools.h>
  15. #include <Jolt/Core/QuickSort.h>
  16. #ifdef JPH_DEBUG_RENDERER
  17. #include <Jolt/Renderer/DebugRenderer.h>
  18. #include <Jolt/Physics/Body/BodyFilter.h>
  19. #endif // JPH_DEBUG_RENDERER
  20. JPH_NAMESPACE_BEGIN
  21. #ifdef JPH_ENABLE_ASSERTS
  22. static thread_local bool sOverrideAllowActivation = false;
  23. static thread_local bool sOverrideAllowDeactivation = false;
  24. bool BodyManager::sGetOverrideAllowActivation()
  25. {
  26. return sOverrideAllowActivation;
  27. }
  28. void BodyManager::sSetOverrideAllowActivation(bool inValue)
  29. {
  30. sOverrideAllowActivation = inValue;
  31. }
  32. bool BodyManager::sGetOverrideAllowDeactivation()
  33. {
  34. return sOverrideAllowDeactivation;
  35. }
  36. void BodyManager::sSetOverrideAllowDeactivation(bool inValue)
  37. {
  38. sOverrideAllowDeactivation = inValue;
  39. }
  40. #endif
  41. // Helper class that combines a body and its motion properties
  42. class BodyWithMotionProperties : public Body
  43. {
  44. public:
  45. JPH_OVERRIDE_NEW_DELETE
  46. MotionProperties mMotionProperties;
  47. };
  48. // Helper class that combines a soft body its motion properties and shape
  49. class SoftBodyWithMotionPropertiesAndShape : public Body
  50. {
  51. public:
  52. SoftBodyWithMotionPropertiesAndShape()
  53. {
  54. mShape.SetEmbedded();
  55. }
  56. SoftBodyMotionProperties mMotionProperties;
  57. SoftBodyShape mShape;
  58. };
  59. inline void BodyManager::sDeleteBody(Body *inBody)
  60. {
  61. if (inBody->mMotionProperties != nullptr)
  62. {
  63. JPH_IF_ENABLE_ASSERTS(inBody->mMotionProperties = nullptr;)
  64. if (inBody->IsSoftBody())
  65. {
  66. inBody->mShape = nullptr; // Release the shape to avoid assertion on shape destruction because of embedded object with refcount > 0
  67. delete static_cast<SoftBodyWithMotionPropertiesAndShape *>(inBody);
  68. }
  69. else
  70. delete static_cast<BodyWithMotionProperties *>(inBody);
  71. }
  72. else
  73. delete inBody;
  74. }
  75. BodyManager::~BodyManager()
  76. {
  77. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  78. // Destroy any bodies that are still alive
  79. for (Body *b : mBodies)
  80. if (sIsValidBodyPointer(b))
  81. sDeleteBody(b);
  82. for (BodyID *active_bodies : mActiveBodies)
  83. delete [] active_bodies;
  84. }
  85. void BodyManager::Init(uint inMaxBodies, uint inNumBodyMutexes, const BroadPhaseLayerInterface &inLayerInterface)
  86. {
  87. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  88. // Num body mutexes must be a power of two and not bigger than our MutexMask
  89. uint num_body_mutexes = Clamp<uint>(GetNextPowerOf2(inNumBodyMutexes == 0? 2 * thread::hardware_concurrency() : inNumBodyMutexes), 1, sizeof(MutexMask) * 8);
  90. // Allocate the body mutexes
  91. mBodyMutexes.Init(num_body_mutexes);
  92. // Allocate space for bodies
  93. mBodies.reserve(inMaxBodies);
  94. // Allocate space for active bodies
  95. for (BodyID *&active_bodies : mActiveBodies)
  96. {
  97. JPH_ASSERT(active_bodies == nullptr);
  98. active_bodies = new BodyID [inMaxBodies];
  99. }
  100. // Allocate space for sequence numbers
  101. mBodySequenceNumbers.resize(inMaxBodies);
  102. // Keep layer interface
  103. mBroadPhaseLayerInterface = &inLayerInterface;
  104. }
  105. uint BodyManager::GetNumBodies() const
  106. {
  107. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  108. return mNumBodies;
  109. }
  110. BodyManager::BodyStats BodyManager::GetBodyStats() const
  111. {
  112. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  113. BodyStats stats;
  114. stats.mNumBodies = mNumBodies;
  115. stats.mMaxBodies = uint(mBodies.capacity());
  116. for (const Body *body : mBodies)
  117. if (sIsValidBodyPointer(body))
  118. {
  119. if (body->IsSoftBody())
  120. {
  121. stats.mNumSoftBodies++;
  122. if (body->IsActive())
  123. stats.mNumActiveSoftBodies++;
  124. }
  125. else
  126. {
  127. switch (body->GetMotionType())
  128. {
  129. case EMotionType::Static:
  130. stats.mNumBodiesStatic++;
  131. break;
  132. case EMotionType::Dynamic:
  133. stats.mNumBodiesDynamic++;
  134. if (body->IsActive())
  135. stats.mNumActiveBodiesDynamic++;
  136. break;
  137. case EMotionType::Kinematic:
  138. stats.mNumBodiesKinematic++;
  139. if (body->IsActive())
  140. stats.mNumActiveBodiesKinematic++;
  141. break;
  142. }
  143. }
  144. }
  145. return stats;
  146. }
  147. Body *BodyManager::AllocateBody(const BodyCreationSettings &inBodyCreationSettings) const
  148. {
  149. // Fill in basic properties
  150. Body *body;
  151. if (inBodyCreationSettings.HasMassProperties())
  152. {
  153. BodyWithMotionProperties *bmp = new BodyWithMotionProperties;
  154. body = bmp;
  155. body->mMotionProperties = &bmp->mMotionProperties;
  156. }
  157. else
  158. {
  159. body = new Body;
  160. }
  161. body->mBodyType = EBodyType::RigidBody;
  162. body->mShape = inBodyCreationSettings.GetShape();
  163. body->mUserData = inBodyCreationSettings.mUserData;
  164. body->SetFriction(inBodyCreationSettings.mFriction);
  165. body->SetRestitution(inBodyCreationSettings.mRestitution);
  166. body->mMotionType = inBodyCreationSettings.mMotionType;
  167. if (inBodyCreationSettings.mIsSensor)
  168. body->SetIsSensor(true);
  169. if (inBodyCreationSettings.mSensorDetectsStatic)
  170. body->SetSensorDetectsStatic(true);
  171. if (inBodyCreationSettings.mUseManifoldReduction)
  172. body->SetUseManifoldReduction(true);
  173. if (inBodyCreationSettings.mApplyGyroscopicForce)
  174. body->SetApplyGyroscopicForce(true);
  175. SetBodyObjectLayerInternal(*body, inBodyCreationSettings.mObjectLayer);
  176. body->mObjectLayer = inBodyCreationSettings.mObjectLayer;
  177. body->mCollisionGroup = inBodyCreationSettings.mCollisionGroup;
  178. if (inBodyCreationSettings.HasMassProperties())
  179. {
  180. MotionProperties *mp = body->mMotionProperties;
  181. mp->SetLinearDamping(inBodyCreationSettings.mLinearDamping);
  182. mp->SetAngularDamping(inBodyCreationSettings.mAngularDamping);
  183. mp->SetMaxLinearVelocity(inBodyCreationSettings.mMaxLinearVelocity);
  184. mp->SetMaxAngularVelocity(inBodyCreationSettings.mMaxAngularVelocity);
  185. mp->SetLinearVelocity(inBodyCreationSettings.mLinearVelocity); // Needs to happen after setting the max linear/angular velocity
  186. mp->SetAngularVelocity(inBodyCreationSettings.mAngularVelocity);
  187. mp->SetGravityFactor(inBodyCreationSettings.mGravityFactor);
  188. mp->SetNumVelocityStepsOverride(inBodyCreationSettings.mNumVelocityStepsOverride);
  189. mp->SetNumPositionStepsOverride(inBodyCreationSettings.mNumPositionStepsOverride);
  190. mp->mMotionQuality = inBodyCreationSettings.mMotionQuality;
  191. mp->mAllowSleeping = inBodyCreationSettings.mAllowSleeping;
  192. JPH_IF_ENABLE_ASSERTS(mp->mCachedBodyType = body->mBodyType;)
  193. JPH_IF_ENABLE_ASSERTS(mp->mCachedMotionType = body->mMotionType;)
  194. mp->SetMassProperties(inBodyCreationSettings.mAllowedDOFs, inBodyCreationSettings.GetMassProperties());
  195. }
  196. // Position body
  197. body->SetPositionAndRotationInternal(inBodyCreationSettings.mPosition, inBodyCreationSettings.mRotation);
  198. return body;
  199. }
  200. /// Create a soft body using creation settings. The returned body will not be part of the body manager yet.
  201. Body *BodyManager::AllocateSoftBody(const SoftBodyCreationSettings &inSoftBodyCreationSettings) const
  202. {
  203. // Fill in basic properties
  204. SoftBodyWithMotionPropertiesAndShape *bmp = new SoftBodyWithMotionPropertiesAndShape;
  205. SoftBodyMotionProperties *mp = &bmp->mMotionProperties;
  206. SoftBodyShape *shape = &bmp->mShape;
  207. Body *body = bmp;
  208. shape->mSoftBodyMotionProperties = mp;
  209. body->mBodyType = EBodyType::SoftBody;
  210. body->mMotionProperties = mp;
  211. body->mShape = shape;
  212. body->mUserData = inSoftBodyCreationSettings.mUserData;
  213. body->SetFriction(inSoftBodyCreationSettings.mFriction);
  214. body->SetRestitution(inSoftBodyCreationSettings.mRestitution);
  215. body->mMotionType = EMotionType::Dynamic;
  216. SetBodyObjectLayerInternal(*body, inSoftBodyCreationSettings.mObjectLayer);
  217. body->mObjectLayer = inSoftBodyCreationSettings.mObjectLayer;
  218. body->mCollisionGroup = inSoftBodyCreationSettings.mCollisionGroup;
  219. mp->SetLinearDamping(inSoftBodyCreationSettings.mLinearDamping);
  220. mp->SetAngularDamping(0);
  221. mp->SetMaxLinearVelocity(inSoftBodyCreationSettings.mMaxLinearVelocity);
  222. mp->SetMaxAngularVelocity(FLT_MAX);
  223. mp->SetLinearVelocity(Vec3::sZero());
  224. mp->SetAngularVelocity(Vec3::sZero());
  225. mp->SetGravityFactor(inSoftBodyCreationSettings.mGravityFactor);
  226. mp->mMotionQuality = EMotionQuality::Discrete;
  227. mp->mAllowSleeping = inSoftBodyCreationSettings.mAllowSleeping;
  228. JPH_IF_ENABLE_ASSERTS(mp->mCachedBodyType = body->mBodyType;)
  229. JPH_IF_ENABLE_ASSERTS(mp->mCachedMotionType = body->mMotionType;)
  230. mp->Initialize(inSoftBodyCreationSettings);
  231. body->SetPositionAndRotationInternal(inSoftBodyCreationSettings.mPosition, inSoftBodyCreationSettings.mMakeRotationIdentity? Quat::sIdentity() : inSoftBodyCreationSettings.mRotation);
  232. return body;
  233. }
  234. void BodyManager::FreeBody(Body *inBody) const
  235. {
  236. JPH_ASSERT(inBody->GetID().IsInvalid(), "This function should only be called on a body that doesn't have an ID yet, use DestroyBody otherwise");
  237. sDeleteBody(inBody);
  238. }
  239. bool BodyManager::AddBody(Body *ioBody)
  240. {
  241. // Return error when body was already added
  242. if (!ioBody->GetID().IsInvalid())
  243. return false;
  244. // Determine next free index
  245. uint32 idx;
  246. {
  247. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  248. if (mBodyIDFreeListStart != cBodyIDFreeListEnd)
  249. {
  250. // Pop an item from the freelist
  251. JPH_ASSERT(mBodyIDFreeListStart & cIsFreedBody);
  252. idx = uint32(mBodyIDFreeListStart >> cFreedBodyIndexShift);
  253. JPH_ASSERT(!sIsValidBodyPointer(mBodies[idx]));
  254. mBodyIDFreeListStart = uintptr_t(mBodies[idx]);
  255. mBodies[idx] = ioBody;
  256. }
  257. else
  258. {
  259. if (mBodies.size() < mBodies.capacity())
  260. {
  261. // Allocate a new entry, note that the array should not actually resize since we've reserved it at init time
  262. idx = uint32(mBodies.size());
  263. mBodies.push_back(ioBody);
  264. }
  265. else
  266. {
  267. // Out of bodies
  268. return false;
  269. }
  270. }
  271. // Update cached number of bodies
  272. mNumBodies++;
  273. }
  274. // Get next sequence number and assign the ID
  275. uint8 seq_no = GetNextSequenceNumber(idx);
  276. ioBody->mID = BodyID(idx, seq_no);
  277. return true;
  278. }
  279. bool BodyManager::AddBodyWithCustomID(Body *ioBody, const BodyID &inBodyID)
  280. {
  281. // Return error when body was already added
  282. if (!ioBody->GetID().IsInvalid())
  283. return false;
  284. {
  285. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  286. // Check if index is beyond the max body ID
  287. uint32 idx = inBodyID.GetIndex();
  288. if (idx >= mBodies.capacity())
  289. return false; // Return error
  290. if (idx < mBodies.size())
  291. {
  292. // Body array entry has already been allocated, check if there's a free body here
  293. if (sIsValidBodyPointer(mBodies[idx]))
  294. return false; // Return error
  295. // Remove the entry from the freelist
  296. uintptr_t idx_start = mBodyIDFreeListStart >> cFreedBodyIndexShift;
  297. if (idx == idx_start)
  298. {
  299. // First entry, easy to remove, the start of the list is our next
  300. mBodyIDFreeListStart = uintptr_t(mBodies[idx]);
  301. }
  302. else
  303. {
  304. // Loop over the freelist and find the entry in the freelist pointing to our index
  305. // TODO: This is O(N), see if this becomes a performance problem (don't want to put the freed bodies in a double linked list)
  306. uintptr_t cur, next;
  307. for (cur = idx_start; cur != cBodyIDFreeListEnd >> cFreedBodyIndexShift; cur = next)
  308. {
  309. next = uintptr_t(mBodies[cur]) >> cFreedBodyIndexShift;
  310. if (next == idx)
  311. {
  312. mBodies[cur] = mBodies[idx];
  313. break;
  314. }
  315. }
  316. JPH_ASSERT(cur != cBodyIDFreeListEnd >> cFreedBodyIndexShift);
  317. }
  318. // Put the body in the slot
  319. mBodies[idx] = ioBody;
  320. }
  321. else
  322. {
  323. // Ensure that all body IDs up to this body ID have been allocated and added to the free list
  324. while (idx > mBodies.size())
  325. {
  326. // Push the id onto the freelist
  327. mBodies.push_back((Body *)mBodyIDFreeListStart);
  328. mBodyIDFreeListStart = (uintptr_t(mBodies.size() - 1) << cFreedBodyIndexShift) | cIsFreedBody;
  329. }
  330. // Add the element to the list
  331. mBodies.push_back(ioBody);
  332. }
  333. // Update cached number of bodies
  334. mNumBodies++;
  335. }
  336. // Assign the ID
  337. ioBody->mID = inBodyID;
  338. return true;
  339. }
  340. Body *BodyManager::RemoveBodyInternal(const BodyID &inBodyID)
  341. {
  342. // Get body
  343. uint32 idx = inBodyID.GetIndex();
  344. Body *body = mBodies[idx];
  345. // Validate that it can be removed
  346. JPH_ASSERT(body->GetID() == inBodyID);
  347. JPH_ASSERT(!body->IsActive());
  348. JPH_ASSERT(!body->IsInBroadPhase());
  349. // Push the id onto the freelist
  350. mBodies[idx] = (Body *)mBodyIDFreeListStart;
  351. mBodyIDFreeListStart = (uintptr_t(idx) << cFreedBodyIndexShift) | cIsFreedBody;
  352. return body;
  353. }
  354. #if defined(_DEBUG) && defined(JPH_ENABLE_ASSERTS)
  355. void BodyManager::ValidateFreeList() const
  356. {
  357. // Check that the freelist is correct
  358. size_t num_freed = 0;
  359. for (uintptr_t start = mBodyIDFreeListStart; start != cBodyIDFreeListEnd; start = uintptr_t(mBodies[start >> cFreedBodyIndexShift]))
  360. {
  361. JPH_ASSERT(start & cIsFreedBody);
  362. num_freed++;
  363. }
  364. JPH_ASSERT(mNumBodies == mBodies.size() - num_freed);
  365. }
  366. #endif // defined(_DEBUG) && _defined(JPH_ENABLE_ASSERTS)
  367. void BodyManager::RemoveBodies(const BodyID *inBodyIDs, int inNumber, Body **outBodies)
  368. {
  369. // Don't take lock if no bodies are to be destroyed
  370. if (inNumber <= 0)
  371. return;
  372. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  373. // Update cached number of bodies
  374. JPH_ASSERT(mNumBodies >= (uint)inNumber);
  375. mNumBodies -= inNumber;
  376. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  377. {
  378. // Remove body
  379. Body *body = RemoveBodyInternal(*b);
  380. // Clear the ID
  381. body->mID = BodyID();
  382. // Return the body to the caller
  383. if (outBodies != nullptr)
  384. {
  385. *outBodies = body;
  386. ++outBodies;
  387. }
  388. }
  389. #if defined(_DEBUG) && defined(JPH_ENABLE_ASSERTS)
  390. ValidateFreeList();
  391. #endif // defined(_DEBUG) && _defined(JPH_ENABLE_ASSERTS)
  392. }
  393. void BodyManager::DestroyBodies(const BodyID *inBodyIDs, int inNumber)
  394. {
  395. // Don't take lock if no bodies are to be destroyed
  396. if (inNumber <= 0)
  397. return;
  398. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  399. // Update cached number of bodies
  400. JPH_ASSERT(mNumBodies >= (uint)inNumber);
  401. mNumBodies -= inNumber;
  402. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  403. {
  404. // Remove body
  405. Body *body = RemoveBodyInternal(*b);
  406. // Free the body
  407. sDeleteBody(body);
  408. }
  409. #if defined(_DEBUG) && defined(JPH_ENABLE_ASSERTS)
  410. ValidateFreeList();
  411. #endif // defined(_DEBUG) && _defined(JPH_ENABLE_ASSERTS)
  412. }
  413. void BodyManager::AddBodyToActiveBodies(Body &ioBody)
  414. {
  415. // Select the correct array to use
  416. int type = (int)ioBody.GetBodyType();
  417. atomic<uint32> &num_active_bodies = mNumActiveBodies[type];
  418. BodyID *active_bodies = mActiveBodies[type];
  419. MotionProperties *mp = ioBody.mMotionProperties;
  420. mp->mIndexInActiveBodies = num_active_bodies;
  421. JPH_ASSERT(num_active_bodies < GetMaxBodies());
  422. active_bodies[num_active_bodies] = ioBody.GetID();
  423. num_active_bodies++; // Increment atomic after setting the body ID so that PhysicsSystem::JobFindCollisions (which doesn't lock the mActiveBodiesMutex) will only read valid IDs
  424. // Count CCD bodies
  425. if (mp->GetMotionQuality() == EMotionQuality::LinearCast)
  426. mNumActiveCCDBodies++;
  427. }
  428. void BodyManager::RemoveBodyFromActiveBodies(Body &ioBody)
  429. {
  430. // Select the correct array to use
  431. int type = (int)ioBody.GetBodyType();
  432. atomic<uint32> &num_active_bodies = mNumActiveBodies[type];
  433. BodyID *active_bodies = mActiveBodies[type];
  434. uint32 last_body_index = num_active_bodies - 1;
  435. MotionProperties *mp = ioBody.mMotionProperties;
  436. if (mp->mIndexInActiveBodies != last_body_index)
  437. {
  438. // This is not the last body, use the last body to fill the hole
  439. BodyID last_body_id = active_bodies[last_body_index];
  440. active_bodies[mp->mIndexInActiveBodies] = last_body_id;
  441. // Update that body's index in the active list
  442. Body &last_body = *mBodies[last_body_id.GetIndex()];
  443. JPH_ASSERT(last_body.mMotionProperties->mIndexInActiveBodies == last_body_index);
  444. last_body.mMotionProperties->mIndexInActiveBodies = mp->mIndexInActiveBodies;
  445. }
  446. // Mark this body as no longer active
  447. mp->mIndexInActiveBodies = Body::cInactiveIndex;
  448. // Remove unused element from active bodies list
  449. --num_active_bodies;
  450. // Count CCD bodies
  451. if (mp->GetMotionQuality() == EMotionQuality::LinearCast)
  452. mNumActiveCCDBodies--;
  453. }
  454. void BodyManager::ActivateBodies(const BodyID *inBodyIDs, int inNumber)
  455. {
  456. // Don't take lock if no bodies are to be activated
  457. if (inNumber <= 0)
  458. return;
  459. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  460. JPH_ASSERT(!mActiveBodiesLocked || sOverrideAllowActivation);
  461. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  462. if (!b->IsInvalid())
  463. {
  464. BodyID body_id = *b;
  465. Body &body = *mBodies[body_id.GetIndex()];
  466. JPH_ASSERT(body.GetID() == body_id);
  467. JPH_ASSERT(body.IsInBroadPhase());
  468. if (!body.IsStatic()
  469. && body.mMotionProperties->mIndexInActiveBodies == Body::cInactiveIndex)
  470. {
  471. // Reset sleeping
  472. body.ResetSleepTimer();
  473. AddBodyToActiveBodies(body);
  474. // Call activation listener
  475. if (mActivationListener != nullptr)
  476. mActivationListener->OnBodyActivated(body_id, body.GetUserData());
  477. }
  478. }
  479. }
  480. void BodyManager::DeactivateBodies(const BodyID *inBodyIDs, int inNumber)
  481. {
  482. // Don't take lock if no bodies are to be deactivated
  483. if (inNumber <= 0)
  484. return;
  485. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  486. JPH_ASSERT(!mActiveBodiesLocked || sOverrideAllowDeactivation);
  487. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  488. if (!b->IsInvalid())
  489. {
  490. BodyID body_id = *b;
  491. Body &body = *mBodies[body_id.GetIndex()];
  492. JPH_ASSERT(body.GetID() == body_id);
  493. JPH_ASSERT(body.IsInBroadPhase());
  494. if (body.mMotionProperties != nullptr
  495. && body.mMotionProperties->mIndexInActiveBodies != Body::cInactiveIndex)
  496. {
  497. // Remove the body from the active bodies list
  498. RemoveBodyFromActiveBodies(body);
  499. // Mark this body as no longer active
  500. body.mMotionProperties->mIslandIndex = Body::cInactiveIndex;
  501. // Reset velocity
  502. body.mMotionProperties->mLinearVelocity = Vec3::sZero();
  503. body.mMotionProperties->mAngularVelocity = Vec3::sZero();
  504. // Call activation listener
  505. if (mActivationListener != nullptr)
  506. mActivationListener->OnBodyDeactivated(body_id, body.GetUserData());
  507. }
  508. }
  509. }
  510. void BodyManager::SetMotionQuality(Body &ioBody, EMotionQuality inMotionQuality)
  511. {
  512. MotionProperties *mp = ioBody.GetMotionPropertiesUnchecked();
  513. if (mp != nullptr && mp->GetMotionQuality() != inMotionQuality)
  514. {
  515. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  516. JPH_ASSERT(!mActiveBodiesLocked);
  517. bool is_active = ioBody.IsActive();
  518. if (is_active && mp->GetMotionQuality() == EMotionQuality::LinearCast)
  519. --mNumActiveCCDBodies;
  520. mp->mMotionQuality = inMotionQuality;
  521. if (is_active && mp->GetMotionQuality() == EMotionQuality::LinearCast)
  522. ++mNumActiveCCDBodies;
  523. }
  524. }
  525. void BodyManager::GetActiveBodies(EBodyType inType, BodyIDVector &outBodyIDs) const
  526. {
  527. JPH_PROFILE_FUNCTION();
  528. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  529. const BodyID *active_bodies = mActiveBodies[(int)inType];
  530. outBodyIDs.assign(active_bodies, active_bodies + mNumActiveBodies[(int)inType]);
  531. }
  532. void BodyManager::GetBodyIDs(BodyIDVector &outBodies) const
  533. {
  534. JPH_PROFILE_FUNCTION();
  535. UniqueLock lock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  536. // Reserve space for all bodies
  537. outBodies.clear();
  538. outBodies.reserve(mNumBodies);
  539. // Iterate the list and find the bodies that are not null
  540. for (const Body *b : mBodies)
  541. if (sIsValidBodyPointer(b))
  542. outBodies.push_back(b->GetID());
  543. // Validate that our reservation was correct
  544. JPH_ASSERT(outBodies.size() == mNumBodies);
  545. }
  546. void BodyManager::SetBodyActivationListener(BodyActivationListener *inListener)
  547. {
  548. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  549. mActivationListener = inListener;
  550. }
  551. BodyManager::MutexMask BodyManager::GetMutexMask(const BodyID *inBodies, int inNumber) const
  552. {
  553. JPH_ASSERT(sizeof(MutexMask) * 8 >= mBodyMutexes.GetNumMutexes(), "MutexMask must have enough bits");
  554. if (inNumber >= (int)mBodyMutexes.GetNumMutexes())
  555. {
  556. // Just lock everything if there are too many bodies
  557. return GetAllBodiesMutexMask();
  558. }
  559. else
  560. {
  561. MutexMask mask = 0;
  562. for (const BodyID *b = inBodies, *b_end = inBodies + inNumber; b < b_end; ++b)
  563. if (!b->IsInvalid())
  564. {
  565. uint32 index = mBodyMutexes.GetMutexIndex(b->GetIndex());
  566. mask |= (MutexMask(1) << index);
  567. }
  568. return mask;
  569. }
  570. }
  571. void BodyManager::LockRead(MutexMask inMutexMask) const
  572. {
  573. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckLock(this, EPhysicsLockTypes::PerBody));
  574. int index = 0;
  575. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  576. if (mask & 1)
  577. mBodyMutexes.GetMutexByIndex(index).lock_shared();
  578. }
  579. void BodyManager::UnlockRead(MutexMask inMutexMask) const
  580. {
  581. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckUnlock(this, EPhysicsLockTypes::PerBody));
  582. int index = 0;
  583. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  584. if (mask & 1)
  585. mBodyMutexes.GetMutexByIndex(index).unlock_shared();
  586. }
  587. void BodyManager::LockWrite(MutexMask inMutexMask) const
  588. {
  589. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckLock(this, EPhysicsLockTypes::PerBody));
  590. int index = 0;
  591. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  592. if (mask & 1)
  593. mBodyMutexes.GetMutexByIndex(index).lock();
  594. }
  595. void BodyManager::UnlockWrite(MutexMask inMutexMask) const
  596. {
  597. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckUnlock(this, EPhysicsLockTypes::PerBody));
  598. int index = 0;
  599. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  600. if (mask & 1)
  601. mBodyMutexes.GetMutexByIndex(index).unlock();
  602. }
  603. void BodyManager::LockAllBodies() const
  604. {
  605. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckLock(this, EPhysicsLockTypes::PerBody));
  606. mBodyMutexes.LockAll();
  607. PhysicsLock::sLock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  608. }
  609. void BodyManager::UnlockAllBodies() const
  610. {
  611. PhysicsLock::sUnlock(mBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::BodiesList));
  612. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckUnlock(this, EPhysicsLockTypes::PerBody));
  613. mBodyMutexes.UnlockAll();
  614. }
  615. void BodyManager::SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const
  616. {
  617. {
  618. LockAllBodies();
  619. // Determine which bodies to save
  620. Array<const Body *> bodies;
  621. bodies.reserve(mNumBodies);
  622. for (const Body *b : mBodies)
  623. if (sIsValidBodyPointer(b) && b->IsInBroadPhase() && (inFilter == nullptr || inFilter->ShouldSaveBody(*b)))
  624. bodies.push_back(b);
  625. // Write state of bodies
  626. uint32 num_bodies = (uint32)bodies.size();
  627. inStream.Write(num_bodies);
  628. for (const Body *b : bodies)
  629. {
  630. inStream.Write(b->GetID());
  631. inStream.Write(b->IsActive());
  632. b->SaveState(inStream);
  633. }
  634. UnlockAllBodies();
  635. }
  636. }
  637. bool BodyManager::RestoreState(StateRecorder &inStream)
  638. {
  639. BodyIDVector bodies_to_activate, bodies_to_deactivate;
  640. {
  641. LockAllBodies();
  642. if (inStream.IsValidating())
  643. {
  644. // Read state of bodies, note this reads it in a way to be consistent with validation
  645. uint32 old_num_bodies = 0;
  646. for (const Body *b : mBodies)
  647. if (sIsValidBodyPointer(b) && b->IsInBroadPhase())
  648. ++old_num_bodies;
  649. uint32 num_bodies = old_num_bodies; // Initialize to current value for validation
  650. inStream.Read(num_bodies);
  651. if (num_bodies != old_num_bodies)
  652. {
  653. JPH_ASSERT(false, "Cannot handle adding/removing bodies");
  654. UnlockAllBodies();
  655. return false;
  656. }
  657. for (Body *b : mBodies)
  658. if (sIsValidBodyPointer(b) && b->IsInBroadPhase())
  659. {
  660. BodyID body_id = b->GetID(); // Initialize to current value for validation
  661. inStream.Read(body_id);
  662. if (body_id != b->GetID())
  663. {
  664. JPH_ASSERT(false, "Cannot handle adding/removing bodies");
  665. UnlockAllBodies();
  666. return false;
  667. }
  668. bool is_active = b->IsActive(); // Initialize to current value for validation
  669. inStream.Read(is_active);
  670. if (is_active != b->IsActive())
  671. {
  672. if (is_active)
  673. bodies_to_activate.push_back(body_id);
  674. else
  675. bodies_to_deactivate.push_back(body_id);
  676. }
  677. b->RestoreState(inStream);
  678. }
  679. }
  680. else
  681. {
  682. // Not validating, we can be a bit more loose, read number of bodies
  683. uint32 num_bodies = 0;
  684. inStream.Read(num_bodies);
  685. // Iterate over the stored bodies and restore their state
  686. for (uint32 idx = 0; idx < num_bodies; ++idx)
  687. {
  688. BodyID body_id;
  689. inStream.Read(body_id);
  690. Body *b = TryGetBody(body_id);
  691. if (b == nullptr)
  692. {
  693. JPH_ASSERT(false, "Restoring state for non-existing body");
  694. UnlockAllBodies();
  695. return false;
  696. }
  697. bool is_active;
  698. inStream.Read(is_active);
  699. if (is_active != b->IsActive())
  700. {
  701. if (is_active)
  702. bodies_to_activate.push_back(body_id);
  703. else
  704. bodies_to_deactivate.push_back(body_id);
  705. }
  706. b->RestoreState(inStream);
  707. }
  708. }
  709. UnlockAllBodies();
  710. }
  711. {
  712. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  713. for (BodyID body_id : bodies_to_activate)
  714. {
  715. Body *body = TryGetBody(body_id);
  716. AddBodyToActiveBodies(*body);
  717. }
  718. for (BodyID body_id : bodies_to_deactivate)
  719. {
  720. Body *body = TryGetBody(body_id);
  721. RemoveBodyFromActiveBodies(*body);
  722. }
  723. }
  724. return true;
  725. }
  726. void BodyManager::SaveBodyState(const Body &inBody, StateRecorder &inStream) const
  727. {
  728. inStream.Write(inBody.IsActive());
  729. inBody.SaveState(inStream);
  730. }
  731. void BodyManager::RestoreBodyState(Body &ioBody, StateRecorder &inStream)
  732. {
  733. bool is_active = ioBody.IsActive();
  734. inStream.Read(is_active);
  735. ioBody.RestoreState(inStream);
  736. if (is_active != ioBody.IsActive())
  737. {
  738. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  739. JPH_ASSERT(!mActiveBodiesLocked || sOverrideAllowActivation);
  740. if (is_active)
  741. AddBodyToActiveBodies(ioBody);
  742. else
  743. RemoveBodyFromActiveBodies(ioBody);
  744. }
  745. }
  746. #ifdef JPH_DEBUG_RENDERER
  747. void BodyManager::Draw(const DrawSettings &inDrawSettings, const PhysicsSettings &inPhysicsSettings, DebugRenderer *inRenderer, const BodyDrawFilter *inBodyFilter)
  748. {
  749. JPH_PROFILE_FUNCTION();
  750. LockAllBodies();
  751. for (const Body *body : mBodies)
  752. if (sIsValidBodyPointer(body) && body->IsInBroadPhase() && (!inBodyFilter || inBodyFilter->ShouldDraw(*body)))
  753. {
  754. JPH_ASSERT(mBodies[body->GetID().GetIndex()] == body);
  755. bool is_sensor = body->IsSensor();
  756. // Determine drawing mode
  757. Color color;
  758. if (is_sensor)
  759. color = Color::sYellow;
  760. else
  761. switch (inDrawSettings.mDrawShapeColor)
  762. {
  763. case EShapeColor::InstanceColor:
  764. // Each instance has own color
  765. color = Color::sGetDistinctColor(body->mID.GetIndex());
  766. break;
  767. case EShapeColor::ShapeTypeColor:
  768. color = ShapeFunctions::sGet(body->GetShape()->GetSubType()).mColor;
  769. break;
  770. case EShapeColor::MotionTypeColor:
  771. // Determine color based on motion type
  772. switch (body->mMotionType)
  773. {
  774. case EMotionType::Static:
  775. color = Color::sGrey;
  776. break;
  777. case EMotionType::Kinematic:
  778. color = Color::sGreen;
  779. break;
  780. case EMotionType::Dynamic:
  781. color = Color::sGetDistinctColor(body->mID.GetIndex());
  782. break;
  783. default:
  784. JPH_ASSERT(false);
  785. color = Color::sBlack;
  786. break;
  787. }
  788. break;
  789. case EShapeColor::SleepColor:
  790. // Determine color based on motion type
  791. switch (body->mMotionType)
  792. {
  793. case EMotionType::Static:
  794. color = Color::sGrey;
  795. break;
  796. case EMotionType::Kinematic:
  797. color = body->IsActive()? Color::sGreen : Color::sRed;
  798. break;
  799. case EMotionType::Dynamic:
  800. color = body->IsActive()? Color::sYellow : Color::sRed;
  801. break;
  802. default:
  803. JPH_ASSERT(false);
  804. color = Color::sBlack;
  805. break;
  806. }
  807. break;
  808. case EShapeColor::IslandColor:
  809. // Determine color based on motion type
  810. switch (body->mMotionType)
  811. {
  812. case EMotionType::Static:
  813. color = Color::sGrey;
  814. break;
  815. case EMotionType::Kinematic:
  816. case EMotionType::Dynamic:
  817. {
  818. uint32 idx = body->GetMotionProperties()->GetIslandIndexInternal();
  819. color = idx != Body::cInactiveIndex? Color::sGetDistinctColor(idx) : Color::sLightGrey;
  820. }
  821. break;
  822. default:
  823. JPH_ASSERT(false);
  824. color = Color::sBlack;
  825. break;
  826. }
  827. break;
  828. case EShapeColor::MaterialColor:
  829. color = Color::sWhite;
  830. break;
  831. default:
  832. JPH_ASSERT(false);
  833. color = Color::sBlack;
  834. break;
  835. }
  836. // Draw the results of GetSupportFunction
  837. if (inDrawSettings.mDrawGetSupportFunction)
  838. body->mShape->DrawGetSupportFunction(inRenderer, body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f), color, inDrawSettings.mDrawSupportDirection);
  839. // Draw the results of GetSupportingFace
  840. if (inDrawSettings.mDrawGetSupportingFace)
  841. body->mShape->DrawGetSupportingFace(inRenderer, body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f));
  842. // Draw the shape
  843. if (inDrawSettings.mDrawShape)
  844. body->mShape->Draw(inRenderer, body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f), color, inDrawSettings.mDrawShapeColor == EShapeColor::MaterialColor, inDrawSettings.mDrawShapeWireframe || is_sensor);
  845. // Draw bounding box
  846. if (inDrawSettings.mDrawBoundingBox)
  847. inRenderer->DrawWireBox(body->mBounds, color);
  848. // Draw center of mass transform
  849. if (inDrawSettings.mDrawCenterOfMassTransform)
  850. inRenderer->DrawCoordinateSystem(body->GetCenterOfMassTransform(), 0.2f);
  851. // Draw world transform
  852. if (inDrawSettings.mDrawWorldTransform)
  853. inRenderer->DrawCoordinateSystem(body->GetWorldTransform(), 0.2f);
  854. // Draw world space linear and angular velocity
  855. if (inDrawSettings.mDrawVelocity)
  856. {
  857. RVec3 pos = body->GetCenterOfMassPosition();
  858. inRenderer->DrawArrow(pos, pos + body->GetLinearVelocity(), Color::sGreen, 0.1f);
  859. inRenderer->DrawArrow(pos, pos + body->GetAngularVelocity(), Color::sRed, 0.1f);
  860. }
  861. if (inDrawSettings.mDrawMassAndInertia && body->IsDynamic())
  862. {
  863. const MotionProperties *mp = body->GetMotionProperties();
  864. if (mp->GetInverseMass() > 0.0f
  865. && !Vec3::sEquals(mp->GetInverseInertiaDiagonal(), Vec3::sZero()).TestAnyXYZTrue())
  866. {
  867. // Invert mass again
  868. float mass = 1.0f / mp->GetInverseMass();
  869. // Invert diagonal again
  870. Vec3 diagonal = mp->GetInverseInertiaDiagonal().Reciprocal();
  871. // Determine how big of a box has the equivalent inertia
  872. Vec3 box_size = MassProperties::sGetEquivalentSolidBoxSize(mass, diagonal);
  873. // Draw box with equivalent inertia
  874. inRenderer->DrawWireBox(body->GetCenterOfMassTransform() * Mat44::sRotation(mp->GetInertiaRotation()), AABox(-0.5f * box_size, 0.5f * box_size), Color::sOrange);
  875. // Draw mass
  876. inRenderer->DrawText3D(body->GetCenterOfMassPosition(), StringFormat("%.2f", (double)mass), Color::sOrange, 0.2f);
  877. }
  878. }
  879. if (inDrawSettings.mDrawSleepStats && body->IsDynamic() && body->IsActive())
  880. {
  881. // Draw stats to know which bodies could go to sleep
  882. String text = StringFormat("t: %.1f", (double)body->mMotionProperties->mSleepTestTimer);
  883. uint8 g = uint8(Clamp(255.0f * body->mMotionProperties->mSleepTestTimer / inPhysicsSettings.mTimeBeforeSleep, 0.0f, 255.0f));
  884. Color sleep_color = Color(0, 255 - g, g);
  885. inRenderer->DrawText3D(body->GetCenterOfMassPosition(), text, sleep_color, 0.2f);
  886. for (int i = 0; i < 3; ++i)
  887. inRenderer->DrawWireSphere(JPH_IF_DOUBLE_PRECISION(body->mMotionProperties->GetSleepTestOffset() +) body->mMotionProperties->mSleepTestSpheres[i].GetCenter(), body->mMotionProperties->mSleepTestSpheres[i].GetRadius(), sleep_color);
  888. }
  889. if (body->IsSoftBody())
  890. {
  891. const SoftBodyMotionProperties *mp = static_cast<const SoftBodyMotionProperties *>(body->GetMotionProperties());
  892. RMat44 com = body->GetCenterOfMassTransform();
  893. if (inDrawSettings.mDrawSoftBodyVertices)
  894. mp->DrawVertices(inRenderer, com);
  895. if (inDrawSettings.mDrawSoftBodyEdgeConstraints)
  896. mp->DrawEdgeConstraints(inRenderer, com);
  897. if (inDrawSettings.mDrawSoftBodyVolumeConstraints)
  898. mp->DrawVolumeConstraints(inRenderer, com);
  899. if (inDrawSettings.mDrawSoftBodyPredictedBounds)
  900. mp->DrawPredictedBounds(inRenderer, com);
  901. }
  902. }
  903. UnlockAllBodies();
  904. }
  905. #endif // JPH_DEBUG_RENDERER
  906. void BodyManager::InvalidateContactCacheForBody(Body &ioBody)
  907. {
  908. // If this is the first time we flip the collision cache invalid flag, we need to add it to an internal list to ensure we reset the flag at the end of the physics update
  909. if (ioBody.InvalidateContactCacheInternal())
  910. {
  911. lock_guard lock(mBodiesCacheInvalidMutex);
  912. mBodiesCacheInvalid.push_back(ioBody.GetID());
  913. }
  914. }
  915. void BodyManager::ValidateContactCacheForAllBodies()
  916. {
  917. lock_guard lock(mBodiesCacheInvalidMutex);
  918. for (const BodyID &b : mBodiesCacheInvalid)
  919. {
  920. // The body may have been removed between the call to InvalidateContactCacheForBody and this call, so check if it still exists
  921. Body *body = TryGetBody(b);
  922. if (body != nullptr)
  923. body->ValidateContactCacheInternal();
  924. }
  925. mBodiesCacheInvalid.clear();
  926. }
  927. #ifdef _DEBUG
  928. void BodyManager::ValidateActiveBodyBounds()
  929. {
  930. UniqueLock lock(mActiveBodiesMutex JPH_IF_ENABLE_ASSERTS(, this, EPhysicsLockTypes::ActiveBodiesList));
  931. for (uint type = 0; type < cBodyTypeCount; ++type)
  932. for (BodyID *id = mActiveBodies[type], *id_end = mActiveBodies[type] + mNumActiveBodies[type]; id < id_end; ++id)
  933. {
  934. const Body *body = mBodies[id->GetIndex()];
  935. AABox cached = body->GetWorldSpaceBounds();
  936. AABox calculated = body->GetShape()->GetWorldSpaceBounds(body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f));
  937. JPH_ASSERT(cached == calculated);
  938. }
  939. }
  940. #endif // _DEBUG
  941. JPH_NAMESPACE_END