BodyManager.cpp 38 KB

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