BodyManager.cpp 27 KB

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