BodyManager.cpp 28 KB

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