BodyManager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Physics/PhysicsSettings.h>
  5. #include <Jolt/Physics/Body/BodyManager.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/Body/BodyLock.h>
  8. #include <Jolt/Physics/Body/BodyActivationListener.h>
  9. #include <Jolt/Physics/StateRecorder.h>
  10. #include <Jolt/Core/StringTools.h>
  11. #ifdef JPH_DEBUG_RENDERER
  12. #include <Jolt/Renderer/DebugRenderer.h>
  13. #endif // JPH_DEBUG_RENDERER
  14. JPH_NAMESPACE_BEGIN
  15. #ifdef JPH_ENABLE_ASSERTS
  16. thread_local bool BodyManager::sOverrideAllowActivation = false;
  17. thread_local bool BodyManager::sOverrideAllowDeactivation = false;
  18. #endif
  19. // Helper class that combines a body and its motion properties
  20. class BodyWithMotionProperties : public Body
  21. {
  22. public:
  23. JPH_OVERRIDE_NEW_DELETE
  24. MotionProperties mMotionProperties;
  25. };
  26. inline void BodyManager::sDeleteBody(Body *inBody)
  27. {
  28. if (inBody->mMotionProperties != nullptr)
  29. {
  30. JPH_IF_ENABLE_ASSERTS(inBody->mMotionProperties = nullptr;)
  31. delete static_cast<BodyWithMotionProperties *>(inBody);
  32. }
  33. else
  34. delete inBody;
  35. }
  36. BodyManager::~BodyManager()
  37. {
  38. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  39. // Destroy any bodies that are still alive
  40. for (Body *b : mBodies)
  41. if (sIsValidBodyPointer(b))
  42. sDeleteBody(b);
  43. delete [] mActiveBodies;
  44. }
  45. void BodyManager::Init(uint inMaxBodies, uint inNumBodyMutexes, const BroadPhaseLayerInterface &inLayerInterface)
  46. {
  47. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  48. // Num body mutexes must be a power of two and not bigger than our MutexMask
  49. uint num_body_mutexes = Clamp<uint>(GetNextPowerOf2(inNumBodyMutexes == 0? 2 * thread::hardware_concurrency() : inNumBodyMutexes), 1, sizeof(MutexMask) * 8);
  50. // Allocate the body mutexes
  51. mBodyMutexes.Init(num_body_mutexes);
  52. // Allocate space for bodies
  53. mBodies.reserve(inMaxBodies);
  54. // Allocate space for active bodies
  55. JPH_ASSERT(mActiveBodies == nullptr);
  56. mActiveBodies = new BodyID [inMaxBodies];
  57. // Allocate space for sequence numbers
  58. mBodySequenceNumbers.resize(inMaxBodies);
  59. // Keep layer interface
  60. mBroadPhaseLayerInterface = &inLayerInterface;
  61. }
  62. uint BodyManager::GetNumBodies() const
  63. {
  64. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  65. return mNumBodies;
  66. }
  67. BodyManager::BodyStats BodyManager::GetBodyStats() const
  68. {
  69. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  70. BodyStats stats;
  71. stats.mNumBodies = mNumBodies;
  72. stats.mMaxBodies = uint(mBodies.capacity());
  73. for (const Body *body : mBodies)
  74. if (sIsValidBodyPointer(body))
  75. {
  76. switch (body->GetMotionType())
  77. {
  78. case EMotionType::Static:
  79. stats.mNumBodiesStatic++;
  80. break;
  81. case EMotionType::Dynamic:
  82. stats.mNumBodiesDynamic++;
  83. if (body->IsActive())
  84. stats.mNumActiveBodiesDynamic++;
  85. break;
  86. case EMotionType::Kinematic:
  87. stats.mNumBodiesKinematic++;
  88. if (body->IsActive())
  89. stats.mNumActiveBodiesKinematic++;
  90. break;
  91. }
  92. }
  93. return stats;
  94. }
  95. Body *BodyManager::CreateBody(const BodyCreationSettings &inBodyCreationSettings)
  96. {
  97. // Determine next free index
  98. uint32 idx;
  99. {
  100. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  101. if (mBodyIDFreeListStart != cBodyIDFreeListEnd)
  102. {
  103. // Pop an item from the freelist
  104. JPH_ASSERT(mBodyIDFreeListStart & cIsFreedBody);
  105. idx = uint32(mBodyIDFreeListStart >> cFreedBodyIndexShift);
  106. JPH_ASSERT(!sIsValidBodyPointer(mBodies[idx]));
  107. mBodyIDFreeListStart = uintptr_t(mBodies[idx]);
  108. }
  109. else
  110. {
  111. if (mBodies.size() < mBodies.capacity())
  112. {
  113. // Allocate a new entry, note that the array should not actually resize since we've reserved it at init time
  114. idx = uint32(mBodies.size());
  115. mBodies.push_back((Body *)cBodyIDFreeListEnd);
  116. }
  117. else
  118. {
  119. // Out of bodies
  120. return nullptr;
  121. }
  122. }
  123. // Update cached number of bodies
  124. mNumBodies++;
  125. }
  126. // Get next sequence number
  127. uint8 seq_no = GetNextSequenceNumber(idx);
  128. // Fill in basic properties
  129. Body *body;
  130. if (inBodyCreationSettings.HasMassProperties())
  131. {
  132. BodyWithMotionProperties *bmp = new BodyWithMotionProperties;
  133. body = bmp;
  134. body->mMotionProperties = &bmp->mMotionProperties;
  135. }
  136. else
  137. {
  138. body = new Body;
  139. }
  140. body->mID = BodyID(idx, seq_no);
  141. body->mShape = inBodyCreationSettings.GetShape();
  142. body->mUserData = inBodyCreationSettings.mUserData;
  143. body->SetFriction(inBodyCreationSettings.mFriction);
  144. body->SetRestitution(inBodyCreationSettings.mRestitution);
  145. body->mMotionType = inBodyCreationSettings.mMotionType;
  146. if (inBodyCreationSettings.mIsSensor)
  147. body->SetIsSensor(true);
  148. SetBodyObjectLayerInternal(*body, inBodyCreationSettings.mObjectLayer);
  149. body->mObjectLayer = inBodyCreationSettings.mObjectLayer;
  150. body->mCollisionGroup = inBodyCreationSettings.mCollisionGroup;
  151. if (inBodyCreationSettings.HasMassProperties())
  152. {
  153. MotionProperties *mp = body->mMotionProperties;
  154. mp->SetLinearDamping(inBodyCreationSettings.mLinearDamping);
  155. mp->SetAngularDamping(inBodyCreationSettings.mAngularDamping);
  156. mp->SetMaxLinearVelocity(inBodyCreationSettings.mMaxLinearVelocity);
  157. mp->SetMaxAngularVelocity(inBodyCreationSettings.mMaxAngularVelocity);
  158. mp->SetLinearVelocity(inBodyCreationSettings.mLinearVelocity); // Needs to happen after setting the max linear/angular velocity
  159. mp->SetAngularVelocity(inBodyCreationSettings.mAngularVelocity);
  160. mp->SetGravityFactor(inBodyCreationSettings.mGravityFactor);
  161. mp->SetMotionQuality(inBodyCreationSettings.mMotionQuality);
  162. mp->mAllowSleeping = inBodyCreationSettings.mAllowSleeping;
  163. mp->mIndexInActiveBodies = Body::cInactiveIndex;
  164. mp->mIslandIndex = Body::cInactiveIndex;
  165. JPH_IF_ENABLE_ASSERTS(mp->mCachedMotionType = body->mMotionType;)
  166. mp->SetMassProperties(inBodyCreationSettings.GetMassProperties());
  167. }
  168. // Position body
  169. body->SetPositionAndRotationInternal(inBodyCreationSettings.mPosition, inBodyCreationSettings.mRotation);
  170. // Add body
  171. mBodies[idx] = body;
  172. return body;
  173. }
  174. void BodyManager::DestroyBodies(const BodyID *inBodyIDs, int inNumber)
  175. {
  176. // Don't take lock if no bodies are to be destroyed
  177. if (inNumber <= 0)
  178. return;
  179. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  180. // Update cached number of bodies
  181. JPH_ASSERT(mNumBodies >= (uint)inNumber);
  182. mNumBodies -= inNumber;
  183. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  184. {
  185. // Get body
  186. BodyID body_id = *b;
  187. uint32 idx = body_id.GetIndex();
  188. Body *body = mBodies[idx];
  189. // Validate that it can be removed
  190. JPH_ASSERT(body->GetID() == body_id);
  191. JPH_ASSERT(!body->IsActive());
  192. JPH_ASSERT(!body->IsInBroadPhase());
  193. // Push the id onto the freelist
  194. mBodies[idx] = (Body *)mBodyIDFreeListStart;
  195. mBodyIDFreeListStart = (uintptr_t(idx) << cFreedBodyIndexShift) | cIsFreedBody;
  196. // Free the body
  197. sDeleteBody(body);
  198. }
  199. #if defined(_DEBUG) && defined(JPH_ENABLE_ASSERTS)
  200. // Check that the freelist is correct
  201. size_t num_freed = 0;
  202. for (uintptr_t start = mBodyIDFreeListStart; start != cBodyIDFreeListEnd; start = uintptr_t(mBodies[start >> cFreedBodyIndexShift]))
  203. {
  204. JPH_ASSERT(start & cIsFreedBody);
  205. num_freed++;
  206. }
  207. JPH_ASSERT(mNumBodies == mBodies.size() - num_freed);
  208. #endif // defined(_DEBUG) && _defined(JPH_ENABLE_ASSERTS)
  209. }
  210. void BodyManager::ActivateBodies(const BodyID *inBodyIDs, int inNumber)
  211. {
  212. // Don't take lock if no bodies are to be activated
  213. if (inNumber <= 0)
  214. return;
  215. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  216. JPH_ASSERT(!mActiveBodiesLocked || sOverrideAllowActivation);
  217. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  218. if (!b->IsInvalid())
  219. {
  220. BodyID body_id = *b;
  221. Body &body = *mBodies[body_id.GetIndex()];
  222. JPH_ASSERT(GetMutexForBody(body_id).is_locked(), "Assuming that body has been locked!");
  223. JPH_ASSERT(body.GetID() == body_id);
  224. JPH_ASSERT(body.IsInBroadPhase());
  225. if (!body.IsStatic()
  226. && body.mMotionProperties->mIndexInActiveBodies == Body::cInactiveIndex)
  227. {
  228. body.mMotionProperties->mIndexInActiveBodies = mNumActiveBodies;
  229. body.ResetSleepTestSpheres();
  230. JPH_ASSERT(mNumActiveBodies < GetMaxBodies());
  231. mActiveBodies[mNumActiveBodies] = body_id;
  232. mNumActiveBodies++; // Increment atomic after setting the body ID so that PhysicsSystem::JobFindCollisions (which doesn't lock the mActiveBodiesMutex) will only read valid IDs
  233. // Count CCD bodies
  234. if (body.mMotionProperties->GetMotionQuality() == EMotionQuality::LinearCast)
  235. mNumActiveCCDBodies++;
  236. // Call activation listener
  237. if (mActivationListener != nullptr)
  238. mActivationListener->OnBodyActivated(body_id, body.GetUserData());
  239. }
  240. }
  241. }
  242. void BodyManager::DeactivateBodies(const BodyID *inBodyIDs, int inNumber)
  243. {
  244. // Don't take lock if no bodies are to be deactivated
  245. if (inNumber <= 0)
  246. return;
  247. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  248. JPH_ASSERT(!mActiveBodiesLocked || sOverrideAllowDeactivation);
  249. for (const BodyID *b = inBodyIDs, *b_end = inBodyIDs + inNumber; b < b_end; b++)
  250. if (!b->IsInvalid())
  251. {
  252. BodyID body_id = *b;
  253. Body &body = *mBodies[body_id.GetIndex()];
  254. JPH_ASSERT(GetMutexForBody(body_id).is_locked(), "Assuming that body has been locked!");
  255. JPH_ASSERT(body.GetID() == body_id);
  256. JPH_ASSERT(body.IsInBroadPhase());
  257. if (body.mMotionProperties != nullptr
  258. && body.mMotionProperties->mIndexInActiveBodies != Body::cInactiveIndex)
  259. {
  260. uint32 last_body_index = mNumActiveBodies - 1;
  261. if (body.mMotionProperties->mIndexInActiveBodies != last_body_index)
  262. {
  263. // This is not the last body, use the last body to fill the hole
  264. BodyID last_body_id = mActiveBodies[last_body_index];
  265. mActiveBodies[body.mMotionProperties->mIndexInActiveBodies] = last_body_id;
  266. // Update that body's index in the active list
  267. Body &last_body = *mBodies[last_body_id.GetIndex()];
  268. JPH_ASSERT(last_body.mMotionProperties->mIndexInActiveBodies == last_body_index);
  269. last_body.mMotionProperties->mIndexInActiveBodies = body.mMotionProperties->mIndexInActiveBodies;
  270. }
  271. // Mark this body as no longer active
  272. body.mMotionProperties->mIndexInActiveBodies = Body::cInactiveIndex;
  273. body.mMotionProperties->mIslandIndex = Body::cInactiveIndex;
  274. // Reset velocity
  275. body.mMotionProperties->mLinearVelocity = Vec3::sZero();
  276. body.mMotionProperties->mAngularVelocity = Vec3::sZero();
  277. // Remove unused element from active bodies list
  278. --mNumActiveBodies;
  279. // Count CCD bodies
  280. if (body.mMotionProperties->GetMotionQuality() == EMotionQuality::LinearCast)
  281. mNumActiveCCDBodies--;
  282. // Call activation listener
  283. if (mActivationListener != nullptr)
  284. mActivationListener->OnBodyDeactivated(body_id, body.GetUserData());
  285. }
  286. }
  287. }
  288. void BodyManager::GetActiveBodies(BodyIDVector &outBodyIDs) const
  289. {
  290. JPH_PROFILE_FUNCTION();
  291. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  292. outBodyIDs.assign(mActiveBodies, mActiveBodies + mNumActiveBodies);
  293. }
  294. void BodyManager::GetBodyIDs(BodyIDVector &outBodies) const
  295. {
  296. JPH_PROFILE_FUNCTION();
  297. UniqueLock lock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  298. // Reserve space for all bodies
  299. outBodies.clear();
  300. outBodies.reserve(mNumBodies);
  301. // Iterate the list and find the bodies that are not null
  302. for (const Body *b : mBodies)
  303. if (sIsValidBodyPointer(b))
  304. outBodies.push_back(b->GetID());
  305. // Validate that our reservation was correct
  306. JPH_ASSERT(outBodies.size() == mNumBodies);
  307. }
  308. void BodyManager::SetBodyActivationListener(BodyActivationListener *inListener)
  309. {
  310. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  311. mActivationListener = inListener;
  312. }
  313. BodyManager::MutexMask BodyManager::GetMutexMask(const BodyID *inBodies, int inNumber) const
  314. {
  315. JPH_ASSERT(sizeof(MutexMask) * 8 >= mBodyMutexes.GetNumMutexes(), "MutexMask must have enough bits");
  316. if (inNumber >= (int)mBodyMutexes.GetNumMutexes())
  317. {
  318. // Just lock everything if there are too many bodies
  319. return GetAllBodiesMutexMask();
  320. }
  321. else
  322. {
  323. MutexMask mask = 0;
  324. for (const BodyID *b = inBodies, *b_end = inBodies + inNumber; b < b_end; ++b)
  325. if (!b->IsInvalid())
  326. {
  327. uint32 index = mBodyMutexes.GetMutexIndex(b->GetIndex());
  328. mask |= (MutexMask(1) << index);
  329. }
  330. return mask;
  331. }
  332. }
  333. void BodyManager::LockRead(MutexMask inMutexMask) const
  334. {
  335. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckLock(EPhysicsLockTypes::PerBody));
  336. int index = 0;
  337. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  338. if (mask & 1)
  339. mBodyMutexes.GetMutexByIndex(index).lock_shared();
  340. }
  341. void BodyManager::UnlockRead(MutexMask inMutexMask) const
  342. {
  343. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckUnlock(EPhysicsLockTypes::PerBody));
  344. int index = 0;
  345. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  346. if (mask & 1)
  347. mBodyMutexes.GetMutexByIndex(index).unlock_shared();
  348. }
  349. void BodyManager::LockWrite(MutexMask inMutexMask) const
  350. {
  351. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckLock(EPhysicsLockTypes::PerBody));
  352. int index = 0;
  353. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  354. if (mask & 1)
  355. mBodyMutexes.GetMutexByIndex(index).lock();
  356. }
  357. void BodyManager::UnlockWrite(MutexMask inMutexMask) const
  358. {
  359. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckUnlock(EPhysicsLockTypes::PerBody));
  360. int index = 0;
  361. for (MutexMask mask = inMutexMask; mask != 0; mask >>= 1, index++)
  362. if (mask & 1)
  363. mBodyMutexes.GetMutexByIndex(index).unlock();
  364. }
  365. void BodyManager::LockAllBodies() const
  366. {
  367. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckLock(EPhysicsLockTypes::PerBody));
  368. mBodyMutexes.LockAll();
  369. PhysicsLock::sLock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  370. }
  371. void BodyManager::UnlockAllBodies() const
  372. {
  373. PhysicsLock::sUnlock(mBodiesMutex, EPhysicsLockTypes::BodiesList);
  374. JPH_IF_ENABLE_ASSERTS(PhysicsLock::sCheckUnlock(EPhysicsLockTypes::PerBody));
  375. mBodyMutexes.UnlockAll();
  376. }
  377. void BodyManager::SaveState(StateRecorder &inStream) const
  378. {
  379. {
  380. LockAllBodies();
  381. // Count number of bodies
  382. size_t num_bodies = 0;
  383. for (const Body *b : mBodies)
  384. if (sIsValidBodyPointer(b) && b->IsInBroadPhase())
  385. ++num_bodies;
  386. inStream.Write(num_bodies);
  387. // Write state of bodies
  388. for (const Body *b : mBodies)
  389. if (sIsValidBodyPointer(b) && b->IsInBroadPhase())
  390. {
  391. inStream.Write(b->GetID());
  392. b->SaveState(inStream);
  393. }
  394. UnlockAllBodies();
  395. }
  396. {
  397. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  398. // Write active bodies, sort because activation can come from multiple threads, so order is not deterministic
  399. inStream.Write(mNumActiveBodies);
  400. BodyIDVector sorted_active_bodies(mActiveBodies, mActiveBodies + mNumActiveBodies);
  401. sort(sorted_active_bodies.begin(), sorted_active_bodies.end());
  402. for (const BodyID &id : sorted_active_bodies)
  403. inStream.Write(id);
  404. inStream.Write(mNumActiveCCDBodies);
  405. }
  406. }
  407. bool BodyManager::RestoreState(StateRecorder &inStream)
  408. {
  409. {
  410. LockAllBodies();
  411. // Read state of bodies, note this reads it in a way to be consistent with validation
  412. size_t old_num_bodies = 0;
  413. for (const Body *b : mBodies)
  414. if (sIsValidBodyPointer(b) && b->IsInBroadPhase())
  415. ++old_num_bodies;
  416. size_t num_bodies = old_num_bodies; // Initialize to current value for validation
  417. inStream.Read(num_bodies);
  418. if (num_bodies != old_num_bodies)
  419. {
  420. JPH_ASSERT(false, "Cannot handle adding/removing bodies");
  421. UnlockAllBodies();
  422. return false;
  423. }
  424. for (Body *b : mBodies)
  425. if (sIsValidBodyPointer(b) && b->IsInBroadPhase())
  426. {
  427. BodyID body_id = b->GetID(); // Initialize to current value for validation
  428. inStream.Read(body_id);
  429. if (body_id != b->GetID())
  430. {
  431. JPH_ASSERT(false, "Cannot handle adding/removing bodies");
  432. UnlockAllBodies();
  433. return false;
  434. }
  435. b->RestoreState(inStream);
  436. }
  437. UnlockAllBodies();
  438. }
  439. {
  440. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  441. // Mark current active bodies as deactivated
  442. for (const BodyID *id = mActiveBodies, *id_end = mActiveBodies + mNumActiveBodies; id < id_end; ++id)
  443. mBodies[id->GetIndex()]->mMotionProperties->mIndexInActiveBodies = Body::cInactiveIndex;
  444. sort(mActiveBodies, mActiveBodies + mNumActiveBodies); // Sort for validation
  445. // Read active bodies
  446. inStream.Read(mNumActiveBodies);
  447. for (BodyID *id = mActiveBodies, *id_end = mActiveBodies + mNumActiveBodies; id < id_end; ++id)
  448. {
  449. inStream.Read(*id);
  450. mBodies[id->GetIndex()]->mMotionProperties->mIndexInActiveBodies = uint32(id - mActiveBodies);
  451. }
  452. inStream.Read(mNumActiveCCDBodies);
  453. }
  454. return true;
  455. }
  456. #ifdef JPH_DEBUG_RENDERER
  457. void BodyManager::Draw(const DrawSettings &inDrawSettings, const PhysicsSettings &inPhysicsSettings, DebugRenderer *inRenderer)
  458. {
  459. JPH_PROFILE_FUNCTION();
  460. LockAllBodies();
  461. for (const Body *body : mBodies)
  462. if (sIsValidBodyPointer(body) && body->IsInBroadPhase())
  463. {
  464. JPH_ASSERT(mBodies[body->GetID().GetIndex()] == body);
  465. bool is_sensor = body->IsSensor();
  466. // Determine drawing mode
  467. Color color;
  468. if (is_sensor)
  469. color = Color::sYellow;
  470. else
  471. switch (inDrawSettings.mDrawShapeColor)
  472. {
  473. case EShapeColor::InstanceColor:
  474. // Each instance has own color
  475. color = Color::sGetDistinctColor(body->mID.GetIndex());
  476. break;
  477. case EShapeColor::ShapeTypeColor:
  478. color = ShapeFunctions::sGet(body->GetShape()->GetSubType()).mColor;
  479. break;
  480. case EShapeColor::MotionTypeColor:
  481. // Determine color based on motion type
  482. switch (body->mMotionType)
  483. {
  484. case EMotionType::Static:
  485. color = Color::sGrey;
  486. break;
  487. case EMotionType::Kinematic:
  488. color = Color::sGreen;
  489. break;
  490. case EMotionType::Dynamic:
  491. color = Color::sGetDistinctColor(body->mID.GetIndex());
  492. break;
  493. default:
  494. JPH_ASSERT(false);
  495. color = Color::sBlack;
  496. break;
  497. }
  498. break;
  499. case EShapeColor::SleepColor:
  500. // Determine color based on motion type
  501. switch (body->mMotionType)
  502. {
  503. case EMotionType::Static:
  504. color = Color::sGrey;
  505. break;
  506. case EMotionType::Kinematic:
  507. color = body->IsActive()? Color::sGreen : Color::sRed;
  508. break;
  509. case EMotionType::Dynamic:
  510. color = body->IsActive()? Color::sYellow : Color::sRed;
  511. break;
  512. default:
  513. JPH_ASSERT(false);
  514. color = Color::sBlack;
  515. break;
  516. }
  517. break;
  518. case EShapeColor::IslandColor:
  519. // Determine color based on motion type
  520. switch (body->mMotionType)
  521. {
  522. case EMotionType::Static:
  523. color = Color::sGrey;
  524. break;
  525. case EMotionType::Kinematic:
  526. case EMotionType::Dynamic:
  527. {
  528. uint32 idx = body->GetMotionProperties()->GetIslandIndexInternal();
  529. color = idx != Body::cInactiveIndex? Color::sGetDistinctColor(idx) : Color::sLightGrey;
  530. }
  531. break;
  532. default:
  533. JPH_ASSERT(false);
  534. color = Color::sBlack;
  535. break;
  536. }
  537. break;
  538. case EShapeColor::MaterialColor:
  539. color = Color::sWhite;
  540. break;
  541. default:
  542. JPH_ASSERT(false);
  543. color = Color::sBlack;
  544. break;
  545. }
  546. // Draw the results of GetSupportFunction
  547. if (inDrawSettings.mDrawGetSupportFunction)
  548. body->mShape->DrawGetSupportFunction(inRenderer, body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f), color, inDrawSettings.mDrawSupportDirection);
  549. // Draw the results of GetSupportingFace
  550. if (inDrawSettings.mDrawGetSupportingFace)
  551. body->mShape->DrawGetSupportingFace(inRenderer, body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f));
  552. // Draw the shape
  553. if (inDrawSettings.mDrawShape)
  554. body->mShape->Draw(inRenderer, body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f), color, inDrawSettings.mDrawShapeColor == EShapeColor::MaterialColor, inDrawSettings.mDrawShapeWireframe || is_sensor);
  555. // Draw bounding box
  556. if (inDrawSettings.mDrawBoundingBox)
  557. inRenderer->DrawWireBox(body->mBounds, color);
  558. // Draw center of mass transform
  559. if (inDrawSettings.mDrawCenterOfMassTransform)
  560. inRenderer->DrawCoordinateSystem(body->GetCenterOfMassTransform(), 0.2f);
  561. // Draw world transform
  562. if (inDrawSettings.mDrawWorldTransform)
  563. inRenderer->DrawCoordinateSystem(body->GetWorldTransform(), 0.2f);
  564. // Draw world space linear and angular velocity
  565. if (inDrawSettings.mDrawVelocity)
  566. {
  567. Vec3 pos = body->GetCenterOfMassPosition();
  568. inRenderer->DrawArrow(pos, pos + body->GetLinearVelocity(), Color::sGreen, 0.1f);
  569. inRenderer->DrawArrow(pos, pos + body->GetAngularVelocity(), Color::sRed, 0.1f);
  570. }
  571. if (inDrawSettings.mDrawMassAndInertia && body->IsDynamic())
  572. {
  573. const MotionProperties *mp = body->GetMotionProperties();
  574. // Invert mass again
  575. float mass = 1.0f / mp->GetInverseMass();
  576. // Invert diagonal again
  577. Vec3 diagonal = mp->GetInverseInertiaDiagonal().Reciprocal();
  578. // Determine how big of a box has the equivalent inertia
  579. Vec3 box_size = MassProperties::sGetEquivalentSolidBoxSize(mass, diagonal);
  580. // Draw box with equivalent inertia
  581. inRenderer->DrawWireBox(body->GetCenterOfMassTransform() * Mat44::sRotation(mp->GetInertiaRotation()), AABox(-0.5f * box_size, 0.5f * box_size), Color::sOrange);
  582. // Draw mass
  583. inRenderer->DrawText3D(body->GetCenterOfMassPosition(), StringFormat("%.2f", (double)mass), Color::sOrange, 0.2f);
  584. }
  585. if (inDrawSettings.mDrawSleepStats && body->IsDynamic() && body->IsActive())
  586. {
  587. // Draw stats to know which bodies could go to sleep
  588. String text = StringFormat("t: %.1f", (double)body->mMotionProperties->mSleepTestTimer);
  589. uint8 g = uint8(Clamp(255.0f * body->mMotionProperties->mSleepTestTimer / inPhysicsSettings.mTimeBeforeSleep, 0.0f, 255.0f));
  590. Color sleep_color = Color(0, 255 - g, g);
  591. inRenderer->DrawText3D(body->GetCenterOfMassPosition(), text, sleep_color, 0.2f);
  592. for (int i = 0; i < 3; ++i)
  593. inRenderer->DrawWireSphere(body->mMotionProperties->mSleepTestSpheres[i].GetCenter(), body->mMotionProperties->mSleepTestSpheres[i].GetRadius(), sleep_color);
  594. }
  595. }
  596. UnlockAllBodies();
  597. }
  598. #endif // JPH_DEBUG_RENDERER
  599. void BodyManager::InvalidateContactCacheForBody(Body &ioBody)
  600. {
  601. // 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
  602. if (ioBody.InvalidateContactCacheInternal())
  603. {
  604. lock_guard lock(mBodiesCacheInvalidMutex);
  605. mBodiesCacheInvalid.push_back(ioBody.GetID());
  606. }
  607. }
  608. void BodyManager::ValidateContactCacheForAllBodies()
  609. {
  610. lock_guard lock(mBodiesCacheInvalidMutex);
  611. for (const BodyID &b : mBodiesCacheInvalid)
  612. {
  613. // The body may have been removed between the call to InvalidateContactCacheForBody and this call, so check if it still exists
  614. Body *body = TryGetBody(b);
  615. if (body != nullptr)
  616. body->ValidateContactCacheInternal();
  617. }
  618. mBodiesCacheInvalid.clear();
  619. }
  620. #ifdef _DEBUG
  621. void BodyManager::ValidateActiveBodyBounds()
  622. {
  623. UniqueLock lock(mActiveBodiesMutex, EPhysicsLockTypes::ActiveBodiesList);
  624. for (BodyID *id = mActiveBodies, *id_end = mActiveBodies + mNumActiveBodies; id < id_end; ++id)
  625. {
  626. const Body *body = mBodies[id->GetIndex()];
  627. AABox cached = body->GetWorldSpaceBounds();
  628. AABox calculated = body->GetShape()->GetWorldSpaceBounds(body->GetCenterOfMassTransform(), Vec3::sReplicate(1.0f));
  629. JPH_ASSERT(cached == calculated);
  630. }
  631. }
  632. #endif // _DEBUG
  633. JPH_NAMESPACE_END