PhysicsSystem.cpp 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/PhysicsSystem.h>
  5. #include <Physics/PhysicsSettings.h>
  6. #include <Physics/PhysicsUpdateContext.h>
  7. #include <Physics/PhysicsStepListener.h>
  8. #include <Physics/Collision/BroadPhase/BroadPhaseBruteForce.h>
  9. #include <Physics/Collision/BroadPhase/BroadPhaseQuadTree.h>
  10. #include <Physics/Collision/CollisionDispatch.h>
  11. #include <Physics/Collision/AABoxCast.h>
  12. #include <Physics/Collision/ShapeCast.h>
  13. #include <Physics/Collision/CollideShape.h>
  14. #include <Physics/Collision/CollisionCollectorImpl.h>
  15. #include <Physics/Collision/CastResult.h>
  16. #include <Physics/Collision/CollideConvexVsTriangles.h>
  17. #include <Physics/Collision/ManifoldBetweenTwoFaces.h>
  18. #include <Physics/Collision/Shape/ConvexShape.h>
  19. #include <Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  20. #include <Geometry/RayAABox.h>
  21. #include <Core/JobSystem.h>
  22. #include <Core/StatCollector.h>
  23. #include <Core/TempAllocator.h>
  24. namespace JPH {
  25. #ifdef JPH_DEBUG_RENDERER
  26. bool PhysicsSystem::sDrawMotionQualityLinearCast = false;
  27. #endif // JPH_DEBUG_RENDERER
  28. //#define BROAD_PHASE BroadPhaseBruteForce
  29. #define BROAD_PHASE BroadPhaseQuadTree
  30. static const Color cColorUpdateBroadPhaseFinalize = Color::sGetDistinctColor(1);
  31. static const Color cColorUpdateBroadPhasePrepare = Color::sGetDistinctColor(2);
  32. static const Color cColorFindCollisions = Color::sGetDistinctColor(3);
  33. static const Color cColorApplyGravity = Color::sGetDistinctColor(4);
  34. static const Color cColorSetupVelocityConstraints = Color::sGetDistinctColor(5);
  35. static const Color cColorBuildIslandsFromConstraints = Color::sGetDistinctColor(6);
  36. static const Color cColorDetermineActiveConstraints = Color::sGetDistinctColor(7);
  37. static const Color cColorFinalizeIslands = Color::sGetDistinctColor(8);
  38. static const Color cColorContactRemovedCallbacks = Color::sGetDistinctColor(9);
  39. static const Color cColorBodySetIslandIndex = Color::sGetDistinctColor(10);
  40. static const Color cColorStartNextStep = Color::sGetDistinctColor(11);
  41. static const Color cColorSolveVelocityConstraints = Color::sGetDistinctColor(12);
  42. static const Color cColorIntegrateVelocity = Color::sGetDistinctColor(13);
  43. static const Color cColorResolveCCDContacts = Color::sGetDistinctColor(14);
  44. static const Color cColorSolvePositionConstraints = Color::sGetDistinctColor(15);
  45. static const Color cColorStartNextSubStep = Color::sGetDistinctColor(16);
  46. static const Color cColorFindCCDContacts = Color::sGetDistinctColor(17);
  47. static const Color cColorStepListeners = Color::sGetDistinctColor(18);
  48. PhysicsSystem::~PhysicsSystem()
  49. {
  50. // Remove broadphase
  51. delete mBroadPhase;
  52. }
  53. void PhysicsSystem::Init(uint inMaxBodies, uint inMaxBodyPairs, uint inMaxContactConstraints, const ObjectToBroadPhaseLayer &inObjectToBroadPhaseLayer, BroadPhaseLayerPairFilter inBroadPhaseLayerPairFilter, ObjectLayerPairFilter inObjectLayerPairFilter)
  54. {
  55. mBroadPhaseLayerPairFilter = inBroadPhaseLayerPairFilter;
  56. mObjectLayerPairFilter = inObjectLayerPairFilter;
  57. mObjectToBroadPhaseLayer = inObjectToBroadPhaseLayer;
  58. // Initialize body manager
  59. mBodyManager.Init(inMaxBodies);
  60. // Create broadphase
  61. mBroadPhase = new BROAD_PHASE();
  62. mBroadPhase->Init(&mBodyManager, mObjectToBroadPhaseLayer);
  63. // Init contact constraint manager
  64. mContactManager.Init(inMaxBodyPairs, inMaxContactConstraints);
  65. // Init islands builder
  66. mIslandBuilder.Init(inMaxBodies);
  67. // Initialize body interface
  68. mBodyInterfaceLocking.~BodyInterface();
  69. new (&mBodyInterfaceLocking) BodyInterface(mBodyLockInterfaceLocking, mBodyManager, *mBroadPhase);
  70. mBodyInterfaceNoLock.~BodyInterface();
  71. new (&mBodyInterfaceNoLock) BodyInterface(mBodyLockInterfaceNoLock, mBodyManager, *mBroadPhase);
  72. // Initialize narrow phase query
  73. mNarrowPhaseQueryLocking.~NarrowPhaseQuery();
  74. new (&mNarrowPhaseQueryLocking) NarrowPhaseQuery(mBodyInterfaceLocking, *mBroadPhase);
  75. mNarrowPhaseQueryNoLock.~NarrowPhaseQuery();
  76. new (&mNarrowPhaseQueryNoLock) NarrowPhaseQuery(mBodyInterfaceNoLock, *mBroadPhase);
  77. }
  78. void PhysicsSystem::OptimizeBroadPhase()
  79. {
  80. mBroadPhase->Optimize();
  81. }
  82. void PhysicsSystem::AddStepListener(PhysicsStepListener *inListener)
  83. {
  84. lock_guard<Mutex> lock(mStepListenersMutex);
  85. JPH_ASSERT(find(mStepListeners.begin(), mStepListeners.end(), inListener) == mStepListeners.end());
  86. mStepListeners.push_back(inListener);
  87. }
  88. void PhysicsSystem::RemoveStepListener(PhysicsStepListener *inListener)
  89. {
  90. lock_guard<Mutex> lock(mStepListenersMutex);
  91. StepListeners::iterator i = find(mStepListeners.begin(), mStepListeners.end(), inListener);
  92. JPH_ASSERT(i != mStepListeners.end());
  93. mStepListeners.erase(i);
  94. }
  95. void PhysicsSystem::Update(float inDeltaTime, int inCollisionSteps, int inIntegrationSubSteps, TempAllocator *inTempAllocator, JobSystem *inJobSystem)
  96. {
  97. JPH_PROFILE_FUNCTION();
  98. JPH_ASSERT(inDeltaTime >= 0.0f);
  99. JPH_ASSERT(inIntegrationSubSteps <= PhysicsUpdateContext::cMaxSubSteps);
  100. #ifdef JPH_STAT_COLLECTOR
  101. // Reset stats
  102. mManifoldsBeforeReduction = 0;
  103. mManifoldsAfterReduction = 0;
  104. CollideConvexVsTriangles::sResetStats();
  105. ConvexShape::sResetStats();
  106. #endif // JPH_STAT_COLLECTOR
  107. // Sync point for the broadphase. This will allow it to do clean up operations without having any mutexes locked yet.
  108. mBroadPhase->FrameSync();
  109. // If there are no active bodies or there's no time delta
  110. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies();
  111. if (num_active_bodies == 0 || inDeltaTime <= 0.0f)
  112. {
  113. mBodyManager.LockAllBodies();
  114. // Update broadphase
  115. mBroadPhase->LockModifications();
  116. BroadPhase::UpdateState update_state = mBroadPhase->UpdatePrepare();
  117. mBroadPhase->UpdateFinalize(update_state);
  118. mBroadPhase->UnlockModifications();
  119. mBodyManager.UnlockAllBodies();
  120. return;
  121. }
  122. // Calculate ratio between current and previous frame delta time to scale initial constraint forces
  123. float sub_step_delta_time = inDeltaTime / (inCollisionSteps * inIntegrationSubSteps);
  124. float warm_start_impulse_ratio = mPhysicsSettings.mConstraintWarmStart && mPreviousSubStepDeltaTime > 0.0f? sub_step_delta_time / mPreviousSubStepDeltaTime : 0.0f;
  125. mPreviousSubStepDeltaTime = sub_step_delta_time;
  126. // Create the context used for passing information between jobs
  127. PhysicsUpdateContext context;
  128. context.mPhysicsSystem = this;
  129. context.mTempAllocator = inTempAllocator;
  130. context.mJobSystem = inJobSystem;
  131. context.mBarrier = inJobSystem->CreateBarrier();
  132. context.mIslandBuilder = &mIslandBuilder;
  133. context.mStepDeltaTime = inDeltaTime / inCollisionSteps;
  134. context.mSubStepDeltaTime = sub_step_delta_time;
  135. context.mWarmStartImpulseRatio = warm_start_impulse_ratio;
  136. // Allocate space for body pairs
  137. JPH_ASSERT(context.mBodyPairs == nullptr);
  138. context.mBodyPairs = static_cast<BodyPair *>(inTempAllocator->Allocate(sizeof(BodyPair) * mPhysicsSettings.mMaxInFlightBodyPairs));
  139. // Lock all bodies for write so that we can freely touch them
  140. mStepListenersMutex.lock();
  141. mBodyManager.LockAllBodies();
  142. mBroadPhase->LockModifications();
  143. // Get max number of concurrent jobs
  144. int max_concurrency = context.GetMaxConcurrency();
  145. // Calculate how many step listener jobs we spawn
  146. int num_step_listener_jobs = mStepListeners.empty()? 0 : max(1, min((int)mStepListeners.size() / mPhysicsSettings.mStepListenersBatchSize / mPhysicsSettings.mStepListenerBatchesPerJob, max_concurrency));
  147. // Number of gravity jobs depends on the amount of active bodies.
  148. // Launch max 1 job per batch of active bodies
  149. // Leave 1 thread for update broadphase prepare and 1 for determine active constraints
  150. int num_apply_gravity_jobs = max(1, min(((int)num_active_bodies + cApplyGravityBatchSize - 1) / cApplyGravityBatchSize, max_concurrency - 2));
  151. // Number of determine active constraints jobs to run depends on number of constraints.
  152. // Leave 1 thread for update broadphase prepare and 1 for apply gravity
  153. int num_determine_active_constraints_jobs = max(1, min(((int)mConstraintManager.GetNumConstraints() + cDetermineActiveConstraintsBatchSize - 1) / cDetermineActiveConstraintsBatchSize, max_concurrency - 2));
  154. // Number of find collisions jobs to run depends on number of active bodies.
  155. int num_find_collisions_jobs = max(1, min(((int)num_active_bodies + cActiveBodiesBatchSize - 1) / cActiveBodiesBatchSize, max_concurrency));
  156. {
  157. JPH_PROFILE("Build Jobs");
  158. // Iterate over collision steps
  159. context.mSteps.resize(inCollisionSteps);
  160. for (int step_idx = 0; step_idx < inCollisionSteps; ++step_idx)
  161. {
  162. bool is_first_step = step_idx == 0;
  163. bool is_last_step = step_idx == inCollisionSteps - 1;
  164. PhysicsUpdateContext::Step &step = context.mSteps[step_idx];
  165. step.mContext = &context;
  166. step.mSubSteps.resize(inIntegrationSubSteps);
  167. // Create job to do broadphase finalization
  168. // This job must finish before integrating velocities. Until then the positions will not be updated neither will bodies be added / removed.
  169. step.mUpdateBroadphaseFinalize = inJobSystem->CreateJob("UpdateBroadPhaseFinalize", cColorUpdateBroadPhaseFinalize, [&context, &step]()
  170. {
  171. // Validate that all find collision jobs have stopped
  172. JPH_ASSERT(step.mActiveFindCollisionJobs == 0);
  173. // Finalize the broadphase update
  174. context.mPhysicsSystem->mBroadPhase->UpdateFinalize(step.mBroadPhaseUpdateState);
  175. // Signal that it is done
  176. step.mSubSteps[0].mIntegrateVelocity.RemoveDependency();
  177. }, num_find_collisions_jobs + 2); // depends on: find collisions, broadphase prepare update, finish building jobs
  178. // The immediate jobs below are only immediate for the first step, the all finished job will kick them for the next step
  179. int previous_step_dependency_count = is_first_step? 0 : 1;
  180. // Start job immediately: Start the prepare broadphase
  181. // Must be done under body lock protection since the order is body locks then broadphase mutex
  182. // If this is turned around the RemoveBody call will hang since it locks in that order
  183. step.mBroadPhasePrepare = inJobSystem->CreateJob("UpdateBroadPhasePrepare", cColorUpdateBroadPhasePrepare, [&context, &step]()
  184. {
  185. // Prepare the broadphase update
  186. step.mBroadPhaseUpdateState = context.mPhysicsSystem->mBroadPhase->UpdatePrepare();
  187. // Now the finalize can run (if other dependencies are met too)
  188. step.mUpdateBroadphaseFinalize.RemoveDependency();
  189. }, previous_step_dependency_count);
  190. // This job will find all collisions
  191. step.mBodyPairQueues.resize(max_concurrency);
  192. step.mMaxBodyPairsPerQueue = mPhysicsSettings.mMaxInFlightBodyPairs / max_concurrency;
  193. step.mActiveFindCollisionJobs = ~PhysicsUpdateContext::JobMask(0) >> (sizeof(PhysicsUpdateContext::JobMask) * 8 - num_find_collisions_jobs);
  194. step.mFindCollisions.resize(num_find_collisions_jobs);
  195. for (int i = 0; i < num_find_collisions_jobs; ++i)
  196. {
  197. step.mFindCollisions[i] = inJobSystem->CreateJob("FindCollisions", cColorFindCollisions, [&step, i]()
  198. {
  199. step.mContext->mPhysicsSystem->JobFindCollisions(&step, i);
  200. }, num_apply_gravity_jobs + num_determine_active_constraints_jobs + 1); // depends on: apply gravity, determine active constraints, finish building jobs
  201. }
  202. if (is_first_step)
  203. {
  204. #ifdef JPH_ENABLE_ASSERTS
  205. // Don't allow write operations to the active bodies list
  206. mBodyManager.SetActiveBodiesLocked(true);
  207. #endif
  208. // Store the number of active bodies at the start of the step
  209. step.mNumActiveBodiesAtStepStart = mBodyManager.GetNumActiveBodies();
  210. // Lock all constraints
  211. mConstraintManager.LockAllConstraints();
  212. // Allocate memory for storing the active constraints
  213. JPH_ASSERT(context.mActiveConstraints == nullptr);
  214. context.mActiveConstraints = static_cast<Constraint **>(inTempAllocator->Allocate(mConstraintManager.GetNumConstraints() * sizeof(Constraint *)));
  215. // Prepare contact buffer
  216. mContactManager.PrepareConstraintBuffer(&context);
  217. // Setup island builder
  218. mIslandBuilder.PrepareContactConstraints(mContactManager.GetMaxConstraints(), context.mTempAllocator);
  219. }
  220. // This job applies gravity to all active bodies
  221. step.mApplyGravity.resize(num_apply_gravity_jobs);
  222. for (int i = 0; i < num_apply_gravity_jobs; ++i)
  223. step.mApplyGravity[i] = inJobSystem->CreateJob("ApplyGravity", cColorApplyGravity, [&context, &step]()
  224. {
  225. context.mPhysicsSystem->JobApplyGravity(&context, &step);
  226. JobHandle::sRemoveDependencies(step.mFindCollisions);
  227. }, previous_step_dependency_count + num_step_listener_jobs); // depends on: step listeners
  228. // This job will setup velocity constraints for non-collision constraints
  229. step.mSetupVelocityConstraints = inJobSystem->CreateJob("SetupVelocityConstraints", cColorSetupVelocityConstraints, [&context, &step]()
  230. {
  231. context.mPhysicsSystem->JobSetupVelocityConstraints(context.mSubStepDeltaTime, &step);
  232. JobHandle::sRemoveDependencies(step.mSubSteps[0].mSolveVelocityConstraints);
  233. }, num_determine_active_constraints_jobs + 1); // depends on: determine active constraints, finish building jobs
  234. // This job will build islands from constraints
  235. step.mBuildIslandsFromConstraints = inJobSystem->CreateJob("BuildIslandsFromConstraints", cColorBuildIslandsFromConstraints, [&context, &step]()
  236. {
  237. context.mPhysicsSystem->JobBuildIslandsFromConstraints(&context, &step);
  238. step.mFinalizeIslands.RemoveDependency();
  239. }, num_determine_active_constraints_jobs + 1); // depends on: determine active constraints, finish building jobs
  240. // This job determines active constraints
  241. step.mDetermineActiveConstraints.resize(num_determine_active_constraints_jobs);
  242. for (int i = 0; i < num_determine_active_constraints_jobs; ++i)
  243. step.mDetermineActiveConstraints[i] = inJobSystem->CreateJob("DetermineActiveConstraints", cColorDetermineActiveConstraints, [&context, &step]()
  244. {
  245. context.mPhysicsSystem->JobDetermineActiveConstraints(&step);
  246. step.mSetupVelocityConstraints.RemoveDependency();
  247. step.mBuildIslandsFromConstraints.RemoveDependency();
  248. // Kick find collisions last as they will use up all CPU cores leaving no space for the previous 2 jobs
  249. JobHandle::sRemoveDependencies(step.mFindCollisions);
  250. }, previous_step_dependency_count + num_step_listener_jobs);
  251. // This job calls the step listeners
  252. step.mStepListeners.resize(num_step_listener_jobs);
  253. for (int i = 0; i < num_step_listener_jobs; ++i)
  254. step.mStepListeners[i] = inJobSystem->CreateJob("StepListeners", cColorStepListeners, [&context, &step]()
  255. {
  256. // Call the step listeners
  257. context.mPhysicsSystem->JobStepListeners(&step);
  258. // Kick apply gravity and determine active constraint jobs
  259. JobHandle::sRemoveDependencies(step.mApplyGravity);
  260. JobHandle::sRemoveDependencies(step.mDetermineActiveConstraints);
  261. });
  262. // Unblock the previous step
  263. if (!is_first_step)
  264. context.mSteps[step_idx - 1].mStartNextStep.RemoveDependency();
  265. // This job will finalize the simulation islands
  266. step.mFinalizeIslands = inJobSystem->CreateJob("FinalizeIslands", cColorFinalizeIslands, [&context, &step]()
  267. {
  268. // Validate that all find collision jobs have stopped
  269. JPH_ASSERT(step.mActiveFindCollisionJobs == 0);
  270. context.mPhysicsSystem->JobFinalizeIslands(&context);
  271. JobHandle::sRemoveDependencies(step.mSubSteps[0].mSolveVelocityConstraints);
  272. step.mBodySetIslandIndex.RemoveDependency();
  273. }, num_find_collisions_jobs + 2); // depends on: find collisions, build islands from constraints, finish building jobs
  274. // Unblock previous jobs (find collisions last since it will use up all CPU cores)
  275. step.mBuildIslandsFromConstraints.RemoveDependency();
  276. JobHandle::sRemoveDependencies(step.mFindCollisions);
  277. // This job will call the contact removed callbacks
  278. step.mContactRemovedCallbacks = inJobSystem->CreateJob("ContactRemovedCallbacks", cColorContactRemovedCallbacks, [&context, &step]()
  279. {
  280. context.mPhysicsSystem->JobContactRemovedCallbacks();
  281. if (step.mStartNextStep.IsValid())
  282. step.mStartNextStep.RemoveDependency();
  283. }, 1); // depends on the find ccd contacts of the last sub step
  284. // This job will set the island index on each body (only used for debug drawing purposes)
  285. // It will also delete any bodies that have been destroyed in the last frame
  286. step.mBodySetIslandIndex = inJobSystem->CreateJob("BodySetIslandIndex", cColorBodySetIslandIndex, [&context, &step]()
  287. {
  288. context.mPhysicsSystem->JobBodySetIslandIndex(&context);
  289. if (step.mStartNextStep.IsValid())
  290. step.mStartNextStep.RemoveDependency();
  291. }, 1); // depends on: finalize islands
  292. // Job to start the next collision step
  293. if (!is_last_step)
  294. {
  295. PhysicsUpdateContext::Step *next_step = &context.mSteps[step_idx + 1];
  296. step.mStartNextStep = inJobSystem->CreateJob("StartNextStep", cColorStartNextStep, [this, next_step]()
  297. {
  298. #ifdef _DEBUG
  299. // Validate that the cached bounds are correct
  300. mBodyManager.ValidateActiveBodyBounds();
  301. #endif // _DEBUG
  302. // Store the number of active bodies at the start of the step
  303. next_step->mNumActiveBodiesAtStepStart = mBodyManager.GetNumActiveBodies();
  304. // Clear the island builder
  305. TempAllocator *temp_allocator = next_step->mContext->mTempAllocator;
  306. mIslandBuilder.ResetIslands(temp_allocator);
  307. // Setup island builder
  308. mIslandBuilder.PrepareContactConstraints(mContactManager.GetMaxConstraints(), temp_allocator);
  309. // Restart the contact manager
  310. mContactManager.RecycleConstraintBuffer();
  311. // Kick the jobs of the next step (in the same order as the first step)
  312. next_step->mBroadPhasePrepare.RemoveDependency();
  313. JobHandle::sRemoveDependencies(next_step->mApplyGravity);
  314. JobHandle::sRemoveDependencies(next_step->mDetermineActiveConstraints);
  315. }, max_concurrency + 3); // depends on: solve position constraints of the last step, body set island index, contact removed callbacks, finish building the previous step
  316. }
  317. // Create solve jobs for each of the integration sub steps
  318. for (int sub_step_idx = 0; sub_step_idx < inIntegrationSubSteps; ++sub_step_idx)
  319. {
  320. bool is_first_sub_step = sub_step_idx == 0;
  321. bool is_last_sub_step = sub_step_idx == inIntegrationSubSteps - 1;
  322. PhysicsUpdateContext::SubStep &sub_step = step.mSubSteps[sub_step_idx];
  323. sub_step.mStep = &step;
  324. sub_step.mIsFirst = is_first_sub_step;
  325. sub_step.mIsLast = is_last_sub_step;
  326. sub_step.mIsLastOfAll = is_last_step && is_last_sub_step;
  327. // This job will solve the velocity constraints
  328. int num_dependencies_solve_velocity_constraints = is_first_sub_step? 3 : 2; // in first sub step depends on: finalize islands, setup velocity constraints, in later sub steps depends on: previous sub step finished. For both: finish building jobs.
  329. sub_step.mSolveVelocityConstraints.resize(max_concurrency);
  330. for (int i = 0; i < max_concurrency; ++i)
  331. sub_step.mSolveVelocityConstraints[i] = inJobSystem->CreateJob("SolveVelocityConstraints", cColorSolveVelocityConstraints, [&context, &sub_step]()
  332. {
  333. context.mPhysicsSystem->JobSolveVelocityConstraints(&context, &sub_step);
  334. sub_step.mIntegrateVelocity.RemoveDependency();
  335. }, num_dependencies_solve_velocity_constraints);
  336. // Unblock previous jobs
  337. if (is_first_sub_step)
  338. {
  339. step.mFinalizeIslands.RemoveDependency();
  340. step.mSetupVelocityConstraints.RemoveDependency();
  341. }
  342. else
  343. {
  344. step.mSubSteps[sub_step_idx - 1].mStartNextSubStep.RemoveDependency();
  345. }
  346. // This job will update the positions of all active bodies
  347. int num_dependencies_integrate_velocities = is_first_sub_step? 2 + max_concurrency : 1 + max_concurrency; // depends on: broadphase update finalize in first step, solve velocity constraints in all steps. For both: finish building jobs.
  348. sub_step.mIntegrateVelocity = inJobSystem->CreateJob("IntegrateVelocity", cColorIntegrateVelocity, [&context, &sub_step]()
  349. {
  350. context.mPhysicsSystem->JobIntegrateVelocity(&context, &sub_step);
  351. sub_step.mResolveCCDContacts.RemoveDependency();
  352. }, num_dependencies_integrate_velocities);
  353. // Unblock previous jobs
  354. if (is_first_sub_step)
  355. step.mUpdateBroadphaseFinalize.RemoveDependency();
  356. JobHandle::sRemoveDependencies(sub_step.mSolveVelocityConstraints);
  357. // This job will update the positions and velocities for all bodies that need continuous collision detection
  358. sub_step.mResolveCCDContacts = inJobSystem->CreateJob("ResolveCCDContacts", cColorResolveCCDContacts, [&context, &sub_step]()
  359. {
  360. context.mPhysicsSystem->JobResolveCCDContacts(&context, &sub_step);
  361. JobHandle::sRemoveDependencies(sub_step.mSolvePositionConstraints);
  362. }, 2); // depends on: integrate velocities, detect ccd contacts (added dynamically), finish building jobs.
  363. // Unblock previous job
  364. sub_step.mIntegrateVelocity.RemoveDependency();
  365. // Fixes up drift in positions and updates the broadphase with new body positions
  366. sub_step.mSolvePositionConstraints.resize(max_concurrency);
  367. for (int i = 0; i < max_concurrency; ++i)
  368. sub_step.mSolvePositionConstraints[i] = inJobSystem->CreateJob("SolvePositionConstraints", cColorSolvePositionConstraints, [&context, &sub_step]()
  369. {
  370. context.mPhysicsSystem->JobSolvePositionConstraints(&context, &sub_step);
  371. // Kick the next sub step
  372. if (sub_step.mStartNextSubStep.IsValid())
  373. sub_step.mStartNextSubStep.RemoveDependency();
  374. }, 2); // depends on: resolve ccd contacts, finish building jobs.
  375. // Unblock previous job.
  376. sub_step.mResolveCCDContacts.RemoveDependency();
  377. // This job starts the next sub step
  378. if (!is_last_sub_step)
  379. {
  380. PhysicsUpdateContext::SubStep &next_sub_step = step.mSubSteps[sub_step_idx + 1];
  381. sub_step.mStartNextSubStep = inJobSystem->CreateJob("StartNextSubStep", cColorStartNextSubStep, [&next_sub_step]()
  382. {
  383. // Kick velocity constraint solving for the next sub step
  384. JobHandle::sRemoveDependencies(next_sub_step.mSolveVelocityConstraints);
  385. }, max_concurrency + 1); // depends on: solve position constraints, finish building jobs.
  386. }
  387. else
  388. sub_step.mStartNextSubStep = step.mStartNextStep;
  389. // Unblock previous jobs
  390. JobHandle::sRemoveDependencies(sub_step.mSolvePositionConstraints);
  391. }
  392. }
  393. }
  394. // Build the list of jobs to wait for
  395. JobSystem::Barrier *barrier = context.mBarrier;
  396. {
  397. JPH_PROFILE("Build job barrier");
  398. StaticArray<JobHandle, cMaxPhysicsJobs> handles;
  399. for (PhysicsUpdateContext::Step &step : context.mSteps)
  400. {
  401. if (step.mBroadPhasePrepare.IsValid())
  402. handles.push_back(step.mBroadPhasePrepare);
  403. for (JobHandle &h : step.mStepListeners)
  404. handles.push_back(h);
  405. for (JobHandle &h : step.mDetermineActiveConstraints)
  406. handles.push_back(h);
  407. for (JobHandle &h : step.mApplyGravity)
  408. handles.push_back(h);
  409. for (JobHandle &h : step.mFindCollisions)
  410. handles.push_back(h);
  411. if (step.mUpdateBroadphaseFinalize.IsValid())
  412. handles.push_back(step.mUpdateBroadphaseFinalize);
  413. handles.push_back(step.mSetupVelocityConstraints);
  414. handles.push_back(step.mBuildIslandsFromConstraints);
  415. handles.push_back(step.mFinalizeIslands);
  416. handles.push_back(step.mBodySetIslandIndex);
  417. for (PhysicsUpdateContext::SubStep &sub_step : step.mSubSteps)
  418. {
  419. for (JobHandle &h : sub_step.mSolveVelocityConstraints)
  420. handles.push_back(h);
  421. handles.push_back(sub_step.mIntegrateVelocity);
  422. handles.push_back(sub_step.mResolveCCDContacts);
  423. for (JobHandle &h : sub_step.mSolvePositionConstraints)
  424. handles.push_back(h);
  425. if (sub_step.mStartNextSubStep.IsValid())
  426. handles.push_back(sub_step.mStartNextSubStep);
  427. }
  428. handles.push_back(step.mContactRemovedCallbacks);
  429. }
  430. barrier->AddJobs(handles.data(), handles.size());
  431. }
  432. // Wait until all jobs finish
  433. // Note we don't just wait for the last job. If we would and another job
  434. // would be scheduled in between there is the possibility of a deadlock.
  435. // The other job could try to e.g. add/remove a body which would try to
  436. // lock a body mutex while this thread has already locked the mutex
  437. inJobSystem->WaitForJobs(barrier);
  438. // We're done with the barrier for this update
  439. inJobSystem->DestroyBarrier(barrier);
  440. #ifdef _DEBUG
  441. // Validate that the cached bounds are correct
  442. mBodyManager.ValidateActiveBodyBounds();
  443. #endif // _DEBUG
  444. // Clear the island builder
  445. mIslandBuilder.ResetIslands(inTempAllocator);
  446. // Clear the contact manager
  447. mContactManager.FinishConstraintBuffer();
  448. // Free active constraints
  449. inTempAllocator->Free(context.mActiveConstraints, mConstraintManager.GetNumConstraints() * sizeof(Constraint *));
  450. context.mActiveConstraints = nullptr;
  451. // Free body pairs
  452. inTempAllocator->Free(context.mBodyPairs, sizeof(BodyPair) * mPhysicsSettings.mMaxInFlightBodyPairs);
  453. context.mBodyPairs = nullptr;
  454. // Unlock the broadphase
  455. mBroadPhase->UnlockModifications();
  456. // Unlock all constraints
  457. mConstraintManager.UnlockAllConstraints();
  458. #ifdef JPH_ENABLE_ASSERTS
  459. // Allow write operations to the active bodies list
  460. mBodyManager.SetActiveBodiesLocked(false);
  461. #endif
  462. // Unlock all bodies
  463. mBodyManager.UnlockAllBodies();
  464. // Unlock step listeners
  465. mStepListenersMutex.unlock();
  466. }
  467. void PhysicsSystem::JobStepListeners(PhysicsUpdateContext::Step *ioStep)
  468. {
  469. #ifdef JPH_ENABLE_ASSERTS
  470. // Read positions (broadphase updates concurrently so we can't write), read/write velocities
  471. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  472. // Can activate bodies only (we cache the amount of active bodies at the beginning of the step in mNumActiveBodiesAtStepStart so we cannot deactivate here)
  473. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  474. #endif
  475. float step_time = ioStep->mContext->mStepDeltaTime;
  476. uint32 batch_size = mPhysicsSettings.mStepListenersBatchSize;
  477. for (;;)
  478. {
  479. // Get the start of a new batch
  480. uint32 batch = ioStep->mStepListenerReadIdx.fetch_add(batch_size);
  481. if (batch >= mStepListeners.size())
  482. break;
  483. // Call the listeners
  484. for (uint32 i = batch, i_end = min((uint32)mStepListeners.size(), batch + batch_size); i < i_end; ++i)
  485. mStepListeners[i]->OnStep(step_time, *this);
  486. }
  487. }
  488. void PhysicsSystem::JobDetermineActiveConstraints(PhysicsUpdateContext::Step *ioStep)
  489. {
  490. #ifdef JPH_ENABLE_ASSERTS
  491. // No body access
  492. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  493. #endif
  494. uint32 num_constraints = mConstraintManager.GetNumConstraints();
  495. uint32 num_active_constraints;
  496. Constraint **active_constraints = (Constraint **)JPH_STACK_ALLOC(cDetermineActiveConstraintsBatchSize * sizeof(Constraint *));
  497. for (;;)
  498. {
  499. // Atomically fetch a batch of constraints
  500. uint32 constraint_idx = ioStep->mConstraintReadIdx.fetch_add(cDetermineActiveConstraintsBatchSize);
  501. if (constraint_idx >= num_constraints)
  502. break;
  503. // Calculate the end of the batch
  504. uint32 constraint_idx_end = min(num_constraints, constraint_idx + cDetermineActiveConstraintsBatchSize);
  505. // Store the active constraints at the start of the step (bodies get activated during the step which in turn may activate constraints leading to an inconsistent shapshot)
  506. mConstraintManager.GetActiveConstraints(constraint_idx, constraint_idx_end, active_constraints, num_active_constraints);
  507. // Copy the block of active constraints to the global list of active constraints
  508. if (num_active_constraints > 0)
  509. {
  510. uint32 active_constraint_idx = ioStep->mNumActiveConstraints.fetch_add(num_active_constraints);
  511. memcpy(ioStep->mContext->mActiveConstraints + active_constraint_idx, active_constraints, num_active_constraints * sizeof(Constraint *));
  512. }
  513. }
  514. }
  515. void PhysicsSystem::JobApplyGravity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  516. {
  517. #ifdef JPH_ENABLE_ASSERTS
  518. // We update velocities and need the rotation to do so
  519. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  520. #endif
  521. // Get list of active bodies that we had at the start of the physics update.
  522. // Any body that is activated as part of the simulation step does not receive gravity this frame.
  523. // Note that bodies may be activated during this job but not deactivated, this means that only elements
  524. // will be added to the array. Since the array is made to not reallocate, this is a safe operation.
  525. const BodyID *active_bodies = mBodyManager.GetActiveBodiesUnsafe();
  526. uint32 num_active_bodies_at_step_start = ioStep->mNumActiveBodiesAtStepStart;
  527. // Fetch delta time once outside the loop
  528. float delta_time = ioContext->mSubStepDeltaTime;
  529. // Update velocities from forces
  530. for (;;)
  531. {
  532. // Atomically fetch a batch of bodies
  533. uint32 active_body_idx = ioStep->mApplyGravityReadIdx.fetch_add(cApplyGravityBatchSize);
  534. if (active_body_idx >= num_active_bodies_at_step_start)
  535. break;
  536. // Calculate the end of the batch
  537. uint32 active_body_idx_end = min(num_active_bodies_at_step_start, active_body_idx + cApplyGravityBatchSize);
  538. // Process the batch
  539. while (active_body_idx < active_body_idx_end)
  540. {
  541. Body &body = mBodyManager.GetBody(active_bodies[active_body_idx]);
  542. if (body.IsDynamic())
  543. body.GetMotionProperties()->ApplyForceTorqueAndDragInternal(body.GetRotation(), mGravity, delta_time);
  544. active_body_idx++;
  545. }
  546. }
  547. }
  548. void PhysicsSystem::JobSetupVelocityConstraints(float inDeltaTime, PhysicsUpdateContext::Step *ioStep)
  549. {
  550. #ifdef JPH_ENABLE_ASSERTS
  551. // We only read positions
  552. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
  553. #endif
  554. mConstraintManager.SetupVelocityConstraints(ioStep->mContext->mActiveConstraints, ioStep->mNumActiveConstraints, inDeltaTime);
  555. }
  556. void PhysicsSystem::JobBuildIslandsFromConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  557. {
  558. #ifdef JPH_ENABLE_ASSERTS
  559. // We read constraints and positions
  560. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
  561. // Can only activate bodies
  562. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  563. #endif
  564. // Prepare the island builder
  565. mIslandBuilder.PrepareNonContactConstraints(ioStep->mNumActiveConstraints, ioContext->mTempAllocator);
  566. // Build the islands
  567. mConstraintManager.BuildIslands(ioStep->mContext->mActiveConstraints, ioStep->mNumActiveConstraints, mIslandBuilder, mBodyManager);
  568. }
  569. void PhysicsSystem::TrySpawnJobFindCollisions(PhysicsUpdateContext::Step *ioStep)
  570. {
  571. // Get how many jobs we can spawn and check if we can spawn more
  572. uint max_jobs = ioStep->mBodyPairQueues.size();
  573. if (CountBits(ioStep->mActiveFindCollisionJobs) >= max_jobs)
  574. return;
  575. // Count how many body pairs we have waiting
  576. uint32 num_body_pairs = 0;
  577. for (const PhysicsUpdateContext::BodyPairQueue &queue : ioStep->mBodyPairQueues)
  578. num_body_pairs += queue.mWriteIdx - queue.mReadIdx;
  579. // Count how many active bodies we have waiting
  580. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies() - ioStep->mActiveBodyReadIdx;
  581. // Calculate how many jobs we would like
  582. uint desired_num_jobs = min((num_body_pairs + cNarrowPhaseBatchSize - 1) / cNarrowPhaseBatchSize + (num_active_bodies + cActiveBodiesBatchSize - 1) / cActiveBodiesBatchSize, max_jobs);
  583. for (;;)
  584. {
  585. // Get the bit mask of active jobs and see if we can spawn more
  586. PhysicsUpdateContext::JobMask current_active_jobs = ioStep->mActiveFindCollisionJobs;
  587. if (CountBits(current_active_jobs) >= desired_num_jobs)
  588. break;
  589. // Loop through all possible job indices
  590. for (uint job_index = 0; job_index < max_jobs; ++job_index)
  591. {
  592. // Test if it has been started
  593. PhysicsUpdateContext::JobMask job_mask = PhysicsUpdateContext::JobMask(1) << job_index;
  594. if ((current_active_jobs & job_mask) == 0)
  595. {
  596. // Try to claim the job index
  597. PhysicsUpdateContext::JobMask prev_value = ioStep->mActiveFindCollisionJobs.fetch_or(job_mask);
  598. if ((prev_value & job_mask) == 0)
  599. {
  600. // Add dependencies from the find collisions job to the next jobs
  601. ioStep->mUpdateBroadphaseFinalize.AddDependency();
  602. ioStep->mFinalizeIslands.AddDependency();
  603. // Start the job
  604. JobHandle job = ioStep->mContext->mJobSystem->CreateJob("FindCollisions", cColorFindCollisions, [step = ioStep, job_index]()
  605. {
  606. step->mContext->mPhysicsSystem->JobFindCollisions(step, job_index);
  607. });
  608. // Add the job to the job barrier so the main updating thread can execute the job too
  609. ioStep->mContext->mBarrier->AddJob(job);
  610. // Spawn only 1 extra job at a time
  611. return;
  612. }
  613. }
  614. }
  615. }
  616. }
  617. void PhysicsSystem::JobFindCollisions(PhysicsUpdateContext::Step *ioStep, int inJobIndex)
  618. {
  619. #ifdef JPH_ENABLE_ASSERTS
  620. // We only read positions
  621. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
  622. #endif
  623. // Determine initial queue to read pairs from if no broadphase work can be done
  624. // (always start looking at results from the next job)
  625. int read_queue_idx = (inJobIndex + 1) % ioStep->mBodyPairQueues.size();
  626. for (;;)
  627. {
  628. // Check if there are active bodies to be processed
  629. uint32 active_bodies_read_idx = ioStep->mActiveBodyReadIdx;
  630. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies();
  631. if (active_bodies_read_idx < num_active_bodies)
  632. {
  633. // Take a batch of active bodies
  634. uint32 active_bodies_read_idx_end = min(num_active_bodies, active_bodies_read_idx + cActiveBodiesBatchSize);
  635. if (ioStep->mActiveBodyReadIdx.compare_exchange_strong(active_bodies_read_idx, active_bodies_read_idx_end))
  636. {
  637. // Callback when a new body pair is found
  638. class MyBodyPairCallback : public BodyPairCollector
  639. {
  640. public:
  641. // Constructor
  642. MyBodyPairCallback(PhysicsUpdateContext::Step *inStep, int inJobIndex) :
  643. mStep(inStep),
  644. mJobIndex(inJobIndex)
  645. {
  646. }
  647. // Callback function when a body pair is found
  648. virtual void AddHit(const BodyPair &inPair) override
  649. {
  650. // Check if we have space in our write queue
  651. PhysicsUpdateContext::BodyPairQueue &queue = mStep->mBodyPairQueues[mJobIndex];
  652. uint32 body_pairs_in_queue = queue.mWriteIdx - queue.mReadIdx;
  653. if (body_pairs_in_queue >= mStep->mMaxBodyPairsPerQueue)
  654. {
  655. // Buffer full, process the pair now
  656. mStep->mContext->mPhysicsSystem->ProcessBodyPair(inPair);
  657. }
  658. else
  659. {
  660. // Store the pair in our own queue
  661. mStep->mContext->mBodyPairs[mJobIndex * mStep->mMaxBodyPairsPerQueue + queue.mWriteIdx % mStep->mMaxBodyPairsPerQueue] = inPair;
  662. ++queue.mWriteIdx;
  663. }
  664. }
  665. private:
  666. PhysicsUpdateContext::Step * mStep;
  667. int mJobIndex;
  668. };
  669. MyBodyPairCallback add_pair(ioStep, inJobIndex);
  670. // Copy active bodies to temporary array, broadphase will reorder them
  671. uint32 batch_size = active_bodies_read_idx_end - active_bodies_read_idx;
  672. BodyID *active_bodies = (BodyID *)JPH_STACK_ALLOC(batch_size * sizeof(BodyID));
  673. memcpy(active_bodies, mBodyManager.GetActiveBodiesUnsafe() + active_bodies_read_idx, batch_size * sizeof(BodyID));
  674. // Find pairs in the broadphase
  675. mBroadPhase->FindCollidingPairs(active_bodies, batch_size, mPhysicsSettings.mSpeculativeContactDistance, mBroadPhaseLayerPairFilter, mObjectLayerPairFilter, add_pair);
  676. // Check if we have enough pairs in the buffer to start a new job
  677. PhysicsUpdateContext::BodyPairQueue &queue = ioStep->mBodyPairQueues[inJobIndex];
  678. uint32 body_pairs_in_queue = queue.mWriteIdx - queue.mReadIdx;
  679. if (body_pairs_in_queue >= cNarrowPhaseBatchSize)
  680. TrySpawnJobFindCollisions(ioStep);
  681. }
  682. }
  683. else
  684. {
  685. // Lockless loop to get the next body pair from the pairs buffer
  686. PhysicsUpdateContext *context = ioStep->mContext;
  687. int first_read_queue_idx = read_queue_idx;
  688. for (;;)
  689. {
  690. PhysicsUpdateContext::BodyPairQueue &queue = ioStep->mBodyPairQueues[read_queue_idx];
  691. // Get the next pair to process
  692. uint32 pair_idx = queue.mReadIdx;
  693. // If the pair hasn't been written yet
  694. if (pair_idx >= queue.mWriteIdx)
  695. {
  696. // Go to the next queue
  697. read_queue_idx = (read_queue_idx + 1) % ioStep->mBodyPairQueues.size();
  698. // If we're back at the first queue, we've looked at all of them and found nothing
  699. if (read_queue_idx == first_read_queue_idx)
  700. {
  701. // Mark this job as inactive
  702. ioStep->mActiveFindCollisionJobs.fetch_and(~PhysicsUpdateContext::JobMask(1 << inJobIndex));
  703. // Trigger the next jobs
  704. ioStep->mUpdateBroadphaseFinalize.RemoveDependency();
  705. ioStep->mFinalizeIslands.RemoveDependency();
  706. return;
  707. }
  708. // Try again reading from the next queue
  709. continue;
  710. }
  711. // Copy the body pair out of the buffer
  712. BodyPair bp = context->mBodyPairs[read_queue_idx * ioStep->mMaxBodyPairsPerQueue + pair_idx % ioStep->mMaxBodyPairsPerQueue];
  713. // Mark this pair as taken
  714. if (queue.mReadIdx.compare_exchange_strong(pair_idx, pair_idx + 1))
  715. {
  716. // Process the actual body pair
  717. ProcessBodyPair(bp);
  718. break;
  719. }
  720. }
  721. }
  722. }
  723. }
  724. void PhysicsSystem::ProcessBodyPair(const BodyPair &inBodyPair)
  725. {
  726. JPH_PROFILE_FUNCTION();
  727. #ifdef JPH_ENABLE_ASSERTS
  728. // We read positions and read velocities (for elastic collisions)
  729. BodyAccess::Grant grant(BodyAccess::EAccess::Read, BodyAccess::EAccess::Read);
  730. // Can only activate bodies
  731. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  732. #endif
  733. // Fetch body pair
  734. Body *body1 = &mBodyManager.GetBody(inBodyPair.mBodyA);
  735. Body *body2 = &mBodyManager.GetBody(inBodyPair.mBodyB);
  736. JPH_ASSERT(body1->IsActive());
  737. // Ensure that body1 is dynamic, this ensures that we do the collision detection in the space of a moving body, which avoids accuracy problems when testing a very large static object against a small dynamic object
  738. // Ensure that body1 id < body2 id for dynamic vs dynamic
  739. if (!body1->IsDynamic() || (body2->IsDynamic() && inBodyPair.mBodyB < inBodyPair.mBodyA))
  740. swap(body1, body2);
  741. JPH_ASSERT(body1->IsDynamic());
  742. // Check if the contact points from the previous frame are reusable and if so copy them
  743. bool pair_handled = false, contact_found = false;
  744. if (mPhysicsSettings.mUseBodyPairContactCache && !(body1->IsCollisionCacheInvalid() || body2->IsCollisionCacheInvalid()))
  745. mContactManager.GetContactsFromCache(*body1, *body2, pair_handled, contact_found);
  746. // If the cache hasn't handled this body pair do actual collision detection
  747. if (!pair_handled)
  748. {
  749. // Create entry in the cache for this body pair
  750. // Needs to happen irrespective if we found a collision or not (we want to remember that no collision was found too)
  751. ContactConstraintManager::BodyPairHandle body_pair_handle = mContactManager.AddBodyPair(*body1, *body2);
  752. if (body_pair_handle == nullptr)
  753. return; // Out of cache space
  754. // Create the query settings
  755. CollideShapeSettings settings;
  756. settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
  757. settings.mActiveEdgeMode = mPhysicsSettings.mCheckActiveEdges? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
  758. settings.mMaxSeparationDistance = mPhysicsSettings.mSpeculativeContactDistance;
  759. settings.mActiveEdgeMovementDirection = body1->GetLinearVelocity() - body2->GetLinearVelocity();
  760. if (mPhysicsSettings.mUseManifoldReduction)
  761. {
  762. // Version WITH contact manifold reduction
  763. class MyManifold : public ContactManifold
  764. {
  765. public:
  766. Vec3 mFirstWorldSpaceNormal;
  767. };
  768. // A temporary structure that allows us to keep track of the all manifolds between this body pair
  769. using Manifolds = StaticArray<MyManifold, 32>;
  770. // Create collector
  771. class ReductionCollideShapeCollector : public CollideShapeCollector
  772. {
  773. public:
  774. ReductionCollideShapeCollector(PhysicsSystem *inSystem, const Body *inBody1, const Body *inBody2) :
  775. mSystem(inSystem),
  776. mBody1(inBody1),
  777. mBody2(inBody2)
  778. {
  779. }
  780. virtual void AddHit(const CollideShapeResult &inResult) override
  781. {
  782. // Body 1 should always be dynamic, body 2 may be static / kinematic
  783. JPH_ASSERT(mBody1->IsDynamic());
  784. JPH_ASSERT(!ShouldEarlyOut());
  785. // Test if we want to accept this hit
  786. if (mValidateBodyPair)
  787. {
  788. switch (mSystem->mContactManager.ValidateContactPoint(*mBody1, *mBody2, inResult))
  789. {
  790. case ValidateResult::AcceptContact:
  791. // We're just accepting this one, nothing to do
  792. break;
  793. case ValidateResult::AcceptAllContactsForThisBodyPair:
  794. // Accept and stop calling the validate callback
  795. mValidateBodyPair = false;
  796. break;
  797. case ValidateResult::RejectContact:
  798. // Skip this contact
  799. return;
  800. case ValidateResult::RejectAllContactsForThisBodyPair:
  801. // Skip this and early out
  802. ForceEarlyOut();
  803. return;
  804. }
  805. }
  806. // Count amount of manifolds before reduction
  807. JPH_IF_STAT_COLLECTOR(++mSystem->mManifoldsBeforeReduction;)
  808. // Calculate normal
  809. Vec3 world_space_normal = inResult.mPenetrationAxis.Normalized();
  810. // Check if we can add it to an existing manifold
  811. Manifolds::iterator manifold;
  812. float contact_normal_cos_max_delta_rot = mSystem->mPhysicsSettings.mContactNormalCosMaxDeltaRotation;
  813. for (manifold = mManifolds.begin(); manifold != mManifolds.end(); ++manifold)
  814. if (world_space_normal.Dot(manifold->mFirstWorldSpaceNormal) >= contact_normal_cos_max_delta_rot)
  815. {
  816. // Update average normal
  817. manifold->mWorldSpaceNormal += world_space_normal;
  818. manifold->mPenetrationDepth = max(manifold->mPenetrationDepth, inResult.mPenetrationDepth);
  819. break;
  820. }
  821. if (manifold == mManifolds.end())
  822. {
  823. // Check if array is full
  824. if (mManifolds.size() == mManifolds.capacity())
  825. {
  826. // Full, find manifold with least amount of penetration
  827. manifold = mManifolds.begin();
  828. for (Manifolds::iterator m = mManifolds.begin() + 1; m < mManifolds.end(); ++m)
  829. if (m->mPenetrationDepth < manifold->mPenetrationDepth)
  830. manifold = m;
  831. // If this contacts penetration is smaller than the smallest manifold, we skip this contact
  832. if (inResult.mPenetrationDepth < manifold->mPenetrationDepth)
  833. return;
  834. // Replace the manifold
  835. *manifold = { { world_space_normal, inResult.mPenetrationDepth, inResult.mSubShapeID1, inResult.mSubShapeID2, { }, { } }, world_space_normal };
  836. }
  837. else
  838. {
  839. // Not full, create new manifold
  840. mManifolds.push_back({ { world_space_normal, inResult.mPenetrationDepth, inResult.mSubShapeID1, inResult.mSubShapeID2, { }, { } }, world_space_normal });
  841. manifold = mManifolds.end() - 1;
  842. // Count manifolds after reduction
  843. JPH_IF_STAT_COLLECTOR(mSystem->mManifoldsAfterReduction++;)
  844. }
  845. }
  846. // Determine contact points
  847. const PhysicsSettings &settings = mSystem->mPhysicsSettings;
  848. ManifoldBetweenTwoFaces(inResult.mContactPointOn1, inResult.mContactPointOn2, inResult.mPenetrationAxis, Square(settings.mSpeculativeContactDistance) + settings.mManifoldToleranceSq, inResult.mShape1Face, inResult.mShape2Face, manifold->mWorldSpaceContactPointsOn1, manifold->mWorldSpaceContactPointsOn2);
  849. // Prune if we have more than 32 points (this means we could run out of space in the next iteration)
  850. if (manifold->mWorldSpaceContactPointsOn1.size() > 32)
  851. PruneContactPoints(mBody1->GetCenterOfMassPosition(), manifold->mFirstWorldSpaceNormal, manifold->mWorldSpaceContactPointsOn1, manifold->mWorldSpaceContactPointsOn2);
  852. }
  853. PhysicsSystem * mSystem;
  854. const Body * mBody1;
  855. const Body * mBody2;
  856. bool mValidateBodyPair = true;
  857. Manifolds mManifolds;
  858. };
  859. ReductionCollideShapeCollector collector(this, body1, body2);
  860. // Perform collision detection between the two shapes
  861. SubShapeIDCreator part1, part2;
  862. CollisionDispatch::sCollideShapeVsShape(body1->GetShape(), body2->GetShape(), Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), body1->GetCenterOfMassTransform(), body2->GetCenterOfMassTransform(), part1, part2, settings, collector);
  863. // Add the contacts
  864. for (ContactManifold &manifold : collector.mManifolds)
  865. {
  866. // Normalize the normal (is a sum of all normals from merged manifolds)
  867. manifold.mWorldSpaceNormal = manifold.mWorldSpaceNormal.Normalized();
  868. // If we still have too many points, prune them now
  869. if (manifold.mWorldSpaceContactPointsOn1.size() > 4)
  870. PruneContactPoints(body1->GetCenterOfMassPosition(), manifold.mWorldSpaceNormal, manifold.mWorldSpaceContactPointsOn1, manifold.mWorldSpaceContactPointsOn2);
  871. // Actually add the contact points to the manager
  872. mContactManager.AddContactConstraint(body_pair_handle, *body1, *body2, manifold);
  873. }
  874. contact_found = !collector.mManifolds.empty();
  875. }
  876. else
  877. {
  878. // Version WITHOUT contact manifold reduction
  879. // Create collector
  880. class NonReductionCollideShapeCollector : public CollideShapeCollector
  881. {
  882. public:
  883. NonReductionCollideShapeCollector(PhysicsSystem *inSystem, Body *inBody1, Body *inBody2, const ContactConstraintManager::BodyPairHandle &inPairHandle) :
  884. mSystem(inSystem),
  885. mBody1(inBody1),
  886. mBody2(inBody2),
  887. mBodyPairHandle(inPairHandle)
  888. {
  889. }
  890. virtual void AddHit(const CollideShapeResult &inResult) override
  891. {
  892. // Body 1 should always be dynamic, body 2 may be static / kinematic
  893. JPH_ASSERT(mBody1->IsDynamic());
  894. JPH_ASSERT(!ShouldEarlyOut());
  895. // Test if we want to accept this hit
  896. if (mValidateBodyPair)
  897. {
  898. switch (mSystem->mContactManager.ValidateContactPoint(*mBody1, *mBody2, inResult))
  899. {
  900. case ValidateResult::AcceptContact:
  901. // We're just accepting this one, nothing to do
  902. break;
  903. case ValidateResult::AcceptAllContactsForThisBodyPair:
  904. // Accept and stop calling the validate callback
  905. mValidateBodyPair = false;
  906. break;
  907. case ValidateResult::RejectContact:
  908. // Skip this contact
  909. return;
  910. case ValidateResult::RejectAllContactsForThisBodyPair:
  911. // Skip this and early out
  912. ForceEarlyOut();
  913. return;
  914. }
  915. }
  916. // Determine contact points
  917. ContactManifold manifold;
  918. const PhysicsSettings &settings = mSystem->mPhysicsSettings;
  919. ManifoldBetweenTwoFaces(inResult.mContactPointOn1, inResult.mContactPointOn2, inResult.mPenetrationAxis, Square(settings.mSpeculativeContactDistance) + settings.mManifoldToleranceSq, inResult.mShape1Face, inResult.mShape2Face, manifold.mWorldSpaceContactPointsOn1, manifold.mWorldSpaceContactPointsOn2);
  920. // Calculate normal
  921. manifold.mWorldSpaceNormal = inResult.mPenetrationAxis.Normalized();
  922. // Store penetration depth
  923. manifold.mPenetrationDepth = inResult.mPenetrationDepth;
  924. // Prune if we have more than 4 points
  925. if (manifold.mWorldSpaceContactPointsOn1.size() > 4)
  926. PruneContactPoints(mBody1->GetCenterOfMassPosition(), manifold.mWorldSpaceNormal, manifold.mWorldSpaceContactPointsOn1, manifold.mWorldSpaceContactPointsOn2);
  927. // Set other properties
  928. manifold.mSubShapeID1 = inResult.mSubShapeID1;
  929. manifold.mSubShapeID2 = inResult.mSubShapeID2;
  930. // Actually add the contact points to the manager
  931. mSystem->mContactManager.AddContactConstraint(mBodyPairHandle, *mBody1, *mBody2, manifold);
  932. // Mark contact found
  933. mContactFound = true;
  934. }
  935. PhysicsSystem * mSystem;
  936. Body * mBody1;
  937. Body * mBody2;
  938. ContactConstraintManager::BodyPairHandle mBodyPairHandle;
  939. bool mValidateBodyPair = true;
  940. bool mContactFound = false;
  941. };
  942. NonReductionCollideShapeCollector collector(this, body1, body2, body_pair_handle);
  943. // Perform collision detection between the two shapes
  944. SubShapeIDCreator part1, part2;
  945. CollisionDispatch::sCollideShapeVsShape(body1->GetShape(), body2->GetShape(), Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), body1->GetCenterOfMassTransform(), body2->GetCenterOfMassTransform(), part1, part2, settings, collector);
  946. contact_found = collector.mContactFound;
  947. }
  948. }
  949. // If an actual contact is present we need to do some extra work
  950. if (contact_found)
  951. {
  952. // Wake up sleeping bodies
  953. BodyID body_ids[2];
  954. int num_bodies = 0;
  955. if (body1->IsDynamic() && !body1->IsActive())
  956. body_ids[num_bodies++] = body1->GetID();
  957. if (body2->IsDynamic() && !body2->IsActive())
  958. body_ids[num_bodies++] = body2->GetID();
  959. if (num_bodies > 0)
  960. mBodyManager.ActivateBodies(body_ids, num_bodies);
  961. // Link the two bodies
  962. mIslandBuilder.LinkBodies(body1->GetIndexInActiveBodiesInternal(), body2->GetIndexInActiveBodiesInternal());
  963. }
  964. }
  965. void PhysicsSystem::JobFinalizeIslands(PhysicsUpdateContext *ioContext)
  966. {
  967. #ifdef JPH_ENABLE_ASSERTS
  968. // We only touch island data
  969. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  970. #endif
  971. // Finish collecting the islands, at this point the active body list doesn't change so it's safe to access
  972. mIslandBuilder.Finalize(mBodyManager.GetActiveBodiesUnsafe(), mBodyManager.GetNumActiveBodies(), mContactManager.GetNumConstraints(), ioContext->mTempAllocator);
  973. }
  974. void PhysicsSystem::JobBodySetIslandIndex(PhysicsUpdateContext *ioContext)
  975. {
  976. #ifdef JPH_ENABLE_ASSERTS
  977. // We only touch island data
  978. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  979. #endif
  980. // Loop through the result and tag all bodies with an island index
  981. for (uint32 island_idx = 0, n = mIslandBuilder.GetNumIslands(); island_idx < n; ++island_idx)
  982. {
  983. BodyID *body_start, *body_end;
  984. mIslandBuilder.GetBodiesInIsland(island_idx, body_start, body_end);
  985. for (const BodyID *body = body_start; body < body_end; ++body)
  986. mBodyManager.GetBody(*body).GetMotionProperties()->SetIslandIndexInternal(island_idx);
  987. }
  988. }
  989. void PhysicsSystem::JobSolveVelocityConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::SubStep *ioSubStep)
  990. {
  991. #ifdef JPH_ENABLE_ASSERTS
  992. // We update velocities and need to read positions to do so
  993. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  994. #endif
  995. float delta_time = ioContext->mSubStepDeltaTime;
  996. float warm_start_impulse_ratio = ioContext->mWarmStartImpulseRatio;
  997. Constraint **active_constraints = ioContext->mActiveConstraints;
  998. bool first_sub_step = ioSubStep->mIsFirst;
  999. bool last_sub_step = ioSubStep->mIsLast;
  1000. for (;;)
  1001. {
  1002. // Next island
  1003. uint32 island_idx = ioSubStep->mSolveVelocityConstraintsNextIsland++;
  1004. if (island_idx >= mIslandBuilder.GetNumIslands())
  1005. break;
  1006. JPH_PROFILE("Island");
  1007. // Get iterators
  1008. uint32 *constraints_begin, *constraints_end;
  1009. bool has_constraints = mIslandBuilder.GetConstraintsInIsland(island_idx, constraints_begin, constraints_end);
  1010. uint32 *contacts_begin, *contacts_end;
  1011. bool has_contacts = mIslandBuilder.GetContactsInIsland(island_idx, contacts_begin, contacts_end);
  1012. if (first_sub_step)
  1013. {
  1014. // If we don't have any contacts or constraints, we know that none of the following islands have any contacts or constraints
  1015. // (because they're sorted by most constraints first). This means we're done.
  1016. if (!has_contacts && !has_constraints)
  1017. {
  1018. #ifdef JPH_ENABLE_ASSERTS
  1019. // Validate our assumption that the next islands don't have any constraints or contacts
  1020. for (; island_idx < mIslandBuilder.GetNumIslands(); ++island_idx)
  1021. {
  1022. JPH_ASSERT(!mIslandBuilder.GetConstraintsInIsland(island_idx, constraints_begin, constraints_end));
  1023. JPH_ASSERT(!mIslandBuilder.GetContactsInIsland(island_idx, contacts_begin, contacts_end));
  1024. }
  1025. #endif // JPH_ENABLE_ASSERTS
  1026. return;
  1027. }
  1028. // Sort constraints to give a deterministic simulation
  1029. mConstraintManager.SortConstraints(active_constraints, constraints_begin, constraints_end);
  1030. // Sort contacts to give a deterministic simulation
  1031. mContactManager.SortContacts(contacts_begin, contacts_end);
  1032. }
  1033. else
  1034. {
  1035. {
  1036. JPH_PROFILE("Apply Gravity");
  1037. // Get bodies in this island
  1038. BodyID *bodies_begin, *bodies_end;
  1039. mIslandBuilder.GetBodiesInIsland(island_idx, bodies_begin, bodies_end);
  1040. // Apply gravity. In the first step this is done in a separate job.
  1041. for (const BodyID *body_id = bodies_begin; body_id < bodies_end; ++body_id)
  1042. {
  1043. Body &body = mBodyManager.GetBody(*body_id);
  1044. if (body.IsDynamic())
  1045. body.GetMotionProperties()->ApplyForceTorqueAndDragInternal(body.GetRotation(), mGravity, delta_time);
  1046. }
  1047. }
  1048. // If we don't have any contacts or constraints, we don't need to run the solver, but we do need to process
  1049. // the next island in order to apply gravity
  1050. if (!has_contacts && !has_constraints)
  1051. continue;
  1052. // Prepare velocity constraints. In the first step this is done when adding the contact constraints.
  1053. mConstraintManager.SetupVelocityConstraints(active_constraints, constraints_begin, constraints_end, delta_time);
  1054. mContactManager.SetupVelocityConstraints(contacts_begin, contacts_end, delta_time);
  1055. }
  1056. // Warm start
  1057. mConstraintManager.WarmStartVelocityConstraints(active_constraints, constraints_begin, constraints_end, warm_start_impulse_ratio);
  1058. mContactManager.WarmStartVelocityConstraints(contacts_begin, contacts_end, warm_start_impulse_ratio);
  1059. // Solve
  1060. for (int velocity_step = 0; velocity_step < mPhysicsSettings.mNumVelocitySteps; ++velocity_step)
  1061. {
  1062. bool constraint_impulse = mConstraintManager.SolveVelocityConstraints(active_constraints, constraints_begin, constraints_end, delta_time);
  1063. bool contact_impulse = mContactManager.SolveVelocityConstraints(contacts_begin, contacts_end);
  1064. if (!constraint_impulse && !contact_impulse)
  1065. break;
  1066. }
  1067. // Save back the lambdas in the contact cache for the warm start of the next physics update
  1068. if (last_sub_step)
  1069. mContactManager.StoreAppliedImpulses(contacts_begin, contacts_end);
  1070. }
  1071. }
  1072. void PhysicsSystem::JobIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::SubStep *ioSubStep)
  1073. {
  1074. #ifdef JPH_ENABLE_ASSERTS
  1075. // We update positions and need velocity to do so, we also clamp velocities so need to write to them
  1076. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::ReadWrite);
  1077. #endif
  1078. float delta_time = ioContext->mSubStepDeltaTime;
  1079. uint32 num_active_bodies_after_find_collisions = ioSubStep->mStep->mActiveBodyReadIdx;
  1080. // Reserve enough space for all bodies that may need a cast
  1081. TempAllocator *temp_allocator = ioContext->mTempAllocator;
  1082. JPH_ASSERT(ioSubStep->mCCDBodies == nullptr);
  1083. ioSubStep->mCCDBodiesCapacity = mBodyManager.GetNumActiveCCDBodies();
  1084. ioSubStep->mCCDBodies = (CCDBody *)temp_allocator->Allocate(ioSubStep->mCCDBodiesCapacity * sizeof(CCDBody));
  1085. // Initialize the mapping table between active body and CCD body
  1086. const BodyID *active_bodies = mBodyManager.GetActiveBodiesUnsafe();
  1087. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies();
  1088. JPH_ASSERT(ioSubStep->mActiveBodyToCCDBody == nullptr);
  1089. ioSubStep->mActiveBodyToCCDBody = (int *)temp_allocator->Allocate(num_active_bodies * sizeof(int));
  1090. // We can move bodies that are not part of an island. In this case we need to notify the broadphase of the movement.
  1091. static constexpr int cBodiesBatch = 64;
  1092. BodyID *bodies_to_update_bounds = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  1093. int num_bodies_to_update_bounds = 0;
  1094. // Update the positions using an Symplectic Euler step (which integrates using the updated velocity v1' rather
  1095. // than the original velocity v1):
  1096. // x1' = x1 + h * v1'
  1097. // At this point the active bodies list does not change, so it is safe to access the array.
  1098. for (const BodyID *id = active_bodies, *id_end = active_bodies + num_active_bodies; id < id_end; ++id)
  1099. {
  1100. Body &body = mBodyManager.GetBody(*id);
  1101. MotionProperties *mp = body.GetMotionProperties();
  1102. // Clamp velocities (not for kinematic bodies)
  1103. if (body.IsDynamic())
  1104. {
  1105. mp->ClampLinearVelocity();
  1106. mp->ClampAngularVelocity();
  1107. }
  1108. // Update the rotation of the body according to the angular velocity
  1109. // For motion type discrete we need to do this anyway, for motion type linear cast we have multiple choices
  1110. // 1. Rotate the body first and then sweep
  1111. // 2. First sweep and then rotate the body at the end
  1112. // 3. Pick some inbetween rotation (e.g. half way), then sweep and finally rotate the remainder
  1113. // (1) has some clear advantages as when a long thin body hits a surface away from the center of mass, this will result in a large angular velocity and a limited reduction in linear velocity.
  1114. // When simulation the rotation first before doing the translation, the body will be able to rotate away from the contact point allowing the center of mass to approach the surface. When using
  1115. // approach (2) in this case what will happen is that we will immediately detect the same collision again (the body has not rotated and the body was already colliding at the end of the previous
  1116. // time step) resulting in a lot of stolen time and the body appearing to be frozen in an unnatural pose (like it is glued at an angle to the surface). (2) obviously has some negative side effects
  1117. // too as simulating the rotation first may cause it to tunnel through a small object that the linear cast might have otherwise dectected. In any case a linear cast is not good for detecting
  1118. // tunneling due to angular rotation, so we don't care about that too much (you'd need a full cast to take angular effects into account).
  1119. body.AddRotationStep(body.GetAngularVelocity() * delta_time);
  1120. // Get delta position
  1121. Vec3 delta_pos = body.GetLinearVelocity() * delta_time;
  1122. // If the position should be updated (or if it is delayed because of CCD)
  1123. bool update_position = true;
  1124. switch (mp->GetMotionQuality())
  1125. {
  1126. case EMotionQuality::Discrete:
  1127. // No additional collision checking to be done
  1128. break;
  1129. case EMotionQuality::LinearCast:
  1130. if (body.IsDynamic()) // Kinematic bodies cannot be stopped
  1131. {
  1132. // Determine inner radius (the smallest sphere that fits into the shape)
  1133. float inner_radius = body.GetShape()->GetInnerRadius();
  1134. JPH_ASSERT(inner_radius > 0.0f, "The shape has no inner radius, this makes the shape unsuitable for the linear cast motion quality as we cannot move it without risking tunneling.");
  1135. // Measure translation in this step and check if it above the treshold to perform a linear cast
  1136. float linear_cast_threshold_sq = Square(mPhysicsSettings.mLinearCastThreshold * inner_radius);
  1137. if (delta_pos.LengthSq() > linear_cast_threshold_sq)
  1138. {
  1139. // This body needs a cast
  1140. ioSubStep->mActiveBodyToCCDBody[ioSubStep->mNumActiveBodyToCCDBody++] = ioSubStep->mNumCCDBodies;
  1141. new (&ioSubStep->mCCDBodies[ioSubStep->mNumCCDBodies++]) CCDBody(*id, delta_pos, linear_cast_threshold_sq, min(mPhysicsSettings.mPenetrationSlop, mPhysicsSettings.mLinearCastMaxPenetration * inner_radius));
  1142. update_position = false;
  1143. }
  1144. }
  1145. break;
  1146. }
  1147. if (update_position)
  1148. {
  1149. // Move the body now
  1150. body.AddPositionStep(delta_pos);
  1151. // If the body was activated due to an earlier CCD step it will have an index in the active
  1152. // body list that it higher than the highest one we processed during FindCollisions
  1153. // which means it hasn't been assigned an island and will not be updated by an island
  1154. // this means that we need to update its bounds manually
  1155. if (mp->GetIndexInActiveBodiesInternal() >= num_active_bodies_after_find_collisions)
  1156. {
  1157. body.CalculateWorldSpaceBoundsInternal();
  1158. bodies_to_update_bounds[num_bodies_to_update_bounds++] = body.GetID();
  1159. if (num_bodies_to_update_bounds == cBodiesBatch)
  1160. {
  1161. // Buffer full, flush now
  1162. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds);
  1163. num_bodies_to_update_bounds = 0;
  1164. }
  1165. }
  1166. // We did not create a CCD body
  1167. ioSubStep->mActiveBodyToCCDBody[ioSubStep->mNumActiveBodyToCCDBody++] = -1;
  1168. }
  1169. }
  1170. // Validate that our reservations were correct
  1171. JPH_ASSERT(ioSubStep->mNumCCDBodies <= mBodyManager.GetNumActiveCCDBodies());
  1172. JPH_ASSERT(ioSubStep->mNumActiveBodyToCCDBody == num_active_bodies);
  1173. // Notify change bounds on requested bodies
  1174. if (num_bodies_to_update_bounds > 0)
  1175. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  1176. if (ioSubStep->mNumCCDBodies == 0)
  1177. {
  1178. // No continous collision detection jobs -> kick the next job ourselves
  1179. if (ioSubStep->mIsLast)
  1180. ioSubStep->mStep->mContactRemovedCallbacks.RemoveDependency();
  1181. }
  1182. else
  1183. {
  1184. // Run the continous collision detection jobs
  1185. int num_continuous_collision_jobs = min(int(ioSubStep->mNumCCDBodies + cNumCCDBodiesPerJob - 1) / cNumCCDBodiesPerJob, ioContext->GetMaxConcurrency());
  1186. ioSubStep->mResolveCCDContacts.AddDependency(num_continuous_collision_jobs);
  1187. if (ioSubStep->mIsLast)
  1188. ioSubStep->mStep->mContactRemovedCallbacks.AddDependency(num_continuous_collision_jobs - 1); // Already had 1 dependency
  1189. for (int i = 0; i < num_continuous_collision_jobs; ++i)
  1190. {
  1191. JobHandle job = ioContext->mJobSystem->CreateJob("FindCCDContacts", cColorFindCCDContacts, [ioContext, ioSubStep]()
  1192. {
  1193. ioContext->mPhysicsSystem->JobFindCCDContacts(ioContext, ioSubStep);
  1194. ioSubStep->mResolveCCDContacts.RemoveDependency();
  1195. if (ioSubStep->mIsLast)
  1196. ioSubStep->mStep->mContactRemovedCallbacks.RemoveDependency();
  1197. });
  1198. ioContext->mBarrier->AddJob(job);
  1199. }
  1200. }
  1201. }
  1202. // Helper function to calculate the motion of a body during this CCD step
  1203. inline static Vec3 sCalculateBodyMotion(const Body &inBody, float inDeltaTime)
  1204. {
  1205. // If the body is linear casting, the body has not yet moved so we need to calculate its motion
  1206. if (inBody.IsDynamic() && inBody.GetMotionProperties()->GetMotionQuality() == EMotionQuality::LinearCast)
  1207. return inDeltaTime * inBody.GetLinearVelocity();
  1208. // Body has already moved, so we don't need to correct for anything
  1209. return Vec3::sZero();
  1210. }
  1211. // Helper function that finds the CCD body corresponding to a body (if it exists)
  1212. inline static PhysicsUpdateContext::SubStep::CCDBody *sGetCCDBody(const Body &inBody, PhysicsUpdateContext::SubStep *inSubStep)
  1213. {
  1214. // If the body has no motion properties it cannot have a CCD body
  1215. const MotionProperties *motion_properties = inBody.GetMotionPropertiesUnchecked();
  1216. if (motion_properties == nullptr)
  1217. return nullptr;
  1218. // If it is not active it cannot have a CCD body
  1219. uint32 active_index = motion_properties->GetIndexInActiveBodiesInternal();
  1220. if (active_index == Body::cInactiveIndex)
  1221. return nullptr;
  1222. // Check if the active body has a corresponding CCD body
  1223. JPH_ASSERT(active_index < inSubStep->mNumActiveBodyToCCDBody); // Ensure that the body has a mapping to CCD body
  1224. int ccd_index = inSubStep->mActiveBodyToCCDBody[active_index];
  1225. if (ccd_index < 0)
  1226. return nullptr;
  1227. PhysicsUpdateContext::SubStep::CCDBody *ccd_body = &inSubStep->mCCDBodies[ccd_index];
  1228. JPH_ASSERT(ccd_body->mBodyID1 == inBody.GetID(), "We found the wrong CCD body!");
  1229. return ccd_body;
  1230. }
  1231. void PhysicsSystem::JobFindCCDContacts(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::SubStep *ioSubStep)
  1232. {
  1233. #ifdef JPH_ENABLE_ASSERTS
  1234. // We only read positions, but the validate callback may read body positions and velocities
  1235. BodyAccess::Grant grant(BodyAccess::EAccess::Read, BodyAccess::EAccess::Read);
  1236. #endif
  1237. // Settings
  1238. ShapeCastSettings settings;
  1239. settings.mUseShrunkenShapeAndConvexRadius = true;
  1240. settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  1241. settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  1242. settings.mReturnDeepestPoint = true;
  1243. settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
  1244. settings.mActiveEdgeMode = mPhysicsSettings.mCheckActiveEdges? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
  1245. for (;;)
  1246. {
  1247. // Fetch the next body to cast
  1248. uint32 idx = ioSubStep->mNextCCDBody++;
  1249. if (idx >= ioSubStep->mNumCCDBodies)
  1250. break;
  1251. CCDBody &ccd_body = ioSubStep->mCCDBodies[idx];
  1252. const Body &body = mBodyManager.GetBody(ccd_body.mBodyID1);
  1253. // Filter out layers
  1254. DefaultBroadPhaseLayerFilter broadphase_layer_filter = GetDefaultBroadPhaseLayerFilter(body.GetObjectLayer());
  1255. DefaultObjectLayerFilter object_layer_filter = GetDefaultLayerFilter(body.GetObjectLayer());
  1256. #ifdef JPH_DEBUG_RENDERER
  1257. // Draw start and end shape of cast
  1258. if (sDrawMotionQualityLinearCast)
  1259. {
  1260. Mat44 com = body.GetCenterOfMassTransform();
  1261. body.GetShape()->Draw(DebugRenderer::sInstance, com, Vec3::sReplicate(1.0f), Color::sGreen, false, true);
  1262. DebugRenderer::sInstance->DrawArrow(com.GetTranslation(), com.GetTranslation() + ccd_body.mDeltaPosition, Color::sGreen, 0.1f);
  1263. body.GetShape()->Draw(DebugRenderer::sInstance, Mat44::sTranslation(ccd_body.mDeltaPosition) * com, Vec3::sReplicate(1.0f), Color::sRed, false, true);
  1264. }
  1265. #endif // JPH_DEBUG_RENDERER
  1266. // Create a collector that will find the maximum distance allowed to travel while not penetrating more than 'max penetration'
  1267. class CCDNarrowPhaseCollector : public CastShapeCollector
  1268. {
  1269. public:
  1270. CCDNarrowPhaseCollector(const BodyManager &inBodyManager, ContactConstraintManager &inContactConstraintManager, CCDBody &inCCDBody, ShapeCastResult &inResult, float inDeltaTime) :
  1271. mBodyManager(inBodyManager),
  1272. mContactConstraintManager(inContactConstraintManager),
  1273. mCCDBody(inCCDBody),
  1274. mResult(inResult),
  1275. mDeltaTime(inDeltaTime)
  1276. {
  1277. }
  1278. virtual void AddHit(const ShapeCastResult &inResult) override
  1279. {
  1280. JPH_PROFILE_FUNCTION();
  1281. // Check if this is a possible earlier hit than the one before
  1282. float fraction = inResult.mFraction;
  1283. if (fraction < mCCDBody.mFractionPlusSlop)
  1284. {
  1285. // Normalize normal
  1286. Vec3 normal = inResult.mPenetrationAxis.Normalized();
  1287. // Calculate how much we can add to the fraction to penetrate the collision point by mMaxPenetration.
  1288. // Note that the normal is pointing towards body 2!
  1289. // Let the extra distance that we can travel along delta_pos be 'dist': mMaxPenetration / dist = cos(angle between normal and delta_pos) = normal . delta_pos / |delta_pos|
  1290. // <=> dist = mMaxPenetration * |delta_pos| / normal . delta_pos
  1291. // Converting to a faction: delta_fraction = dist / |delta_pos| = mLinearCastTreshold / normal . delta_pos
  1292. float denominator = normal.Dot(mCCDBody.mDeltaPosition);
  1293. if (denominator > mCCDBody.mMaxPenetration) // Avoid dividing by zero, if extra hit fraction > 1 there's also no point in continuing
  1294. {
  1295. float fraction_plus_slop = fraction + mCCDBody.mMaxPenetration / denominator;
  1296. if (fraction_plus_slop < mCCDBody.mFractionPlusSlop)
  1297. {
  1298. const Body &body2 = mBodyManager.GetBody(inResult.mBodyID2);
  1299. // Check if we've already accepted all hits from this body
  1300. if (mValidateBodyPair)
  1301. {
  1302. // Validate the contact result
  1303. ValidateResult validate_result = mContactConstraintManager.ValidateContactPoint(mBodyManager.GetBody(mCCDBody.mBodyID1), body2, inResult);
  1304. switch (validate_result)
  1305. {
  1306. case ValidateResult::AcceptContact:
  1307. // Just continue
  1308. break;
  1309. case ValidateResult::AcceptAllContactsForThisBodyPair:
  1310. // Accept this and all following contacts from this body
  1311. mValidateBodyPair = false;
  1312. break;
  1313. case ValidateResult::RejectContact:
  1314. return;
  1315. case ValidateResult::RejectAllContactsForThisBodyPair:
  1316. // Reject this and all following contacts from this body
  1317. mRejectAll = true;
  1318. ForceEarlyOut();
  1319. return;
  1320. }
  1321. }
  1322. // This is the earliest hit so far, store it
  1323. mCCDBody.mContactNormal = normal;
  1324. mCCDBody.mBodyID2 = inResult.mBodyID2;
  1325. mCCDBody.mFraction = fraction;
  1326. mCCDBody.mFractionPlusSlop = fraction_plus_slop;
  1327. mResult = inResult;
  1328. // Result was assuming body 2 is not moving, but it is, so we need to correct for it
  1329. Vec3 movement2 = fraction * sCalculateBodyMotion(body2, mDeltaTime);
  1330. if (!movement2.IsNearZero())
  1331. {
  1332. mResult.mContactPointOn1 += movement2;
  1333. mResult.mContactPointOn2 += movement2;
  1334. for (Vec3 &v : mResult.mShape1Face)
  1335. v += movement2;
  1336. for (Vec3 &v : mResult.mShape2Face)
  1337. v += movement2;
  1338. }
  1339. // Update early out fraction
  1340. CastShapeCollector::UpdateEarlyOutFraction(fraction_plus_slop);
  1341. }
  1342. }
  1343. }
  1344. }
  1345. bool mValidateBodyPair; ///< If we still have to call the ValidateContactPoint for this body pair
  1346. bool mRejectAll; ///< Reject all further contacts between this body pair
  1347. private:
  1348. const BodyManager & mBodyManager;
  1349. ContactConstraintManager & mContactConstraintManager;
  1350. CCDBody & mCCDBody;
  1351. ShapeCastResult & mResult;
  1352. float mDeltaTime;
  1353. BodyID mAcceptedBodyID;
  1354. };
  1355. // Narrowphase collector
  1356. ShapeCastResult cast_shape_result;
  1357. CCDNarrowPhaseCollector np_collector(mBodyManager, mContactManager, ccd_body, cast_shape_result, ioContext->mSubStepDeltaTime);
  1358. // This collector wraps the narrowphase collector and collects the closest hit
  1359. class CCDBroadPhaseCollector : public CastShapeBodyCollector
  1360. {
  1361. public:
  1362. CCDBroadPhaseCollector(const CCDBody &inCCDBody, const Body &inBody1, const ShapeCast &inShapeCast, ShapeCastSettings &inShapeCastSettings, CCDNarrowPhaseCollector &ioCollector, const BodyManager &inBodyManager, PhysicsUpdateContext::SubStep *inSubStep, float inDeltaTime) :
  1363. mCCDBody(inCCDBody),
  1364. mBody1(inBody1),
  1365. mBody1Extent(inShapeCast.mShapeWorldBounds.GetExtent()),
  1366. mShapeCast(inShapeCast),
  1367. mShapeCastSettings(inShapeCastSettings),
  1368. mCollector(ioCollector),
  1369. mBodyManager(inBodyManager),
  1370. mSubStep(inSubStep),
  1371. mDeltaTime(inDeltaTime)
  1372. {
  1373. }
  1374. virtual void AddHit(const BroadPhaseCastResult &inResult) override
  1375. {
  1376. JPH_PROFILE_FUNCTION();
  1377. JPH_ASSERT(inResult.mFraction <= GetEarlyOutFraction(), "This hit should not have been passed on to the collector");
  1378. // Test if we're colliding with ourselves
  1379. if (mBody1.GetID() == inResult.mBodyID)
  1380. return;
  1381. // Avoid treating duplicates, if both bodies are doing CCD then only consider collision if body ID < other body ID
  1382. const Body &body2 = mBodyManager.GetBody(inResult.mBodyID);
  1383. const CCDBody *ccd_body2 = sGetCCDBody(body2, mSubStep);
  1384. if (ccd_body2 != nullptr && mCCDBody.mBodyID1 > ccd_body2->mBodyID1)
  1385. return;
  1386. // Test group filter
  1387. if (!mBody1.GetCollisionGroup().CanCollide(body2.GetCollisionGroup()))
  1388. return;
  1389. // TODO: For now we ignore sensors
  1390. if (body2.IsSensor())
  1391. return;
  1392. // Get relative movement of these two bodies
  1393. Vec3 direction = mShapeCast.mDirection - sCalculateBodyMotion(body2, mDeltaTime);
  1394. // Test if the remaining movement is less than our movement threshold
  1395. if (direction.LengthSq() < mCCDBody.mLinearCastThresholdSq)
  1396. return;
  1397. // Get the bounds of 2, widen it by the extent of 1 and test a ray to see if it hits earlier than the current early out fraction
  1398. AABox bounds = body2.GetWorldSpaceBounds();
  1399. bounds.mMin -= mBody1Extent;
  1400. bounds.mMax += mBody1Extent;
  1401. float hit_fraction = RayAABox(mShapeCast.mCenterOfMassStart.GetTranslation(), RayInvDirection(direction), bounds.mMin, bounds.mMax);
  1402. if (hit_fraction > max(FLT_MIN, GetEarlyOutFraction())) // If early out fraction <= 0, we have the possibility of finding a deeper hit so we need to clamp the early out fraction
  1403. return;
  1404. // Reset collector (this is a new body pair)
  1405. mCollector.ResetEarlyOutFraction(GetEarlyOutFraction());
  1406. mCollector.mValidateBodyPair = true;
  1407. mCollector.mRejectAll = false;
  1408. // Provide direction as hint for the active edges algorithm
  1409. mShapeCastSettings.mActiveEdgeMovementDirection = direction;
  1410. // Do narrow phase collision check
  1411. ShapeCast relative_cast(mShapeCast.mShape, mShapeCast.mScale, mShapeCast.mCenterOfMassStart, direction, mShapeCast.mShapeWorldBounds);
  1412. body2.GetTransformedShape().CastShape(relative_cast, mShapeCastSettings, mCollector);
  1413. // Update early out fraction based on narrow phase collector
  1414. if (!mCollector.mRejectAll)
  1415. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  1416. }
  1417. const CCDBody & mCCDBody;
  1418. const Body & mBody1;
  1419. Vec3 mBody1Extent;
  1420. ShapeCast mShapeCast;
  1421. ShapeCastSettings & mShapeCastSettings;
  1422. CCDNarrowPhaseCollector & mCollector;
  1423. const BodyManager & mBodyManager;
  1424. PhysicsUpdateContext::SubStep *mSubStep;
  1425. float mDeltaTime;
  1426. };
  1427. // Check if we collide with any other body
  1428. ShapeCast shape_cast(body.GetShape(), Vec3::sReplicate(1.0f), body.GetCenterOfMassTransform(), ccd_body.mDeltaPosition);
  1429. CCDBroadPhaseCollector bp_collector(ccd_body, body, shape_cast, settings, np_collector, mBodyManager, ioSubStep, ioContext->mSubStepDeltaTime);
  1430. mBroadPhase->CastAABox({ shape_cast.mShapeWorldBounds, shape_cast.mDirection }, bp_collector, broadphase_layer_filter, object_layer_filter);
  1431. // Check if there was a hit
  1432. if (ccd_body.mFractionPlusSlop < 1.0f)
  1433. {
  1434. const Body &body2 = mBodyManager.GetBody(ccd_body.mBodyID2);
  1435. // Determine contact manifold
  1436. ContactManifold manifold;
  1437. ManifoldBetweenTwoFaces(cast_shape_result.mContactPointOn1, cast_shape_result.mContactPointOn2, cast_shape_result.mPenetrationAxis, mPhysicsSettings.mManifoldToleranceSq, cast_shape_result.mShape1Face, cast_shape_result.mShape2Face, manifold.mWorldSpaceContactPointsOn1, manifold.mWorldSpaceContactPointsOn2);
  1438. manifold.mSubShapeID1 = cast_shape_result.mSubShapeID1;
  1439. manifold.mSubShapeID2 = cast_shape_result.mSubShapeID2;
  1440. manifold.mPenetrationDepth = cast_shape_result.mPenetrationDepth;
  1441. manifold.mWorldSpaceNormal = ccd_body.mContactNormal;
  1442. // Call contact point callbacks
  1443. mContactManager.OnCCDContactAdded(body, body2, manifold, ccd_body.mContactSettings);
  1444. // Calculate the average position from the manifold (this will result in the same impulse applied as when we apply impulses to all contact points)
  1445. if (manifold.mWorldSpaceContactPointsOn2.size() > 1)
  1446. {
  1447. Vec3 average_contact_point = Vec3::sZero();
  1448. for (const Vec3 &v : manifold.mWorldSpaceContactPointsOn2)
  1449. average_contact_point += v;
  1450. average_contact_point /= (float)manifold.mWorldSpaceContactPointsOn2.size();
  1451. ccd_body.mContactPointOn2 = average_contact_point;
  1452. }
  1453. else
  1454. ccd_body.mContactPointOn2 = cast_shape_result.mContactPointOn2;
  1455. }
  1456. }
  1457. }
  1458. void PhysicsSystem::JobResolveCCDContacts(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::SubStep *ioSubStep)
  1459. {
  1460. #ifdef JPH_ENABLE_ASSERTS
  1461. // Read/write body access
  1462. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::ReadWrite);
  1463. // We activate bodies that we collide with
  1464. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  1465. #endif
  1466. uint32 num_active_bodies_after_find_collisions = ioSubStep->mStep->mActiveBodyReadIdx;
  1467. TempAllocator *temp_allocator = ioContext->mTempAllocator;
  1468. // Check if there's anything to do
  1469. uint num_ccd_bodies = ioSubStep->mNumCCDBodies;
  1470. if (num_ccd_bodies > 0)
  1471. {
  1472. // Sort on fraction so that we process earliest collisions first
  1473. // This is needed to make the simulation deterministic and also to be able to stop contact processing
  1474. // between body pairs if an earlier hit was found involving the body by another CCD body
  1475. // (if it's body ID < this CCD body's body ID - see filtering logic in CCDBroadPhaseCollector)
  1476. CCDBody **sorted_ccd_bodies = (CCDBody **)temp_allocator->Allocate(num_ccd_bodies * sizeof(CCDBody *));
  1477. {
  1478. JPH_PROFILE("Sort");
  1479. // We don't want to copy the entire struct (it's quite big), so we create a pointer array first
  1480. CCDBody *src_ccd_bodies = ioSubStep->mCCDBodies;
  1481. CCDBody **dst_ccd_bodies = sorted_ccd_bodies;
  1482. CCDBody **dst_ccd_bodies_end = dst_ccd_bodies + num_ccd_bodies;
  1483. while (dst_ccd_bodies < dst_ccd_bodies_end)
  1484. *(dst_ccd_bodies++) = src_ccd_bodies++;
  1485. // Which we then sort
  1486. sort(sorted_ccd_bodies, sorted_ccd_bodies + num_ccd_bodies, [](const CCDBody *inBody1, const CCDBody *inBody2)
  1487. {
  1488. if (inBody1->mFractionPlusSlop != inBody2->mFractionPlusSlop)
  1489. return inBody1->mFractionPlusSlop < inBody2->mFractionPlusSlop;
  1490. return inBody1->mBodyID1 < inBody2->mBodyID1;
  1491. });
  1492. }
  1493. // We can collide with bodies that are not active, we track them here so we can activate them in one go at the end.
  1494. // This is also needed because we can't modify the active body array while we iterate it.
  1495. static constexpr int cBodiesBatch = 64;
  1496. BodyID *bodies_to_activate = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  1497. int num_bodies_to_activate = 0;
  1498. // We can move bodies that are not part of an island. In this case we need to notify the broadphase of the movement.
  1499. BodyID *bodies_to_update_bounds = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  1500. int num_bodies_to_update_bounds = 0;
  1501. for (uint i = 0; i < num_ccd_bodies; ++i)
  1502. {
  1503. const CCDBody *ccd_body = sorted_ccd_bodies[i];
  1504. Body &body1 = mBodyManager.GetBody(ccd_body->mBodyID1);
  1505. MotionProperties *body_mp = body1.GetMotionProperties();
  1506. // If there was a hit
  1507. if (!ccd_body->mBodyID2.IsInvalid())
  1508. {
  1509. Body &body2 = mBodyManager.GetBody(ccd_body->mBodyID2);
  1510. // Determine if the other body has a CCD body
  1511. CCDBody *ccd_body2 = sGetCCDBody(body2, ioSubStep);
  1512. if (ccd_body2 != nullptr)
  1513. {
  1514. JPH_ASSERT(ccd_body2->mBodyID2 != ccd_body->mBodyID1, "If we collided with another body, that other body should have ignored collisions with us!");
  1515. // Check if the other body found a hit that is further away
  1516. if (ccd_body2->mFraction > ccd_body->mFraction)
  1517. {
  1518. // Reset the colliding body of the other CCD body. The other body will shorten its distance travelled and will not do any collision response (we'll do that).
  1519. // This means that at this point we have triggered a contact point add/persist for our further hit by accident for the other body.
  1520. // We accept this as calling the contact point callbacks here would require persisting the manifolds up to this point and doing the callbacks single threaded.
  1521. ccd_body2->mBodyID2 = BodyID();
  1522. ccd_body2->mFractionPlusSlop = ccd_body->mFraction;
  1523. }
  1524. }
  1525. // If the other body moved less than us before hitting something, we're not colliding with it so we again have triggered contact point add/persist callbacks by accident.
  1526. // We'll just move to the collision position anyway (as that's the last position we know is good), but we won't do any collision response.
  1527. if (ccd_body2 == nullptr || ccd_body2->mFraction >= ccd_body->mFraction)
  1528. {
  1529. // Calculate contact points relative to center of mass of both bodies
  1530. Vec3 r1_plus_u = ccd_body->mContactPointOn2 - (body1.GetCenterOfMassPosition() + ccd_body->mFraction * ccd_body->mDeltaPosition);
  1531. Vec3 r2 = ccd_body->mContactPointOn2 - body2.GetCenterOfMassPosition();
  1532. // Calculate velocity of collision points
  1533. Vec3 v1 = body1.GetLinearVelocity() + body1.GetAngularVelocity().Cross(r1_plus_u);
  1534. Vec3 v2 = body2.GetLinearVelocity() + body2.GetAngularVelocity().Cross(r2);
  1535. Vec3 relative_velocity = v2 - v1;
  1536. float normal_velocity = relative_velocity.Dot(ccd_body->mContactNormal);
  1537. // Calculate velocity bias due to restitution
  1538. float normal_velocity_bias;
  1539. if (ccd_body->mContactSettings.mCombinedRestitution > 0.0f && normal_velocity < -mPhysicsSettings.mMinVelocityForRestitution)
  1540. normal_velocity_bias = ccd_body->mContactSettings.mCombinedRestitution * normal_velocity;
  1541. else
  1542. normal_velocity_bias = 0.0f;
  1543. // Solve contact constraint
  1544. AxisConstraintPart contact_constraint;
  1545. contact_constraint.CalculateConstraintProperties(ioContext->mSubStepDeltaTime, body1, r1_plus_u, body2, r2, ccd_body->mContactNormal, normal_velocity_bias);
  1546. contact_constraint.SolveVelocityConstraint(body1, body2, ccd_body->mContactNormal, -FLT_MAX, FLT_MAX);
  1547. // Apply friction
  1548. if (ccd_body->mContactSettings.mCombinedFriction > 0.0f)
  1549. {
  1550. Vec3 tangent1 = ccd_body->mContactNormal.GetNormalizedPerpendicular();
  1551. Vec3 tangent2 = ccd_body->mContactNormal.Cross(tangent1);
  1552. float max_lambda_f = ccd_body->mContactSettings.mCombinedFriction * contact_constraint.GetTotalLambda();
  1553. AxisConstraintPart friction1;
  1554. friction1.CalculateConstraintProperties(ioContext->mSubStepDeltaTime, body1, r1_plus_u, body2, r2, tangent1, 0.0f);
  1555. friction1.SolveVelocityConstraint(body1, body2, tangent1, -max_lambda_f, max_lambda_f);
  1556. AxisConstraintPart friction2;
  1557. friction2.CalculateConstraintProperties(ioContext->mSubStepDeltaTime, body1, r1_plus_u, body2, r2, tangent2, 0.0f);
  1558. friction2.SolveVelocityConstraint(body1, body2, tangent2, -max_lambda_f, max_lambda_f);
  1559. }
  1560. // Clamp velocities
  1561. body_mp->ClampLinearVelocity();
  1562. body_mp->ClampAngularVelocity();
  1563. if (body2.IsDynamic())
  1564. {
  1565. MotionProperties *body2_mp = body2.GetMotionProperties();
  1566. body2_mp->ClampLinearVelocity();
  1567. body2_mp->ClampAngularVelocity();
  1568. // Activate the body if it is not already active
  1569. if (!body2.IsActive())
  1570. {
  1571. bodies_to_activate[num_bodies_to_activate++] = ccd_body->mBodyID2;
  1572. if (num_bodies_to_activate == cBodiesBatch)
  1573. {
  1574. // Batch is full, activate now
  1575. mBodyManager.ActivateBodies(bodies_to_activate, num_bodies_to_activate);
  1576. num_bodies_to_activate = 0;
  1577. }
  1578. }
  1579. }
  1580. #ifdef JPH_DEBUG_RENDERER
  1581. if (sDrawMotionQualityLinearCast)
  1582. {
  1583. // Draw the collision location
  1584. Mat44 collision_transform = Mat44::sTranslation(ccd_body->mFraction * ccd_body->mDeltaPosition) * body1.GetCenterOfMassTransform();
  1585. body1.GetShape()->Draw(DebugRenderer::sInstance, collision_transform, Vec3::sReplicate(1.0f), Color::sYellow, false, true);
  1586. // Draw the collision location + slop
  1587. Mat44 collision_transform_plus_slop = Mat44::sTranslation(ccd_body->mFractionPlusSlop * ccd_body->mDeltaPosition) * body1.GetCenterOfMassTransform();
  1588. body1.GetShape()->Draw(DebugRenderer::sInstance, collision_transform_plus_slop, Vec3::sReplicate(1.0f), Color::sOrange, false, true);
  1589. // Draw contact normal
  1590. DebugRenderer::sInstance->DrawArrow(ccd_body->mContactPointOn2, ccd_body->mContactPointOn2 - ccd_body->mContactNormal, Color::sYellow, 0.1f);
  1591. // Draw post contact velocity
  1592. DebugRenderer::sInstance->DrawArrow(collision_transform.GetTranslation(), collision_transform.GetTranslation() + body1.GetLinearVelocity(), Color::sOrange, 0.1f);
  1593. DebugRenderer::sInstance->DrawArrow(collision_transform.GetTranslation(), collision_transform.GetTranslation() + body1.GetAngularVelocity(), Color::sPurple, 0.1f);
  1594. }
  1595. #endif // JPH_DEBUG_RENDERER
  1596. }
  1597. }
  1598. // Update body position
  1599. body1.AddPositionStep(ccd_body->mDeltaPosition * ccd_body->mFractionPlusSlop);
  1600. // If the body was activated due to an earlier CCD step it will have an index in the active
  1601. // body list that it higher than the highest one we processed during FindCollisions
  1602. // which means it hasn't been assigned an island and will not be updated by an island
  1603. // this means that we need to update its bounds manually
  1604. if (body_mp->GetIndexInActiveBodiesInternal() >= num_active_bodies_after_find_collisions)
  1605. {
  1606. body1.CalculateWorldSpaceBoundsInternal();
  1607. bodies_to_update_bounds[num_bodies_to_update_bounds++] = body1.GetID();
  1608. if (num_bodies_to_update_bounds == cBodiesBatch)
  1609. {
  1610. // Buffer full, flush now
  1611. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds);
  1612. num_bodies_to_update_bounds = 0;
  1613. }
  1614. }
  1615. }
  1616. // Activate the requested bodies
  1617. if (num_bodies_to_activate > 0)
  1618. mBodyManager.ActivateBodies(bodies_to_activate, num_bodies_to_activate);
  1619. // Notify change bounds on requested bodies
  1620. if (num_bodies_to_update_bounds > 0)
  1621. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  1622. // Free the sorted ccd bodies
  1623. temp_allocator->Free(sorted_ccd_bodies, num_ccd_bodies * sizeof(CCDBody *));
  1624. }
  1625. // Ensure we free the CCD bodies array now, will not call the destructor!
  1626. temp_allocator->Free(ioSubStep->mActiveBodyToCCDBody, ioSubStep->mNumActiveBodyToCCDBody * sizeof(int));
  1627. ioSubStep->mActiveBodyToCCDBody = nullptr;
  1628. ioSubStep->mNumActiveBodyToCCDBody = 0;
  1629. temp_allocator->Free(ioSubStep->mCCDBodies, ioSubStep->mCCDBodiesCapacity * sizeof(CCDBody));
  1630. ioSubStep->mCCDBodies = nullptr;
  1631. ioSubStep->mCCDBodiesCapacity = 0;
  1632. }
  1633. void PhysicsSystem::JobContactRemovedCallbacks()
  1634. {
  1635. #ifdef JPH_ENABLE_ASSERTS
  1636. // We don't touch any bodies
  1637. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  1638. #endif
  1639. // Reset the Body::EFlags::InvalidateContactCache flag for all bodies
  1640. mBodyManager.ValidateContactCacheForAllBodies();
  1641. // Trigger all contact removed callbacks by looking at last step contact points that have not been flagged as reused
  1642. mContactManager.ContactPointRemovedCallbacks();
  1643. // Finalize the contact cache (this swaps the read and write versions of the contact cache)
  1644. mContactManager.FinalizeContactCache();
  1645. }
  1646. void PhysicsSystem::JobSolvePositionConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::SubStep *ioSubStep)
  1647. {
  1648. #ifdef JPH_ENABLE_ASSERTS
  1649. // We fix up position errors
  1650. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::ReadWrite);
  1651. // Can only deactivate bodies
  1652. BodyManager::GrantActiveBodiesAccess grant_active(false, true);
  1653. #endif
  1654. float delta_time = ioContext->mSubStepDeltaTime;
  1655. Constraint **active_constraints = ioContext->mActiveConstraints;
  1656. for (;;)
  1657. {
  1658. // Next island
  1659. uint32 island_idx = ioSubStep->mSolvePositionConstraintsNextIsland++;
  1660. if (island_idx >= mIslandBuilder.GetNumIslands())
  1661. break;
  1662. JPH_PROFILE("Island");
  1663. // Get iterators for this island
  1664. BodyID *bodies_begin, *bodies_end;
  1665. mIslandBuilder.GetBodiesInIsland(island_idx, bodies_begin, bodies_end);
  1666. uint32 *constraints_begin, *constraints_end;
  1667. bool has_constraints = mIslandBuilder.GetConstraintsInIsland(island_idx, constraints_begin, constraints_end);
  1668. uint32 *contacts_begin, *contacts_end;
  1669. bool has_contacts = mIslandBuilder.GetContactsInIsland(island_idx, contacts_begin, contacts_end);
  1670. // Correct positions
  1671. if (has_contacts || has_constraints)
  1672. {
  1673. float baumgarte = mPhysicsSettings.mBaumgarte;
  1674. for (int position_step = 0; position_step < mPhysicsSettings.mNumPositionSteps; ++position_step)
  1675. {
  1676. bool constraint_impulse = mConstraintManager.SolvePositionConstraints(active_constraints, constraints_begin, constraints_end, delta_time, baumgarte);
  1677. bool contact_impulse = mContactManager.SolvePositionConstraints(contacts_begin, contacts_end);
  1678. if (!constraint_impulse && !contact_impulse)
  1679. break;
  1680. }
  1681. }
  1682. // Only check sleeping in the last sub step of the last step
  1683. // Also resets force and torque used during the apply gravity phase
  1684. if (ioSubStep->mIsLastOfAll)
  1685. {
  1686. JPH_PROFILE("Check Sleeping");
  1687. static_assert(int(Body::ECanSleep::CannotSleep) == 0 && int(Body::ECanSleep::CanSleep) == 1, "Loop below makes this assumption");
  1688. int all_can_sleep = mPhysicsSettings.mAllowSleeping? int(Body::ECanSleep::CanSleep) : int(Body::ECanSleep::CannotSleep);
  1689. float time_before_sleep = mPhysicsSettings.mTimeBeforeSleep;
  1690. float max_movement = mPhysicsSettings.mPointVelocitySleepThreshold * time_before_sleep;
  1691. for (const BodyID *body_id = bodies_begin; body_id < bodies_end; ++body_id)
  1692. {
  1693. Body &body = mBodyManager.GetBody(*body_id);
  1694. // Update bounding box
  1695. body.CalculateWorldSpaceBoundsInternal();
  1696. // Update sleeping
  1697. all_can_sleep &= int(body.UpdateSleepStateInternal(ioContext->mSubStepDeltaTime, max_movement, time_before_sleep));
  1698. // Reset force and torque
  1699. body.GetMotionProperties()->ResetForceAndTorqueInternal();
  1700. }
  1701. // If all bodies indicate they can sleep we can deactivate them
  1702. if (all_can_sleep == int(Body::ECanSleep::CanSleep))
  1703. mBodyManager.DeactivateBodies(bodies_begin, int(bodies_end - bodies_begin));
  1704. }
  1705. else
  1706. {
  1707. JPH_PROFILE("Update Bounds");
  1708. // Update bounding box only for all other sub steps
  1709. for (const BodyID *body_id = bodies_begin; body_id < bodies_end; ++body_id)
  1710. {
  1711. Body &body = mBodyManager.GetBody(*body_id);
  1712. body.CalculateWorldSpaceBoundsInternal();
  1713. }
  1714. }
  1715. // Notify broadphase of changed objects (find ccd contacts can do linear casts in the next step, so
  1716. // we need to do this every sub step)
  1717. // Note: Shuffles the BodyID's around!!!
  1718. mBroadPhase->NotifyBodiesAABBChanged(bodies_begin, int(bodies_end - bodies_begin), false);
  1719. }
  1720. }
  1721. #ifdef JPH_STAT_COLLECTOR
  1722. void PhysicsSystem::CollectStats()
  1723. {
  1724. JPH_PROFILE_FUNCTION();
  1725. JPH_STAT_COLLECTOR_ADD("ContactConstraint.ManifoldReductionPercentage", mManifoldsBeforeReduction > 0? 100.0f * (mManifoldsBeforeReduction - mManifoldsAfterReduction) / mManifoldsBeforeReduction : 0.0f);
  1726. mBodyManager.CollectStats();
  1727. mConstraintManager.CollectStats();
  1728. mContactManager.CollectStats();
  1729. CollideConvexVsTriangles::sCollectStats();
  1730. ConvexShape::sCollectStats();
  1731. }
  1732. #endif // JPH_STAT_COLLECTOR
  1733. void PhysicsSystem::SaveState(StateRecorder &inStream) const
  1734. {
  1735. JPH_PROFILE_FUNCTION();
  1736. inStream.Write(mPreviousSubStepDeltaTime);
  1737. inStream.Write(mGravity);
  1738. mBodyManager.SaveState(inStream);
  1739. mContactManager.SaveState(inStream);
  1740. mConstraintManager.SaveState(inStream);
  1741. }
  1742. bool PhysicsSystem::RestoreState(StateRecorder &inStream)
  1743. {
  1744. JPH_PROFILE_FUNCTION();
  1745. inStream.Read(mPreviousSubStepDeltaTime);
  1746. inStream.Read(mGravity);
  1747. if (!mBodyManager.RestoreState(inStream))
  1748. return false;
  1749. if (!mContactManager.RestoreState(inStream))
  1750. return false;
  1751. if (!mConstraintManager.RestoreState(inStream))
  1752. return false;
  1753. // Update bounding boxes for all bodies in the broadphase
  1754. vector<BodyID> bodies;
  1755. for (Body *b : mBodyManager.GetBodies())
  1756. if (BodyManager::sIsValidBodyPointer(b) && b->IsInBroadPhase())
  1757. bodies.push_back(b->GetID());
  1758. if (!bodies.empty())
  1759. mBroadPhase->NotifyBodiesAABBChanged(&bodies[0], (int)bodies.size());
  1760. return true;
  1761. }
  1762. } // JPH