PhysicsSystem.cpp 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/PhysicsSystem.h>
  6. #include <Jolt/Physics/PhysicsSettings.h>
  7. #include <Jolt/Physics/PhysicsUpdateContext.h>
  8. #include <Jolt/Physics/PhysicsStepListener.h>
  9. #include <Jolt/Physics/Collision/BroadPhase/BroadPhaseBruteForce.h>
  10. #include <Jolt/Physics/Collision/BroadPhase/BroadPhaseQuadTree.h>
  11. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  12. #include <Jolt/Physics/Collision/AABoxCast.h>
  13. #include <Jolt/Physics/Collision/ShapeCast.h>
  14. #include <Jolt/Physics/Collision/CollideShape.h>
  15. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  16. #include <Jolt/Physics/Collision/CastResult.h>
  17. #include <Jolt/Physics/Collision/CollideConvexVsTriangles.h>
  18. #include <Jolt/Physics/Collision/ManifoldBetweenTwoFaces.h>
  19. #include <Jolt/Physics/Collision/Shape/ConvexShape.h>
  20. #include <Jolt/Physics/Collision/SimShapeFilterWrapper.h>
  21. #include <Jolt/Physics/Collision/InternalEdgeRemovingCollector.h>
  22. #include <Jolt/Physics/Constraints/CalculateSolverSteps.h>
  23. #include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  24. #include <Jolt/Physics/DeterminismLog.h>
  25. #include <Jolt/Physics/SoftBody/SoftBodyMotionProperties.h>
  26. #include <Jolt/Physics/SoftBody/SoftBodyShape.h>
  27. #include <Jolt/Geometry/RayAABox.h>
  28. #include <Jolt/Geometry/ClosestPoint.h>
  29. #include <Jolt/Core/JobSystem.h>
  30. #include <Jolt/Core/TempAllocator.h>
  31. #include <Jolt/Core/QuickSort.h>
  32. #include <Jolt/Core/ScopeExit.h>
  33. #ifdef JPH_DEBUG_RENDERER
  34. #include <Jolt/Renderer/DebugRenderer.h>
  35. #endif // JPH_DEBUG_RENDERER
  36. JPH_NAMESPACE_BEGIN
  37. #ifdef JPH_DEBUG_RENDERER
  38. bool PhysicsSystem::sDrawMotionQualityLinearCast = false;
  39. #endif // JPH_DEBUG_RENDERER
  40. //#define BROAD_PHASE BroadPhaseBruteForce
  41. #define BROAD_PHASE BroadPhaseQuadTree
  42. static const Color cColorUpdateBroadPhaseFinalize = Color::sGetDistinctColor(1);
  43. static const Color cColorUpdateBroadPhasePrepare = Color::sGetDistinctColor(2);
  44. static const Color cColorFindCollisions = Color::sGetDistinctColor(3);
  45. static const Color cColorApplyGravity = Color::sGetDistinctColor(4);
  46. static const Color cColorSetupVelocityConstraints = Color::sGetDistinctColor(5);
  47. static const Color cColorBuildIslandsFromConstraints = Color::sGetDistinctColor(6);
  48. static const Color cColorDetermineActiveConstraints = Color::sGetDistinctColor(7);
  49. static const Color cColorFinalizeIslands = Color::sGetDistinctColor(8);
  50. static const Color cColorContactRemovedCallbacks = Color::sGetDistinctColor(9);
  51. static const Color cColorBodySetIslandIndex = Color::sGetDistinctColor(10);
  52. static const Color cColorStartNextStep = Color::sGetDistinctColor(11);
  53. static const Color cColorSolveVelocityConstraints = Color::sGetDistinctColor(12);
  54. static const Color cColorPreIntegrateVelocity = Color::sGetDistinctColor(13);
  55. static const Color cColorIntegrateVelocity = Color::sGetDistinctColor(14);
  56. static const Color cColorPostIntegrateVelocity = Color::sGetDistinctColor(15);
  57. static const Color cColorResolveCCDContacts = Color::sGetDistinctColor(16);
  58. static const Color cColorSolvePositionConstraints = Color::sGetDistinctColor(17);
  59. static const Color cColorFindCCDContacts = Color::sGetDistinctColor(18);
  60. static const Color cColorStepListeners = Color::sGetDistinctColor(19);
  61. static const Color cColorSoftBodyPrepare = Color::sGetDistinctColor(20);
  62. static const Color cColorSoftBodyCollide = Color::sGetDistinctColor(21);
  63. static const Color cColorSoftBodySimulate = Color::sGetDistinctColor(22);
  64. static const Color cColorSoftBodyFinalize = Color::sGetDistinctColor(23);
  65. PhysicsSystem::~PhysicsSystem()
  66. {
  67. // Remove broadphase
  68. delete mBroadPhase;
  69. }
  70. void PhysicsSystem::Init(uint inMaxBodies, uint inNumBodyMutexes, uint inMaxBodyPairs, uint inMaxContactConstraints, const BroadPhaseLayerInterface &inBroadPhaseLayerInterface, const ObjectVsBroadPhaseLayerFilter &inObjectVsBroadPhaseLayerFilter, const ObjectLayerPairFilter &inObjectLayerPairFilter)
  71. {
  72. // Clamp max bodies
  73. uint max_bodies = min(inMaxBodies, cMaxBodiesLimit);
  74. JPH_ASSERT(max_bodies == inMaxBodies, "Cannot support this many bodies!");
  75. mObjectVsBroadPhaseLayerFilter = &inObjectVsBroadPhaseLayerFilter;
  76. mObjectLayerPairFilter = &inObjectLayerPairFilter;
  77. // Initialize body manager
  78. mBodyManager.Init(max_bodies, inNumBodyMutexes, inBroadPhaseLayerInterface);
  79. // Create broadphase
  80. mBroadPhase = new BROAD_PHASE();
  81. mBroadPhase->Init(&mBodyManager, inBroadPhaseLayerInterface);
  82. // Init contact constraint manager
  83. mContactManager.Init(inMaxBodyPairs, inMaxContactConstraints);
  84. // Init islands builder
  85. mIslandBuilder.Init(max_bodies);
  86. // Initialize body interface
  87. mBodyInterfaceLocking.Init(mBodyLockInterfaceLocking, mBodyManager, *mBroadPhase);
  88. mBodyInterfaceNoLock.Init(mBodyLockInterfaceNoLock, mBodyManager, *mBroadPhase);
  89. // Initialize narrow phase query
  90. mNarrowPhaseQueryLocking.Init(mBodyLockInterfaceLocking, *mBroadPhase);
  91. mNarrowPhaseQueryNoLock.Init(mBodyLockInterfaceNoLock, *mBroadPhase);
  92. }
  93. void PhysicsSystem::OptimizeBroadPhase()
  94. {
  95. mBroadPhase->Optimize();
  96. }
  97. void PhysicsSystem::AddStepListener(PhysicsStepListener *inListener)
  98. {
  99. lock_guard lock(mStepListenersMutex);
  100. JPH_ASSERT(std::find(mStepListeners.begin(), mStepListeners.end(), inListener) == mStepListeners.end());
  101. mStepListeners.push_back(inListener);
  102. }
  103. void PhysicsSystem::RemoveStepListener(PhysicsStepListener *inListener)
  104. {
  105. lock_guard lock(mStepListenersMutex);
  106. StepListeners::iterator i = std::find(mStepListeners.begin(), mStepListeners.end(), inListener);
  107. JPH_ASSERT(i != mStepListeners.end());
  108. *i = mStepListeners.back();
  109. mStepListeners.pop_back();
  110. }
  111. #ifdef JPH_TRACK_SIMULATION_STATS
  112. void PhysicsSystem::GatherIslandStats()
  113. {
  114. JPH_PROFILE_FUNCTION();
  115. for (uint32 island_idx = 0; island_idx < mIslandBuilder.GetNumIslands(); ++island_idx)
  116. {
  117. BodyID *bodies_begin, *bodies_end;
  118. mIslandBuilder.GetBodiesInIsland(island_idx, bodies_begin, bodies_end);
  119. uint64 num_bodies = bodies_end - bodies_begin;
  120. // Equally distribute the stats over all bodies
  121. const IslandBuilder::IslandStats &stats = mIslandBuilder.GetIslandStats(island_idx);
  122. uint64 num_velocity_ticks = stats.mVelocityConstraintTicks / num_bodies;
  123. uint64 num_position_ticks = stats.mPositionConstraintTicks / num_bodies;
  124. uint8 num_position_steps = (uint8)mIslandBuilder.GetNumPositionSteps(island_idx);
  125. for (BodyID *body_id = bodies_begin; body_id < bodies_end; ++body_id)
  126. {
  127. MotionProperties::SimulationStats &out_stats = mBodyManager.GetBody(*body_id).GetMotionProperties()->GetSimulationStats();
  128. out_stats.mNumVelocitySteps = stats.mNumVelocitySteps;
  129. out_stats.mNumPositionSteps = num_position_steps;
  130. out_stats.mVelocityConstraintTicks += num_velocity_ticks; // In case of multiple collision steps we accumulate
  131. out_stats.mPositionConstraintTicks += num_position_ticks;
  132. }
  133. }
  134. }
  135. #endif // JPH_TRACK_SIMULATION_STATS
  136. EPhysicsUpdateError PhysicsSystem::Update(float inDeltaTime, int inCollisionSteps, TempAllocator *inTempAllocator, JobSystem *inJobSystem)
  137. {
  138. JPH_PROFILE_FUNCTION();
  139. JPH_DET_LOG("PhysicsSystem::Update: dt: " << inDeltaTime << " steps: " << inCollisionSteps);
  140. JPH_ASSERT(inCollisionSteps > 0);
  141. JPH_ASSERT(inDeltaTime >= 0.0f);
  142. // Sync point for the broadphase. This will allow it to do clean up operations without having any mutexes locked yet.
  143. mBroadPhase->FrameSync();
  144. // If there are no active bodies (and no step listener to wake them up) or there's no time delta
  145. uint32 num_active_rigid_bodies = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody);
  146. uint32 num_active_soft_bodies = mBodyManager.GetNumActiveBodies(EBodyType::SoftBody);
  147. if ((num_active_rigid_bodies == 0 && num_active_soft_bodies == 0 && mStepListeners.empty()) || inDeltaTime <= 0.0f)
  148. {
  149. mBodyManager.LockAllBodies();
  150. // Update broadphase
  151. mBroadPhase->LockModifications();
  152. BroadPhase::UpdateState update_state = mBroadPhase->UpdatePrepare();
  153. mBroadPhase->UpdateFinalize(update_state);
  154. mBroadPhase->UnlockModifications();
  155. // If time has passed, call contact removal callbacks from contacts that existed in the previous update
  156. if (inDeltaTime > 0.0f)
  157. mContactManager.FinalizeContactCacheAndCallContactPointRemovedCallbacks(0, 0);
  158. mBodyManager.UnlockAllBodies();
  159. return EPhysicsUpdateError::None;
  160. }
  161. #ifdef JPH_TRACK_SIMULATION_STATS
  162. // Reset accumulated stats on the active bodies
  163. mBodyManager.ResetSimulationStats();
  164. #endif
  165. // Calculate ratio between current and previous frame delta time to scale initial constraint forces
  166. float step_delta_time = inDeltaTime / inCollisionSteps;
  167. float warm_start_impulse_ratio = mPhysicsSettings.mConstraintWarmStart && mPreviousStepDeltaTime > 0.0f? step_delta_time / mPreviousStepDeltaTime : 0.0f;
  168. mPreviousStepDeltaTime = step_delta_time;
  169. // Create the context used for passing information between jobs
  170. PhysicsUpdateContext context(*inTempAllocator);
  171. context.mPhysicsSystem = this;
  172. context.mJobSystem = inJobSystem;
  173. context.mBarrier = inJobSystem->CreateBarrier();
  174. context.mIslandBuilder = &mIslandBuilder;
  175. context.mStepDeltaTime = step_delta_time;
  176. context.mWarmStartImpulseRatio = warm_start_impulse_ratio;
  177. context.mSteps.resize(inCollisionSteps);
  178. // Allocate space for body pairs
  179. JPH_ASSERT(context.mBodyPairs == nullptr);
  180. context.mBodyPairs = static_cast<BodyPair *>(inTempAllocator->Allocate(sizeof(BodyPair) * mPhysicsSettings.mMaxInFlightBodyPairs));
  181. // Lock all bodies for write so that we can freely touch them
  182. mStepListenersMutex.lock();
  183. mBodyManager.LockAllBodies();
  184. mBroadPhase->LockModifications();
  185. // Get max number of concurrent jobs
  186. int max_concurrency = context.GetMaxConcurrency();
  187. // Calculate how many step listener jobs we spawn
  188. int num_step_listener_jobs = mStepListeners.empty()? 0 : max(1, min((int)mStepListeners.size() / mPhysicsSettings.mStepListenersBatchSize / mPhysicsSettings.mStepListenerBatchesPerJob, max_concurrency));
  189. // Number of gravity jobs depends on the amount of active bodies.
  190. // Launch max 1 job per batch of active bodies
  191. // Leave 1 thread for update broadphase prepare and 1 for determine active constraints
  192. int num_apply_gravity_jobs = max(1, min(((int)num_active_rigid_bodies + cApplyGravityBatchSize - 1) / cApplyGravityBatchSize, max_concurrency - 2));
  193. // Number of determine active constraints jobs to run depends on number of constraints.
  194. // Leave 1 thread for update broadphase prepare and 1 for apply gravity
  195. int num_determine_active_constraints_jobs = max(1, min(((int)mConstraintManager.GetNumConstraints() + cDetermineActiveConstraintsBatchSize - 1) / cDetermineActiveConstraintsBatchSize, max_concurrency - 2));
  196. // Number of setup velocity constraints jobs to run depends on number of constraints.
  197. int num_setup_velocity_constraints_jobs = max(1, min(((int)mConstraintManager.GetNumConstraints() + cSetupVelocityConstraintsBatchSize - 1) / cSetupVelocityConstraintsBatchSize, max_concurrency));
  198. // Number of find collisions jobs to run depends on number of active bodies.
  199. // Note that when we have more than 1 thread, we always spawn at least 2 find collisions jobs so that the first job can wait for build islands from constraints
  200. // (which may activate additional bodies that need to be processed) while the second job can start processing collision work.
  201. int num_find_collisions_jobs = max(max_concurrency == 1? 1 : 2, min(((int)num_active_rigid_bodies + cActiveBodiesBatchSize - 1) / cActiveBodiesBatchSize, max_concurrency));
  202. // Number of integrate velocity jobs depends on number of active bodies.
  203. int num_integrate_velocity_jobs = max(1, min(((int)num_active_rigid_bodies + cIntegrateVelocityBatchSize - 1) / cIntegrateVelocityBatchSize, max_concurrency));
  204. {
  205. JPH_PROFILE("Build Jobs");
  206. // Iterate over collision steps
  207. for (int step_idx = 0; step_idx < inCollisionSteps; ++step_idx)
  208. {
  209. bool is_first_step = step_idx == 0;
  210. bool is_last_step = step_idx == inCollisionSteps - 1;
  211. PhysicsUpdateContext::Step &step = context.mSteps[step_idx];
  212. step.mContext = &context;
  213. step.mIsFirst = is_first_step;
  214. step.mIsLast = is_last_step;
  215. // Create job to do broadphase finalization
  216. // This job must finish before integrating velocities. Until then the positions will not be updated neither will bodies be added / removed.
  217. step.mUpdateBroadphaseFinalize = inJobSystem->CreateJob("UpdateBroadPhaseFinalize", cColorUpdateBroadPhaseFinalize, [&context, &step]()
  218. {
  219. // Validate that all find collision jobs have stopped
  220. JPH_ASSERT(step.mActiveFindCollisionJobs.load(memory_order_relaxed) == 0);
  221. // Finalize the broadphase update
  222. context.mPhysicsSystem->mBroadPhase->UpdateFinalize(step.mBroadPhaseUpdateState);
  223. // Signal that it is done
  224. step.mPreIntegrateVelocity.RemoveDependency();
  225. }, num_find_collisions_jobs + 2); // depends on: find collisions, broadphase prepare update, finish building jobs
  226. // The immediate jobs below are only immediate for the first step, the all finished job will kick them for the next step
  227. int previous_step_dependency_count = is_first_step? 0 : 1;
  228. // Start job immediately: Start the prepare broadphase
  229. // Must be done under body lock protection since the order is body locks then broadphase mutex
  230. // If this is turned around the RemoveBody call will hang since it locks in that order
  231. step.mBroadPhasePrepare = inJobSystem->CreateJob("UpdateBroadPhasePrepare", cColorUpdateBroadPhasePrepare, [&context, &step]()
  232. {
  233. // Prepare the broadphase update
  234. step.mBroadPhaseUpdateState = context.mPhysicsSystem->mBroadPhase->UpdatePrepare();
  235. // Now the finalize can run (if other dependencies are met too)
  236. step.mUpdateBroadphaseFinalize.RemoveDependency();
  237. }, previous_step_dependency_count);
  238. // This job will find all collisions
  239. step.mBodyPairQueues.resize(max_concurrency);
  240. step.mMaxBodyPairsPerQueue = mPhysicsSettings.mMaxInFlightBodyPairs / max_concurrency;
  241. step.mActiveFindCollisionJobs.store(~PhysicsUpdateContext::JobMask(0) >> (sizeof(PhysicsUpdateContext::JobMask) * 8 - num_find_collisions_jobs), memory_order_release);
  242. step.mFindCollisions.resize(num_find_collisions_jobs);
  243. for (int i = 0; i < num_find_collisions_jobs; ++i)
  244. {
  245. // Build islands from constraints may activate additional bodies, so the first job will wait for this to finish in order to not miss any active bodies
  246. int num_dep_build_islands_from_constraints = i == 0? 1 : 0;
  247. step.mFindCollisions[i] = inJobSystem->CreateJob("FindCollisions", cColorFindCollisions, [&step, i]()
  248. {
  249. step.mContext->mPhysicsSystem->JobFindCollisions(&step, i);
  250. }, num_apply_gravity_jobs + num_determine_active_constraints_jobs + 1 + num_dep_build_islands_from_constraints); // depends on: apply gravity, determine active constraints, finish building jobs, build islands from constraints
  251. }
  252. if (is_first_step)
  253. {
  254. #ifdef JPH_ENABLE_ASSERTS
  255. // Don't allow write operations to the active bodies list
  256. mBodyManager.SetActiveBodiesLocked(true);
  257. #endif
  258. // Store the number of active bodies at the start of the step
  259. step.mNumActiveBodiesAtStepStart = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody);
  260. // Lock all constraints
  261. mConstraintManager.LockAllConstraints();
  262. // Allocate memory for storing the active constraints
  263. JPH_ASSERT(context.mActiveConstraints == nullptr);
  264. context.mActiveConstraints = static_cast<Constraint **>(inTempAllocator->Allocate(mConstraintManager.GetNumConstraints() * sizeof(Constraint *)));
  265. // Prepare contact buffer
  266. mContactManager.PrepareConstraintBuffer(&context);
  267. // Setup island builder
  268. mIslandBuilder.PrepareContactConstraints(mContactManager.GetMaxConstraints(), context.mTempAllocator);
  269. }
  270. // This job applies gravity to all active bodies
  271. step.mApplyGravity.resize(num_apply_gravity_jobs);
  272. for (int i = 0; i < num_apply_gravity_jobs; ++i)
  273. step.mApplyGravity[i] = inJobSystem->CreateJob("ApplyGravity", cColorApplyGravity, [&context, &step]()
  274. {
  275. context.mPhysicsSystem->JobApplyGravity(&context, &step);
  276. JobHandle::sRemoveDependencies(step.mFindCollisions);
  277. }, num_step_listener_jobs > 0? num_step_listener_jobs : previous_step_dependency_count); // depends on: step listeners (or previous step if no step listeners)
  278. // This job will setup velocity constraints for non-collision constraints
  279. step.mSetupVelocityConstraints.resize(num_setup_velocity_constraints_jobs);
  280. for (int i = 0; i < num_setup_velocity_constraints_jobs; ++i)
  281. step.mSetupVelocityConstraints[i] = inJobSystem->CreateJob("SetupVelocityConstraints", cColorSetupVelocityConstraints, [&context, &step]()
  282. {
  283. context.mPhysicsSystem->JobSetupVelocityConstraints(context.mStepDeltaTime, &step);
  284. JobHandle::sRemoveDependencies(step.mSolveVelocityConstraints);
  285. }, num_determine_active_constraints_jobs + 1); // depends on: determine active constraints, finish building jobs
  286. // This job will build islands from constraints
  287. step.mBuildIslandsFromConstraints = inJobSystem->CreateJob("BuildIslandsFromConstraints", cColorBuildIslandsFromConstraints, [&context, &step]()
  288. {
  289. context.mPhysicsSystem->JobBuildIslandsFromConstraints(&context, &step);
  290. step.mFindCollisions[0].RemoveDependency(); // The first collisions job cannot start running until we've finished building islands and activated all bodies
  291. step.mFinalizeIslands.RemoveDependency();
  292. }, num_determine_active_constraints_jobs + 1); // depends on: determine active constraints, finish building jobs
  293. // This job determines active constraints
  294. step.mDetermineActiveConstraints.resize(num_determine_active_constraints_jobs);
  295. for (int i = 0; i < num_determine_active_constraints_jobs; ++i)
  296. step.mDetermineActiveConstraints[i] = inJobSystem->CreateJob("DetermineActiveConstraints", cColorDetermineActiveConstraints, [&context, &step]()
  297. {
  298. context.mPhysicsSystem->JobDetermineActiveConstraints(&step);
  299. step.mBuildIslandsFromConstraints.RemoveDependency();
  300. // Kick these jobs last as they will use up all CPU cores leaving no space for the previous job, we prefer setup velocity constraints to finish first so we kick it first
  301. JobHandle::sRemoveDependencies(step.mSetupVelocityConstraints);
  302. JobHandle::sRemoveDependencies(step.mFindCollisions);
  303. }, num_step_listener_jobs > 0? num_step_listener_jobs : previous_step_dependency_count); // depends on: step listeners (or previous step if no step listeners)
  304. // This job calls the step listeners
  305. step.mStepListeners.resize(num_step_listener_jobs);
  306. for (int i = 0; i < num_step_listener_jobs; ++i)
  307. step.mStepListeners[i] = inJobSystem->CreateJob("StepListeners", cColorStepListeners, [&context, &step]()
  308. {
  309. // Call the step listeners
  310. context.mPhysicsSystem->JobStepListeners(&step);
  311. // Kick apply gravity and determine active constraint jobs
  312. JobHandle::sRemoveDependencies(step.mApplyGravity);
  313. JobHandle::sRemoveDependencies(step.mDetermineActiveConstraints);
  314. }, previous_step_dependency_count);
  315. // Unblock the previous step
  316. if (!is_first_step)
  317. context.mSteps[step_idx - 1].mStartNextStep.RemoveDependency();
  318. // This job will finalize the simulation islands
  319. step.mFinalizeIslands = inJobSystem->CreateJob("FinalizeIslands", cColorFinalizeIslands, [&context, &step]()
  320. {
  321. // Validate that all find collision jobs have stopped
  322. JPH_ASSERT(step.mActiveFindCollisionJobs.load(memory_order_relaxed) == 0);
  323. context.mPhysicsSystem->JobFinalizeIslands(&context);
  324. JobHandle::sRemoveDependencies(step.mSolveVelocityConstraints);
  325. step.mBodySetIslandIndex.RemoveDependency();
  326. }, num_find_collisions_jobs + 2); // depends on: find collisions, build islands from constraints, finish building jobs
  327. // Unblock previous job
  328. // Note: technically we could release find collisions here but we don't want to because that could make them run before 'setup velocity constraints' which means that job won't have a thread left
  329. step.mBuildIslandsFromConstraints.RemoveDependency();
  330. // This job will call the contact removed callbacks
  331. step.mContactRemovedCallbacks = inJobSystem->CreateJob("ContactRemovedCallbacks", cColorContactRemovedCallbacks, [&context, &step]()
  332. {
  333. context.mPhysicsSystem->JobContactRemovedCallbacks(&step);
  334. if (step.mStartNextStep.IsValid())
  335. step.mStartNextStep.RemoveDependency();
  336. }, 1); // depends on the find ccd contacts
  337. // This job will set the island index on each body (only used for debug drawing purposes)
  338. // It will also delete any bodies that have been destroyed in the last frame
  339. step.mBodySetIslandIndex = inJobSystem->CreateJob("BodySetIslandIndex", cColorBodySetIslandIndex, [&context, &step]()
  340. {
  341. context.mPhysicsSystem->JobBodySetIslandIndex();
  342. JobHandle::sRemoveDependencies(step.mSolvePositionConstraints);
  343. }, 2); // depends on: finalize islands, finish building jobs
  344. // Job to start the next collision step
  345. if (!is_last_step)
  346. {
  347. PhysicsUpdateContext::Step *next_step = &context.mSteps[step_idx + 1];
  348. step.mStartNextStep = inJobSystem->CreateJob("StartNextStep", cColorStartNextStep, [this, next_step]()
  349. {
  350. #ifdef JPH_DEBUG
  351. // Validate that the cached bounds are correct
  352. mBodyManager.ValidateActiveBodyBounds();
  353. #endif // JPH_DEBUG
  354. #ifdef JPH_TRACK_SIMULATION_STATS
  355. // Gather stats from the islands and distribute them over the bodies
  356. GatherIslandStats();
  357. #endif
  358. // Store the number of active bodies at the start of the step
  359. next_step->mNumActiveBodiesAtStepStart = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody);
  360. // Clear the large island splitter
  361. TempAllocator *temp_allocator = next_step->mContext->mTempAllocator;
  362. mLargeIslandSplitter.Reset(temp_allocator);
  363. // Clear the island builder
  364. mIslandBuilder.ResetIslands(temp_allocator);
  365. // Setup island builder
  366. mIslandBuilder.PrepareContactConstraints(mContactManager.GetMaxConstraints(), temp_allocator);
  367. // Restart the contact manager
  368. mContactManager.RecycleConstraintBuffer();
  369. // Kick the jobs of the next step (in the same order as the first step)
  370. next_step->mBroadPhasePrepare.RemoveDependency();
  371. if (next_step->mStepListeners.empty())
  372. {
  373. // Kick the gravity and active constraints jobs immediately
  374. JobHandle::sRemoveDependencies(next_step->mApplyGravity);
  375. JobHandle::sRemoveDependencies(next_step->mDetermineActiveConstraints);
  376. }
  377. else
  378. {
  379. // Kick the step listeners job first
  380. JobHandle::sRemoveDependencies(next_step->mStepListeners);
  381. }
  382. }, 3); // depends on: update soft bodies, contact removed callbacks, finish building the previous step
  383. }
  384. // This job will solve the velocity constraints
  385. step.mSolveVelocityConstraints.resize(max_concurrency);
  386. for (int i = 0; i < max_concurrency; ++i)
  387. step.mSolveVelocityConstraints[i] = inJobSystem->CreateJob("SolveVelocityConstraints", cColorSolveVelocityConstraints, [&context, &step]()
  388. {
  389. context.mPhysicsSystem->JobSolveVelocityConstraints(&context, &step);
  390. step.mPreIntegrateVelocity.RemoveDependency();
  391. }, num_setup_velocity_constraints_jobs + 2); // depends on: finalize islands, setup velocity constraints, finish building jobs.
  392. // We prefer setup velocity constraints to finish first so we kick it first
  393. JobHandle::sRemoveDependencies(step.mSetupVelocityConstraints);
  394. JobHandle::sRemoveDependencies(step.mFindCollisions);
  395. // Finalize islands is a dependency on find collisions so it can go last
  396. step.mFinalizeIslands.RemoveDependency();
  397. // This job will prepare the position update of all active bodies
  398. step.mPreIntegrateVelocity = inJobSystem->CreateJob("PreIntegrateVelocity", cColorPreIntegrateVelocity, [&context, &step]()
  399. {
  400. context.mPhysicsSystem->JobPreIntegrateVelocity(&context, &step);
  401. JobHandle::sRemoveDependencies(step.mIntegrateVelocity);
  402. }, 2 + max_concurrency); // depends on: broadphase update finalize, solve velocity constraints, finish building jobs.
  403. // Unblock previous jobs
  404. step.mUpdateBroadphaseFinalize.RemoveDependency();
  405. JobHandle::sRemoveDependencies(step.mSolveVelocityConstraints);
  406. // This job will update the positions of all active bodies
  407. step.mIntegrateVelocity.resize(num_integrate_velocity_jobs);
  408. for (int i = 0; i < num_integrate_velocity_jobs; ++i)
  409. step.mIntegrateVelocity[i] = inJobSystem->CreateJob("IntegrateVelocity", cColorIntegrateVelocity, [&context, &step]()
  410. {
  411. context.mPhysicsSystem->JobIntegrateVelocity(&context, &step);
  412. step.mPostIntegrateVelocity.RemoveDependency();
  413. }, 2); // depends on: pre integrate velocity, finish building jobs.
  414. // Unblock previous job
  415. step.mPreIntegrateVelocity.RemoveDependency();
  416. // This job will finish the position update of all active bodies
  417. step.mPostIntegrateVelocity = inJobSystem->CreateJob("PostIntegrateVelocity", cColorPostIntegrateVelocity, [&context, &step]()
  418. {
  419. context.mPhysicsSystem->JobPostIntegrateVelocity(&context, &step);
  420. step.mResolveCCDContacts.RemoveDependency();
  421. }, num_integrate_velocity_jobs + 1); // depends on: integrate velocity, finish building jobs
  422. // Unblock previous jobs
  423. JobHandle::sRemoveDependencies(step.mIntegrateVelocity);
  424. // This job will update the positions and velocities for all bodies that need continuous collision detection
  425. step.mResolveCCDContacts = inJobSystem->CreateJob("ResolveCCDContacts", cColorResolveCCDContacts, [&context, &step]()
  426. {
  427. context.mPhysicsSystem->JobResolveCCDContacts(&context, &step);
  428. JobHandle::sRemoveDependencies(step.mSolvePositionConstraints);
  429. }, 2); // depends on: integrate velocities, detect ccd contacts (added dynamically), finish building jobs.
  430. // Unblock previous job
  431. step.mPostIntegrateVelocity.RemoveDependency();
  432. // Fixes up drift in positions and updates the broadphase with new body positions
  433. step.mSolvePositionConstraints.resize(max_concurrency);
  434. for (int i = 0; i < max_concurrency; ++i)
  435. step.mSolvePositionConstraints[i] = inJobSystem->CreateJob("SolvePositionConstraints", cColorSolvePositionConstraints, [&context, &step]()
  436. {
  437. context.mPhysicsSystem->JobSolvePositionConstraints(&context, &step);
  438. // Kick the next step
  439. if (step.mSoftBodyPrepare.IsValid())
  440. step.mSoftBodyPrepare.RemoveDependency();
  441. }, 3); // depends on: resolve ccd contacts, body set island index, finish building jobs.
  442. // Unblock previous jobs.
  443. step.mResolveCCDContacts.RemoveDependency();
  444. step.mBodySetIslandIndex.RemoveDependency();
  445. // The soft body prepare job will create other jobs if needed
  446. step.mSoftBodyPrepare = inJobSystem->CreateJob("SoftBodyPrepare", cColorSoftBodyPrepare, [&context, &step]()
  447. {
  448. context.mPhysicsSystem->JobSoftBodyPrepare(&context, &step);
  449. }, max_concurrency); // depends on: solve position constraints.
  450. // Unblock previous jobs
  451. JobHandle::sRemoveDependencies(step.mSolvePositionConstraints);
  452. }
  453. }
  454. // Build the list of jobs to wait for
  455. JobSystem::Barrier *barrier = context.mBarrier;
  456. {
  457. JPH_PROFILE("Build job barrier");
  458. StaticArray<JobHandle, cMaxPhysicsJobs> handles;
  459. for (const PhysicsUpdateContext::Step &step : context.mSteps)
  460. {
  461. if (step.mBroadPhasePrepare.IsValid())
  462. handles.push_back(step.mBroadPhasePrepare);
  463. for (const JobHandle &h : step.mStepListeners)
  464. handles.push_back(h);
  465. for (const JobHandle &h : step.mDetermineActiveConstraints)
  466. handles.push_back(h);
  467. for (const JobHandle &h : step.mApplyGravity)
  468. handles.push_back(h);
  469. for (const JobHandle &h : step.mFindCollisions)
  470. handles.push_back(h);
  471. if (step.mUpdateBroadphaseFinalize.IsValid())
  472. handles.push_back(step.mUpdateBroadphaseFinalize);
  473. for (const JobHandle &h : step.mSetupVelocityConstraints)
  474. handles.push_back(h);
  475. handles.push_back(step.mBuildIslandsFromConstraints);
  476. handles.push_back(step.mFinalizeIslands);
  477. handles.push_back(step.mBodySetIslandIndex);
  478. for (const JobHandle &h : step.mSolveVelocityConstraints)
  479. handles.push_back(h);
  480. handles.push_back(step.mPreIntegrateVelocity);
  481. for (const JobHandle &h : step.mIntegrateVelocity)
  482. handles.push_back(h);
  483. handles.push_back(step.mPostIntegrateVelocity);
  484. handles.push_back(step.mResolveCCDContacts);
  485. for (const JobHandle &h : step.mSolvePositionConstraints)
  486. handles.push_back(h);
  487. handles.push_back(step.mContactRemovedCallbacks);
  488. if (step.mSoftBodyPrepare.IsValid())
  489. handles.push_back(step.mSoftBodyPrepare);
  490. if (step.mStartNextStep.IsValid())
  491. handles.push_back(step.mStartNextStep);
  492. }
  493. barrier->AddJobs(handles.data(), handles.size());
  494. }
  495. // Wait until all jobs finish
  496. // Note we don't just wait for the last job. If we would and another job
  497. // would be scheduled in between there is the possibility of a deadlock.
  498. // The other job could try to e.g. add/remove a body which would try to
  499. // lock a body mutex while this thread has already locked the mutex
  500. inJobSystem->WaitForJobs(barrier);
  501. // We're done with the barrier for this update
  502. inJobSystem->DestroyBarrier(barrier);
  503. #ifdef JPH_DEBUG
  504. // Validate that the cached bounds are correct
  505. mBodyManager.ValidateActiveBodyBounds();
  506. #endif // JPH_DEBUG
  507. #ifdef JPH_TRACK_SIMULATION_STATS
  508. // Gather stats from the islands and distribute them over the bodies
  509. GatherIslandStats();
  510. #endif
  511. // Clear the large island splitter
  512. mLargeIslandSplitter.Reset(inTempAllocator);
  513. // Clear the island builder
  514. mIslandBuilder.ResetIslands(inTempAllocator);
  515. // Clear the contact manager
  516. mContactManager.FinishConstraintBuffer();
  517. // Free active constraints
  518. inTempAllocator->Free(context.mActiveConstraints, mConstraintManager.GetNumConstraints() * sizeof(Constraint *));
  519. context.mActiveConstraints = nullptr;
  520. // Free body pairs
  521. inTempAllocator->Free(context.mBodyPairs, sizeof(BodyPair) * mPhysicsSettings.mMaxInFlightBodyPairs);
  522. context.mBodyPairs = nullptr;
  523. // Unlock the broadphase
  524. mBroadPhase->UnlockModifications();
  525. // Unlock all constraints
  526. mConstraintManager.UnlockAllConstraints();
  527. #ifdef JPH_ENABLE_ASSERTS
  528. // Allow write operations to the active bodies list
  529. mBodyManager.SetActiveBodiesLocked(false);
  530. #endif
  531. // Unlock all bodies
  532. mBodyManager.UnlockAllBodies();
  533. // Unlock step listeners
  534. mStepListenersMutex.unlock();
  535. // Return any errors
  536. EPhysicsUpdateError errors = static_cast<EPhysicsUpdateError>(context.mErrors.load(memory_order_acquire));
  537. JPH_ASSERT(errors == EPhysicsUpdateError::None, "An error occurred during the physics update, see EPhysicsUpdateError for more information");
  538. return errors;
  539. }
  540. void PhysicsSystem::JobStepListeners(PhysicsUpdateContext::Step *ioStep)
  541. {
  542. #ifdef JPH_ENABLE_ASSERTS
  543. // Read positions (broadphase updates concurrently so we can't write), read/write velocities
  544. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  545. // Can activate bodies only (we cache the amount of active bodies at the beginning of the step in mNumActiveBodiesAtStepStart so we cannot deactivate here)
  546. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  547. #endif
  548. PhysicsStepListenerContext context;
  549. context.mDeltaTime = ioStep->mContext->mStepDeltaTime;
  550. context.mIsFirstStep = ioStep->mIsFirst;
  551. context.mIsLastStep = ioStep->mIsLast;
  552. context.mPhysicsSystem = this;
  553. uint32 batch_size = mPhysicsSettings.mStepListenersBatchSize;
  554. for (;;)
  555. {
  556. // Get the start of a new batch
  557. uint32 batch = ioStep->mStepListenerReadIdx.fetch_add(batch_size);
  558. if (batch >= mStepListeners.size())
  559. break;
  560. // Call the listeners
  561. for (uint32 i = batch, i_end = min((uint32)mStepListeners.size(), batch + batch_size); i < i_end; ++i)
  562. mStepListeners[i]->OnStep(context);
  563. }
  564. }
  565. void PhysicsSystem::JobDetermineActiveConstraints(PhysicsUpdateContext::Step *ioStep) const
  566. {
  567. #ifdef JPH_ENABLE_ASSERTS
  568. // No body access
  569. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  570. #endif
  571. uint32 num_constraints = mConstraintManager.GetNumConstraints();
  572. uint32 num_active_constraints;
  573. Constraint **active_constraints = (Constraint **)JPH_STACK_ALLOC(cDetermineActiveConstraintsBatchSize * sizeof(Constraint *));
  574. for (;;)
  575. {
  576. // Atomically fetch a batch of constraints
  577. uint32 constraint_idx = ioStep->mDetermineActiveConstraintReadIdx.fetch_add(cDetermineActiveConstraintsBatchSize);
  578. if (constraint_idx >= num_constraints)
  579. break;
  580. // Calculate the end of the batch
  581. uint32 constraint_idx_end = min(num_constraints, constraint_idx + cDetermineActiveConstraintsBatchSize);
  582. // 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)
  583. mConstraintManager.GetActiveConstraints(constraint_idx, constraint_idx_end, active_constraints, num_active_constraints);
  584. // Copy the block of active constraints to the global list of active constraints
  585. if (num_active_constraints > 0)
  586. {
  587. uint32 active_constraint_idx = ioStep->mNumActiveConstraints.fetch_add(num_active_constraints);
  588. memcpy(ioStep->mContext->mActiveConstraints + active_constraint_idx, active_constraints, num_active_constraints * sizeof(Constraint *));
  589. }
  590. }
  591. }
  592. void PhysicsSystem::JobApplyGravity(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  593. {
  594. #ifdef JPH_ENABLE_ASSERTS
  595. // We update velocities and need the rotation to do so
  596. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  597. #endif
  598. // Get list of active bodies that we had at the start of the physics update.
  599. // Any body that is activated as part of the simulation step does not receive gravity this frame.
  600. // Note that bodies may be activated during this job but not deactivated, this means that only elements
  601. // will be added to the array. Since the array is made to not reallocate, this is a safe operation.
  602. const BodyID *active_bodies = mBodyManager.GetActiveBodiesUnsafe(EBodyType::RigidBody);
  603. uint32 num_active_bodies_at_step_start = ioStep->mNumActiveBodiesAtStepStart;
  604. // Fetch delta time once outside the loop
  605. float delta_time = ioContext->mStepDeltaTime;
  606. // Update velocities from forces
  607. for (;;)
  608. {
  609. // Atomically fetch a batch of bodies
  610. uint32 active_body_idx = ioStep->mApplyGravityReadIdx.fetch_add(cApplyGravityBatchSize);
  611. if (active_body_idx >= num_active_bodies_at_step_start)
  612. break;
  613. // Calculate the end of the batch
  614. uint32 active_body_idx_end = min(num_active_bodies_at_step_start, active_body_idx + cApplyGravityBatchSize);
  615. // Process the batch
  616. while (active_body_idx < active_body_idx_end)
  617. {
  618. Body &body = mBodyManager.GetBody(active_bodies[active_body_idx]);
  619. if (body.IsDynamic())
  620. {
  621. MotionProperties *mp = body.GetMotionProperties();
  622. Quat rotation = body.GetRotation();
  623. if (body.GetApplyGyroscopicForce())
  624. mp->ApplyGyroscopicForceInternal(rotation, delta_time);
  625. mp->ApplyForceTorqueAndDragInternal(rotation, mGravity, delta_time);
  626. }
  627. active_body_idx++;
  628. }
  629. }
  630. }
  631. void PhysicsSystem::JobSetupVelocityConstraints(float inDeltaTime, PhysicsUpdateContext::Step *ioStep) const
  632. {
  633. #ifdef JPH_ENABLE_ASSERTS
  634. // We only read positions
  635. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
  636. #endif
  637. uint32 num_constraints = ioStep->mNumActiveConstraints;
  638. for (;;)
  639. {
  640. // Atomically fetch a batch of constraints
  641. uint32 constraint_idx = ioStep->mSetupVelocityConstraintsReadIdx.fetch_add(cSetupVelocityConstraintsBatchSize);
  642. if (constraint_idx >= num_constraints)
  643. break;
  644. ConstraintManager::sSetupVelocityConstraints(ioStep->mContext->mActiveConstraints + constraint_idx, min<uint32>(cSetupVelocityConstraintsBatchSize, num_constraints - constraint_idx), inDeltaTime);
  645. }
  646. }
  647. void PhysicsSystem::JobBuildIslandsFromConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  648. {
  649. #ifdef JPH_ENABLE_ASSERTS
  650. // We read constraints and positions
  651. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
  652. // Can only activate bodies
  653. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  654. #endif
  655. // Prepare the island builder
  656. mIslandBuilder.PrepareNonContactConstraints(ioStep->mNumActiveConstraints, ioContext->mTempAllocator);
  657. // Build the islands
  658. ConstraintManager::sBuildIslands(ioStep->mContext->mActiveConstraints, ioStep->mNumActiveConstraints, mIslandBuilder, mBodyManager);
  659. }
  660. void PhysicsSystem::TrySpawnJobFindCollisions(PhysicsUpdateContext::Step *ioStep) const
  661. {
  662. // Get how many jobs we can spawn and check if we can spawn more
  663. uint max_jobs = ioStep->mBodyPairQueues.size();
  664. if (CountBits(ioStep->mActiveFindCollisionJobs.load(memory_order_relaxed)) >= max_jobs)
  665. return;
  666. // Count how many body pairs we have waiting
  667. uint32 num_body_pairs = 0;
  668. for (const PhysicsUpdateContext::BodyPairQueue &queue : ioStep->mBodyPairQueues)
  669. num_body_pairs += queue.mWriteIdx - queue.mReadIdx;
  670. // Count how many active bodies we have waiting
  671. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody) - ioStep->mActiveBodyReadIdx;
  672. // Calculate how many jobs we would like
  673. uint desired_num_jobs = min((num_body_pairs + cNarrowPhaseBatchSize - 1) / cNarrowPhaseBatchSize + (num_active_bodies + cActiveBodiesBatchSize - 1) / cActiveBodiesBatchSize, max_jobs);
  674. for (;;)
  675. {
  676. // Get the bit mask of active jobs and see if we can spawn more
  677. PhysicsUpdateContext::JobMask current_active_jobs = ioStep->mActiveFindCollisionJobs.load(memory_order_relaxed);
  678. uint job_index = CountTrailingZeros(~current_active_jobs);
  679. if (job_index >= desired_num_jobs)
  680. break;
  681. // Try to claim the job index
  682. PhysicsUpdateContext::JobMask job_mask = PhysicsUpdateContext::JobMask(1) << job_index;
  683. PhysicsUpdateContext::JobMask prev_value = ioStep->mActiveFindCollisionJobs.fetch_or(job_mask, memory_order_acquire);
  684. if ((prev_value & job_mask) == 0)
  685. {
  686. // Add dependencies from the find collisions job to the next jobs
  687. ioStep->mUpdateBroadphaseFinalize.AddDependency();
  688. ioStep->mFinalizeIslands.AddDependency();
  689. // Start the job
  690. JobHandle job = ioStep->mContext->mJobSystem->CreateJob("FindCollisions", cColorFindCollisions, [step = ioStep, job_index]()
  691. {
  692. step->mContext->mPhysicsSystem->JobFindCollisions(step, job_index);
  693. });
  694. // Add the job to the job barrier so the main updating thread can execute the job too
  695. ioStep->mContext->mBarrier->AddJob(job);
  696. // Spawn only 1 extra job at a time
  697. return;
  698. }
  699. }
  700. }
  701. static void sFinalizeContactAllocator(PhysicsUpdateContext::Step &ioStep, const ContactConstraintManager::ContactAllocator &inAllocator)
  702. {
  703. // Atomically accumulate the number of found manifolds and body pairs
  704. ioStep.mNumBodyPairs.fetch_add(inAllocator.mNumBodyPairs, memory_order_relaxed);
  705. ioStep.mNumManifolds.fetch_add(inAllocator.mNumManifolds, memory_order_relaxed);
  706. // Combine update errors
  707. ioStep.mContext->mErrors.fetch_or((uint32)inAllocator.mErrors, memory_order_relaxed);
  708. }
  709. // Disable TSAN for this function. It detects a false positive race condition on mBodyPairs.
  710. // We have written mBodyPairs before doing mWriteIdx++ and we check mWriteIdx before reading mBodyPairs, so this should be safe.
  711. JPH_TSAN_NO_SANITIZE
  712. void PhysicsSystem::JobFindCollisions(PhysicsUpdateContext::Step *ioStep, int inJobIndex)
  713. {
  714. #ifdef JPH_ENABLE_ASSERTS
  715. // We read positions and read velocities (for elastic collisions)
  716. BodyAccess::Grant grant(BodyAccess::EAccess::Read, BodyAccess::EAccess::Read);
  717. // Can only activate bodies
  718. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  719. #endif
  720. // Allocation context for allocating new contact points
  721. ContactAllocator contact_allocator(mContactManager.GetContactAllocator());
  722. // Determine initial queue to read pairs from if no broadphase work can be done
  723. // (always start looking at results from the next job)
  724. int read_queue_idx = (inJobIndex + 1) % ioStep->mBodyPairQueues.size();
  725. // Allocate space to temporarily store a batch of active bodies
  726. BodyID *active_bodies = (BodyID *)JPH_STACK_ALLOC(cActiveBodiesBatchSize * sizeof(BodyID));
  727. for (;;)
  728. {
  729. // Check if there are active bodies to be processed
  730. uint32 active_bodies_read_idx = ioStep->mActiveBodyReadIdx;
  731. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody);
  732. if (active_bodies_read_idx < num_active_bodies)
  733. {
  734. // Take a batch of active bodies
  735. uint32 active_bodies_read_idx_end = min(num_active_bodies, active_bodies_read_idx + cActiveBodiesBatchSize);
  736. if (ioStep->mActiveBodyReadIdx.compare_exchange_strong(active_bodies_read_idx, active_bodies_read_idx_end))
  737. {
  738. // Callback when a new body pair is found
  739. class MyBodyPairCallback : public BodyPairCollector
  740. {
  741. public:
  742. // Constructor
  743. MyBodyPairCallback(PhysicsUpdateContext::Step *inStep, ContactAllocator &ioContactAllocator, int inJobIndex) :
  744. mStep(inStep),
  745. mContactAllocator(ioContactAllocator),
  746. mJobIndex(inJobIndex)
  747. {
  748. }
  749. // Callback function when a body pair is found
  750. virtual void AddHit(const BodyPair &inPair) override
  751. {
  752. // Check if we have space in our write queue
  753. PhysicsUpdateContext::BodyPairQueue &queue = mStep->mBodyPairQueues[mJobIndex];
  754. uint32 body_pairs_in_queue = queue.mWriteIdx - queue.mReadIdx;
  755. if (body_pairs_in_queue >= mStep->mMaxBodyPairsPerQueue)
  756. {
  757. // Buffer full, process the pair now
  758. mStep->mContext->mPhysicsSystem->ProcessBodyPair(mContactAllocator, inPair);
  759. }
  760. else
  761. {
  762. // Store the pair in our own queue
  763. mStep->mContext->mBodyPairs[mJobIndex * mStep->mMaxBodyPairsPerQueue + queue.mWriteIdx % mStep->mMaxBodyPairsPerQueue] = inPair;
  764. ++queue.mWriteIdx;
  765. }
  766. }
  767. private:
  768. PhysicsUpdateContext::Step * mStep;
  769. ContactAllocator & mContactAllocator;
  770. int mJobIndex;
  771. };
  772. MyBodyPairCallback add_pair(ioStep, contact_allocator, inJobIndex);
  773. // Copy active bodies to temporary array, broadphase will reorder them
  774. uint32 batch_size = active_bodies_read_idx_end - active_bodies_read_idx;
  775. memcpy(active_bodies, mBodyManager.GetActiveBodiesUnsafe(EBodyType::RigidBody) + active_bodies_read_idx, batch_size * sizeof(BodyID));
  776. // Find pairs in the broadphase
  777. mBroadPhase->FindCollidingPairs(active_bodies, batch_size, mPhysicsSettings.mSpeculativeContactDistance, *mObjectVsBroadPhaseLayerFilter, *mObjectLayerPairFilter, add_pair);
  778. // Check if we have enough pairs in the buffer to start a new job
  779. const PhysicsUpdateContext::BodyPairQueue &queue = ioStep->mBodyPairQueues[inJobIndex];
  780. uint32 body_pairs_in_queue = queue.mWriteIdx - queue.mReadIdx;
  781. if (body_pairs_in_queue >= cNarrowPhaseBatchSize)
  782. TrySpawnJobFindCollisions(ioStep);
  783. }
  784. }
  785. else
  786. {
  787. // Lockless loop to get the next body pair from the pairs buffer
  788. const PhysicsUpdateContext *context = ioStep->mContext;
  789. int first_read_queue_idx = read_queue_idx;
  790. for (;;)
  791. {
  792. PhysicsUpdateContext::BodyPairQueue &queue = ioStep->mBodyPairQueues[read_queue_idx];
  793. // Get the next pair to process
  794. uint32 pair_idx = queue.mReadIdx;
  795. // If the pair hasn't been written yet
  796. if (pair_idx >= queue.mWriteIdx)
  797. {
  798. // Go to the next queue
  799. read_queue_idx = (read_queue_idx + 1) % ioStep->mBodyPairQueues.size();
  800. // If we're back at the first queue, we've looked at all of them and found nothing
  801. if (read_queue_idx == first_read_queue_idx)
  802. {
  803. // Collect information from the contact allocator and accumulate it in the step.
  804. sFinalizeContactAllocator(*ioStep, contact_allocator);
  805. // Mark this job as inactive
  806. ioStep->mActiveFindCollisionJobs.fetch_and(~PhysicsUpdateContext::JobMask(1 << inJobIndex), memory_order_release);
  807. // Trigger the next jobs
  808. ioStep->mUpdateBroadphaseFinalize.RemoveDependency();
  809. ioStep->mFinalizeIslands.RemoveDependency();
  810. return;
  811. }
  812. // Try again reading from the next queue
  813. continue;
  814. }
  815. // Copy the body pair out of the buffer
  816. const BodyPair bp = context->mBodyPairs[read_queue_idx * ioStep->mMaxBodyPairsPerQueue + pair_idx % ioStep->mMaxBodyPairsPerQueue];
  817. // Mark this pair as taken
  818. if (queue.mReadIdx.compare_exchange_strong(pair_idx, pair_idx + 1))
  819. {
  820. // Process the actual body pair
  821. ProcessBodyPair(contact_allocator, bp);
  822. break;
  823. }
  824. }
  825. }
  826. }
  827. }
  828. void PhysicsSystem::sDefaultSimCollideBodyVsBody(const Body &inBody1, const Body &inBody2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, CollideShapeSettings &ioCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter)
  829. {
  830. SubShapeIDCreator part1, part2;
  831. if (inBody1.GetEnhancedInternalEdgeRemovalWithBody(inBody2))
  832. {
  833. // Collide with enhanced internal edge removal
  834. ioCollideShapeSettings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
  835. InternalEdgeRemovingCollector::sCollideShapeVsShape(inBody1.GetShape(), inBody2.GetShape(), Vec3::sOne(), Vec3::sOne(), inCenterOfMassTransform1, inCenterOfMassTransform2, part1, part2, ioCollideShapeSettings, ioCollector, inShapeFilter
  836. #ifdef JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
  837. , inBody1.GetCenterOfMassPosition() // Query is done relative to the position of body 1
  838. #endif // JPH_INTERNAL_EDGE_REMOVING_COLLECTOR_DEBUG
  839. );
  840. }
  841. else
  842. {
  843. // Regular collide
  844. CollisionDispatch::sCollideShapeVsShape(inBody1.GetShape(), inBody2.GetShape(), Vec3::sOne(), Vec3::sOne(), inCenterOfMassTransform1, inCenterOfMassTransform2, part1, part2, ioCollideShapeSettings, ioCollector, inShapeFilter);
  845. }
  846. }
  847. void PhysicsSystem::ProcessBodyPair(ContactAllocator &ioContactAllocator, const BodyPair &inBodyPair)
  848. {
  849. JPH_PROFILE_FUNCTION();
  850. // Fetch body pair
  851. Body *body1 = &mBodyManager.GetBody(inBodyPair.mBodyA);
  852. Body *body2 = &mBodyManager.GetBody(inBodyPair.mBodyB);
  853. JPH_ASSERT(body1->IsActive());
  854. JPH_DET_LOG("ProcessBodyPair: id1: " << inBodyPair.mBodyA << " id2: " << inBodyPair.mBodyB << " p1: " << body1->GetCenterOfMassPosition() << " p2: " << body2->GetCenterOfMassPosition() << " r1: " << body1->GetRotation() << " r2: " << body2->GetRotation());
  855. // Check for soft bodies
  856. if (body2->IsSoftBody())
  857. {
  858. // If the 2nd body is a soft body and not active, we activate it now
  859. if (!body2->IsActive())
  860. mBodyManager.ActivateBodies(&inBodyPair.mBodyB, 1);
  861. // Soft body processing is done later in the pipeline
  862. return;
  863. }
  864. // Ensure that body1 has the higher motion type (i.e. dynamic trumps kinematic), this ensures that we do the collision detection in the space of a moving body,
  865. // which avoids accuracy problems when testing a very large static object against a small dynamic object
  866. // Ensure that body1 id < body2 id when motion types are the same.
  867. if (body1->GetMotionType() < body2->GetMotionType()
  868. || (body1->GetMotionType() == body2->GetMotionType() && inBodyPair.mBodyB < inBodyPair.mBodyA))
  869. std::swap(body1, body2);
  870. // Check if the contact points from the previous frame are reusable and if so copy them
  871. bool pair_handled = false, constraint_created = false;
  872. if (mPhysicsSettings.mUseBodyPairContactCache && !(body1->IsCollisionCacheInvalid() || body2->IsCollisionCacheInvalid()))
  873. mContactManager.GetContactsFromCache(ioContactAllocator, *body1, *body2, pair_handled, constraint_created);
  874. // If the cache hasn't handled this body pair do actual collision detection
  875. if (!pair_handled)
  876. {
  877. #ifdef JPH_TRACK_SIMULATION_STATS
  878. uint64 start_ticks = GetProcessorTickCount();
  879. #endif
  880. // Create entry in the cache for this body pair
  881. // Needs to happen irrespective if we found a collision or not (we want to remember that no collision was found too)
  882. ContactConstraintManager::BodyPairHandle body_pair_handle = mContactManager.AddBodyPair(ioContactAllocator, *body1, *body2);
  883. if (body_pair_handle == nullptr)
  884. return; // Out of cache space
  885. // Create the query settings
  886. CollideShapeSettings settings;
  887. settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
  888. settings.mActiveEdgeMode = mPhysicsSettings.mCheckActiveEdges? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
  889. settings.mMaxSeparationDistance = body1->IsSensor() || body2->IsSensor()? 0.0f : mPhysicsSettings.mSpeculativeContactDistance;
  890. settings.mActiveEdgeMovementDirection = body1->GetLinearVelocity() - body2->GetLinearVelocity();
  891. // Create shape filter
  892. SimShapeFilterWrapper shape_filter(mSimShapeFilter, body1);
  893. shape_filter.SetBody2(body2);
  894. // Get transforms relative to body1
  895. RVec3 offset = body1->GetCenterOfMassPosition();
  896. Mat44 transform1 = Mat44::sRotation(body1->GetRotation());
  897. Mat44 transform2 = body2->GetCenterOfMassTransform().PostTranslated(-offset).ToMat44();
  898. if (mPhysicsSettings.mUseManifoldReduction // Check global flag
  899. && body1->GetUseManifoldReductionWithBody(*body2)) // Check body flag
  900. {
  901. // Version WITH contact manifold reduction
  902. class MyManifold : public ContactManifold
  903. {
  904. public:
  905. Vec3 mFirstWorldSpaceNormal;
  906. };
  907. // A temporary structure that allows us to keep track of the all manifolds between this body pair
  908. using Manifolds = StaticArray<MyManifold, 32>;
  909. // Create collector
  910. class ReductionCollideShapeCollector : public CollideShapeCollector
  911. {
  912. public:
  913. ReductionCollideShapeCollector(PhysicsSystem *inSystem, const Body *inBody1, const Body *inBody2) :
  914. mSystem(inSystem),
  915. mBody1(inBody1),
  916. mBody2(inBody2)
  917. {
  918. }
  919. virtual void AddHit(const CollideShapeResult &inResult) override
  920. {
  921. // The first body should be the one with the highest motion type
  922. JPH_ASSERT(mBody1->GetMotionType() >= mBody2->GetMotionType());
  923. JPH_ASSERT(!ShouldEarlyOut());
  924. // Test if we want to accept this hit
  925. if (mValidateBodyPair)
  926. {
  927. switch (mSystem->mContactManager.ValidateContactPoint(*mBody1, *mBody2, mBody1->GetCenterOfMassPosition(), inResult))
  928. {
  929. case ValidateResult::AcceptContact:
  930. // We're just accepting this one, nothing to do
  931. break;
  932. case ValidateResult::AcceptAllContactsForThisBodyPair:
  933. // Accept and stop calling the validate callback
  934. mValidateBodyPair = false;
  935. break;
  936. case ValidateResult::RejectContact:
  937. // Skip this contact
  938. return;
  939. case ValidateResult::RejectAllContactsForThisBodyPair:
  940. // Skip this and early out
  941. ForceEarlyOut();
  942. return;
  943. }
  944. }
  945. // Calculate normal
  946. Vec3 world_space_normal = inResult.mPenetrationAxis.Normalized();
  947. // Check if we can add it to an existing manifold
  948. Manifolds::iterator manifold;
  949. float contact_normal_cos_max_delta_rot = mSystem->mPhysicsSettings.mContactNormalCosMaxDeltaRotation;
  950. for (manifold = mManifolds.begin(); manifold != mManifolds.end(); ++manifold)
  951. if (world_space_normal.Dot(manifold->mFirstWorldSpaceNormal) >= contact_normal_cos_max_delta_rot)
  952. {
  953. // Update average normal
  954. manifold->mWorldSpaceNormal += world_space_normal;
  955. manifold->mPenetrationDepth = max(manifold->mPenetrationDepth, inResult.mPenetrationDepth);
  956. break;
  957. }
  958. if (manifold == mManifolds.end())
  959. {
  960. // Check if array is full
  961. if (mManifolds.size() == mManifolds.capacity())
  962. {
  963. // Full, find manifold with least amount of penetration
  964. manifold = mManifolds.begin();
  965. for (Manifolds::iterator m = mManifolds.begin() + 1; m < mManifolds.end(); ++m)
  966. if (m->mPenetrationDepth < manifold->mPenetrationDepth)
  967. manifold = m;
  968. // If this contacts penetration is smaller than the smallest manifold, we skip this contact
  969. if (inResult.mPenetrationDepth < manifold->mPenetrationDepth)
  970. return;
  971. // Replace the manifold
  972. *manifold = { { mBody1->GetCenterOfMassPosition(), world_space_normal, inResult.mPenetrationDepth, inResult.mSubShapeID1, inResult.mSubShapeID2, { }, { } }, world_space_normal };
  973. }
  974. else
  975. {
  976. // Not full, create new manifold
  977. mManifolds.push_back({ { mBody1->GetCenterOfMassPosition(), world_space_normal, inResult.mPenetrationDepth, inResult.mSubShapeID1, inResult.mSubShapeID2, { }, { } }, world_space_normal });
  978. manifold = mManifolds.end() - 1;
  979. }
  980. }
  981. // Determine contact points
  982. const PhysicsSettings &settings = mSystem->mPhysicsSettings;
  983. ManifoldBetweenTwoFaces(inResult.mContactPointOn1, inResult.mContactPointOn2, inResult.mPenetrationAxis, settings.mSpeculativeContactDistance + settings.mManifoldTolerance, inResult.mShape1Face, inResult.mShape2Face, manifold->mRelativeContactPointsOn1, manifold->mRelativeContactPointsOn2 JPH_IF_DEBUG_RENDERER(, mBody1->GetCenterOfMassPosition()));
  984. // Prune if we have more than 32 points (this means we could run out of space in the next iteration)
  985. if (manifold->mRelativeContactPointsOn1.size() > 32)
  986. PruneContactPoints(manifold->mFirstWorldSpaceNormal, manifold->mRelativeContactPointsOn1, manifold->mRelativeContactPointsOn2 JPH_IF_DEBUG_RENDERER(, manifold->mBaseOffset));
  987. }
  988. PhysicsSystem * mSystem;
  989. const Body * mBody1;
  990. const Body * mBody2;
  991. bool mValidateBodyPair = true;
  992. Manifolds mManifolds;
  993. };
  994. ReductionCollideShapeCollector collector(this, body1, body2);
  995. // Perform collision detection between the two shapes
  996. mSimCollideBodyVsBody(*body1, *body2, transform1, transform2, settings, collector, shape_filter.GetFilter());
  997. // Add the contacts
  998. for (ContactManifold &manifold : collector.mManifolds)
  999. {
  1000. // Normalize the normal (is a sum of all normals from merged manifolds)
  1001. manifold.mWorldSpaceNormal = manifold.mWorldSpaceNormal.Normalized();
  1002. // If we still have too many points, prune them now
  1003. if (manifold.mRelativeContactPointsOn1.size() > 4)
  1004. PruneContactPoints(manifold.mWorldSpaceNormal, manifold.mRelativeContactPointsOn1, manifold.mRelativeContactPointsOn2 JPH_IF_DEBUG_RENDERER(, manifold.mBaseOffset));
  1005. // Actually add the contact points to the manager
  1006. constraint_created |= mContactManager.AddContactConstraint(ioContactAllocator, body_pair_handle, *body1, *body2, manifold);
  1007. }
  1008. }
  1009. else
  1010. {
  1011. // Version WITHOUT contact manifold reduction
  1012. // Create collector
  1013. class NonReductionCollideShapeCollector : public CollideShapeCollector
  1014. {
  1015. public:
  1016. NonReductionCollideShapeCollector(PhysicsSystem *inSystem, ContactAllocator &ioContactAllocator, Body *inBody1, Body *inBody2, const ContactConstraintManager::BodyPairHandle &inPairHandle) :
  1017. mSystem(inSystem),
  1018. mContactAllocator(ioContactAllocator),
  1019. mBody1(inBody1),
  1020. mBody2(inBody2),
  1021. mBodyPairHandle(inPairHandle)
  1022. {
  1023. }
  1024. virtual void AddHit(const CollideShapeResult &inResult) override
  1025. {
  1026. // The first body should be the one with the highest motion type
  1027. JPH_ASSERT(mBody1->GetMotionType() >= mBody2->GetMotionType());
  1028. JPH_ASSERT(!ShouldEarlyOut());
  1029. // Test if we want to accept this hit
  1030. if (mValidateBodyPair)
  1031. {
  1032. switch (mSystem->mContactManager.ValidateContactPoint(*mBody1, *mBody2, mBody1->GetCenterOfMassPosition(), inResult))
  1033. {
  1034. case ValidateResult::AcceptContact:
  1035. // We're just accepting this one, nothing to do
  1036. break;
  1037. case ValidateResult::AcceptAllContactsForThisBodyPair:
  1038. // Accept and stop calling the validate callback
  1039. mValidateBodyPair = false;
  1040. break;
  1041. case ValidateResult::RejectContact:
  1042. // Skip this contact
  1043. return;
  1044. case ValidateResult::RejectAllContactsForThisBodyPair:
  1045. // Skip this and early out
  1046. ForceEarlyOut();
  1047. return;
  1048. }
  1049. }
  1050. // Determine contact points
  1051. ContactManifold manifold;
  1052. manifold.mBaseOffset = mBody1->GetCenterOfMassPosition();
  1053. const PhysicsSettings &settings = mSystem->mPhysicsSettings;
  1054. ManifoldBetweenTwoFaces(inResult.mContactPointOn1, inResult.mContactPointOn2, inResult.mPenetrationAxis, settings.mSpeculativeContactDistance + settings.mManifoldTolerance, inResult.mShape1Face, inResult.mShape2Face, manifold.mRelativeContactPointsOn1, manifold.mRelativeContactPointsOn2 JPH_IF_DEBUG_RENDERER(, manifold.mBaseOffset));
  1055. // Calculate normal
  1056. manifold.mWorldSpaceNormal = inResult.mPenetrationAxis.Normalized();
  1057. // Store penetration depth
  1058. manifold.mPenetrationDepth = inResult.mPenetrationDepth;
  1059. // Prune if we have more than 4 points
  1060. if (manifold.mRelativeContactPointsOn1.size() > 4)
  1061. PruneContactPoints(manifold.mWorldSpaceNormal, manifold.mRelativeContactPointsOn1, manifold.mRelativeContactPointsOn2 JPH_IF_DEBUG_RENDERER(, manifold.mBaseOffset));
  1062. // Set other properties
  1063. manifold.mSubShapeID1 = inResult.mSubShapeID1;
  1064. manifold.mSubShapeID2 = inResult.mSubShapeID2;
  1065. // Actually add the contact points to the manager
  1066. mConstraintCreated |= mSystem->mContactManager.AddContactConstraint(mContactAllocator, mBodyPairHandle, *mBody1, *mBody2, manifold);
  1067. }
  1068. PhysicsSystem * mSystem;
  1069. ContactAllocator & mContactAllocator;
  1070. Body * mBody1;
  1071. Body * mBody2;
  1072. ContactConstraintManager::BodyPairHandle mBodyPairHandle;
  1073. bool mValidateBodyPair = true;
  1074. bool mConstraintCreated = false;
  1075. };
  1076. NonReductionCollideShapeCollector collector(this, ioContactAllocator, body1, body2, body_pair_handle);
  1077. // Perform collision detection between the two shapes
  1078. mSimCollideBodyVsBody(*body1, *body2, transform1, transform2, settings, collector, shape_filter.GetFilter());
  1079. constraint_created = collector.mConstraintCreated;
  1080. }
  1081. #ifdef JPH_TRACK_SIMULATION_STATS
  1082. // Track time spent processing collision for this body pair
  1083. uint64 num_ticks = GetProcessorTickCount() - start_ticks;
  1084. if (body1->GetMotionType() > body2->GetMotionType())
  1085. {
  1086. // Assign all ticks to the body with the higher motion type
  1087. body1->GetMotionProperties()->GetSimulationStats().mNarrowPhaseTicks.fetch_add(num_ticks, memory_order_relaxed);
  1088. }
  1089. else
  1090. {
  1091. // When two bodies with the same motion type are involved, we give both bodies half the ticks
  1092. JPH_ASSERT(body1->GetMotionType() == body2->GetMotionType());
  1093. num_ticks /= 2;
  1094. body1->GetMotionProperties()->GetSimulationStats().mNarrowPhaseTicks.fetch_add(num_ticks, memory_order_relaxed);
  1095. body1->GetMotionProperties()->GetSimulationStats().mNarrowPhaseTicks.fetch_add(num_ticks, memory_order_relaxed);
  1096. }
  1097. #endif
  1098. }
  1099. // If a contact constraint was created, we need to do some extra work
  1100. if (constraint_created)
  1101. {
  1102. // Wake up sleeping bodies
  1103. BodyID body_ids[2];
  1104. int num_bodies = 0;
  1105. if (body1->IsDynamic() && !body1->IsActive())
  1106. body_ids[num_bodies++] = body1->GetID();
  1107. if (body2->IsDynamic() && !body2->IsActive())
  1108. body_ids[num_bodies++] = body2->GetID();
  1109. if (num_bodies > 0)
  1110. mBodyManager.ActivateBodies(body_ids, num_bodies);
  1111. // Link the two bodies
  1112. mIslandBuilder.LinkBodies(body1->GetIndexInActiveBodiesInternal(), body2->GetIndexInActiveBodiesInternal());
  1113. }
  1114. }
  1115. void PhysicsSystem::JobFinalizeIslands(PhysicsUpdateContext *ioContext)
  1116. {
  1117. #ifdef JPH_ENABLE_ASSERTS
  1118. // We only touch island data
  1119. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  1120. #endif
  1121. // Finish collecting the islands, at this point the active body list doesn't change so it's safe to access
  1122. mIslandBuilder.Finalize(mBodyManager.GetActiveBodiesUnsafe(EBodyType::RigidBody), mBodyManager.GetNumActiveBodies(EBodyType::RigidBody), mContactManager.GetNumConstraints(), ioContext->mTempAllocator);
  1123. // Prepare the large island splitter
  1124. if (mPhysicsSettings.mUseLargeIslandSplitter)
  1125. mLargeIslandSplitter.Prepare(mIslandBuilder, mBodyManager.GetNumActiveBodies(EBodyType::RigidBody), ioContext->mTempAllocator);
  1126. }
  1127. void PhysicsSystem::JobBodySetIslandIndex()
  1128. {
  1129. #ifdef JPH_ENABLE_ASSERTS
  1130. // We only touch island data
  1131. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  1132. #endif
  1133. // Loop through the result and tag all bodies with an island index
  1134. for (uint32 island_idx = 0, n = mIslandBuilder.GetNumIslands(); island_idx < n; ++island_idx)
  1135. {
  1136. BodyID *body_start, *body_end;
  1137. mIslandBuilder.GetBodiesInIsland(island_idx, body_start, body_end);
  1138. for (const BodyID *body = body_start; body < body_end; ++body)
  1139. mBodyManager.GetBody(*body).GetMotionProperties()->SetIslandIndexInternal(island_idx);
  1140. }
  1141. }
  1142. JPH_SUPPRESS_WARNING_PUSH
  1143. JPH_CLANG_SUPPRESS_WARNING("-Wundefined-func-template") // ConstraintManager::sWarmStartVelocityConstraints / ContactConstraintManager::WarmStartVelocityConstraints is instantiated in the cpp file
  1144. void PhysicsSystem::JobSolveVelocityConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  1145. {
  1146. #ifdef JPH_ENABLE_ASSERTS
  1147. // We update velocities and need to read positions to do so
  1148. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  1149. #endif
  1150. float delta_time = ioContext->mStepDeltaTime;
  1151. Constraint **active_constraints = ioContext->mActiveConstraints;
  1152. // Only the first step to correct for the delta time difference in the previous update
  1153. float warm_start_impulse_ratio = ioStep->mIsFirst? ioContext->mWarmStartImpulseRatio : 1.0f;
  1154. bool check_islands = true, check_split_islands = mPhysicsSettings.mUseLargeIslandSplitter;
  1155. for (;;)
  1156. {
  1157. // First try to get work from large islands
  1158. if (check_split_islands)
  1159. {
  1160. bool first_iteration;
  1161. uint split_island_index;
  1162. uint32 *constraints_begin, *constraints_end, *contacts_begin, *contacts_end;
  1163. switch (mLargeIslandSplitter.FetchNextBatch(split_island_index, constraints_begin, constraints_end, contacts_begin, contacts_end, first_iteration))
  1164. {
  1165. case LargeIslandSplitter::EStatus::BatchRetrieved:
  1166. {
  1167. #ifdef JPH_TRACK_SIMULATION_STATS
  1168. uint64 start_tick = GetProcessorTickCount();
  1169. #endif
  1170. if (first_iteration)
  1171. {
  1172. // Iteration 0 is used to warm start the batch (we added 1 to the number of iterations in LargeIslandSplitter::SplitIsland)
  1173. DummyCalculateSolverSteps dummy;
  1174. ConstraintManager::sWarmStartVelocityConstraints(active_constraints, constraints_begin, constraints_end, warm_start_impulse_ratio, dummy);
  1175. mContactManager.WarmStartVelocityConstraints(contacts_begin, contacts_end, warm_start_impulse_ratio, dummy);
  1176. }
  1177. else
  1178. {
  1179. // Solve velocity constraints
  1180. ConstraintManager::sSolveVelocityConstraints(active_constraints, constraints_begin, constraints_end, delta_time);
  1181. mContactManager.SolveVelocityConstraints(contacts_begin, contacts_end);
  1182. }
  1183. // Mark the batch as processed
  1184. bool last_iteration, final_batch;
  1185. mLargeIslandSplitter.MarkBatchProcessed(split_island_index, constraints_begin, constraints_end, contacts_begin, contacts_end, last_iteration, final_batch);
  1186. // Save back the lambdas in the contact cache for the warm start of the next physics update
  1187. if (last_iteration)
  1188. mContactManager.StoreAppliedImpulses(contacts_begin, contacts_end);
  1189. #ifdef JPH_TRACK_SIMULATION_STATS
  1190. uint64 num_ticks = GetProcessorTickCount() - start_tick;
  1191. mIslandBuilder.GetIslandStats(mLargeIslandSplitter.GetIslandIndex(split_island_index)).mVelocityConstraintTicks.fetch_add(num_ticks, memory_order_relaxed);
  1192. #endif
  1193. // We processed work, loop again
  1194. continue;
  1195. }
  1196. case LargeIslandSplitter::EStatus::WaitingForBatch:
  1197. break;
  1198. case LargeIslandSplitter::EStatus::AllBatchesDone:
  1199. check_split_islands = false;
  1200. break;
  1201. }
  1202. }
  1203. // If that didn't succeed try to process an island
  1204. if (check_islands)
  1205. {
  1206. // Next island
  1207. uint32 island_idx = ioStep->mSolveVelocityConstraintsNextIsland++;
  1208. if (island_idx >= mIslandBuilder.GetNumIslands())
  1209. {
  1210. // We processed all islands, stop checking islands
  1211. check_islands = false;
  1212. continue;
  1213. }
  1214. JPH_PROFILE("Island");
  1215. // Get iterators for this island
  1216. uint32 *constraints_begin, *constraints_end, *contacts_begin, *contacts_end;
  1217. bool has_constraints = mIslandBuilder.GetConstraintsInIsland(island_idx, constraints_begin, constraints_end);
  1218. bool has_contacts = mIslandBuilder.GetContactsInIsland(island_idx, contacts_begin, contacts_end);
  1219. // If we don't have any contacts or constraints, we know that none of the following islands have any contacts or constraints
  1220. // (because they're sorted by most constraints first). This means we're done.
  1221. if (!has_contacts && !has_constraints)
  1222. {
  1223. #ifdef JPH_ENABLE_ASSERTS
  1224. // Validate our assumption that the next islands don't have any constraints or contacts
  1225. for (; island_idx < mIslandBuilder.GetNumIslands(); ++island_idx)
  1226. {
  1227. JPH_ASSERT(!mIslandBuilder.GetConstraintsInIsland(island_idx, constraints_begin, constraints_end));
  1228. JPH_ASSERT(!mIslandBuilder.GetContactsInIsland(island_idx, contacts_begin, contacts_end));
  1229. }
  1230. #endif // JPH_ENABLE_ASSERTS
  1231. check_islands = false;
  1232. continue;
  1233. }
  1234. #ifdef JPH_TRACK_SIMULATION_STATS
  1235. uint64 start_tick = GetProcessorTickCount();
  1236. #endif
  1237. // Sorting is costly but needed for a deterministic simulation, allow the user to turn this off
  1238. if (mPhysicsSettings.mDeterministicSimulation)
  1239. {
  1240. // Sort constraints to give a deterministic simulation
  1241. ConstraintManager::sSortConstraints(active_constraints, constraints_begin, constraints_end);
  1242. // Sort contacts to give a deterministic simulation
  1243. mContactManager.SortContacts(contacts_begin, contacts_end);
  1244. }
  1245. // Split up large islands
  1246. CalculateSolverSteps steps_calculator(mPhysicsSettings);
  1247. if (!mPhysicsSettings.mUseLargeIslandSplitter
  1248. || !mLargeIslandSplitter.SplitIsland(island_idx, mIslandBuilder, mBodyManager, mContactManager, active_constraints, steps_calculator))
  1249. {
  1250. // We didn't create a split, just run the solver now for this entire island. Begin by warm starting.
  1251. ConstraintManager::sWarmStartVelocityConstraints(active_constraints, constraints_begin, constraints_end, warm_start_impulse_ratio, steps_calculator);
  1252. mContactManager.WarmStartVelocityConstraints(contacts_begin, contacts_end, warm_start_impulse_ratio, steps_calculator);
  1253. steps_calculator.Finalize();
  1254. // Solve velocity constraints
  1255. for (uint velocity_step = 0; velocity_step < steps_calculator.GetNumVelocitySteps(); ++velocity_step)
  1256. {
  1257. bool applied_impulse = ConstraintManager::sSolveVelocityConstraints(active_constraints, constraints_begin, constraints_end, delta_time);
  1258. applied_impulse |= mContactManager.SolveVelocityConstraints(contacts_begin, contacts_end);
  1259. if (!applied_impulse)
  1260. break;
  1261. }
  1262. // Save back the lambdas in the contact cache for the warm start of the next physics update
  1263. mContactManager.StoreAppliedImpulses(contacts_begin, contacts_end);
  1264. }
  1265. // Store the number of position steps for later
  1266. mIslandBuilder.SetNumPositionSteps(island_idx, steps_calculator.GetNumPositionSteps());
  1267. #ifdef JPH_TRACK_SIMULATION_STATS
  1268. uint64 num_ticks = GetProcessorTickCount() - start_tick;
  1269. IslandBuilder::IslandStats &stats = mIslandBuilder.GetIslandStats(island_idx);
  1270. stats.mNumVelocitySteps = (uint8)steps_calculator.GetNumVelocitySteps();
  1271. stats.mVelocityConstraintTicks.fetch_add(num_ticks, memory_order_relaxed);
  1272. #endif
  1273. // We processed work, loop again
  1274. continue;
  1275. }
  1276. if (check_islands)
  1277. {
  1278. // If there are islands, we don't need to wait and can pick up new work
  1279. continue;
  1280. }
  1281. else if (check_split_islands)
  1282. {
  1283. // If there are split islands, but we didn't do any work, give up a time slice
  1284. std::this_thread::yield();
  1285. }
  1286. else
  1287. {
  1288. // No more work
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. JPH_SUPPRESS_WARNING_POP
  1294. void PhysicsSystem::JobPreIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  1295. {
  1296. // Reserve enough space for all bodies that may need a cast
  1297. TempAllocator *temp_allocator = ioContext->mTempAllocator;
  1298. JPH_ASSERT(ioStep->mCCDBodies == nullptr);
  1299. ioStep->mCCDBodiesCapacity = mBodyManager.GetNumActiveCCDBodies();
  1300. ioStep->mCCDBodies = (CCDBody *)temp_allocator->Allocate(ioStep->mCCDBodiesCapacity * sizeof(CCDBody));
  1301. // Initialize the mapping table between active body and CCD body
  1302. JPH_ASSERT(ioStep->mActiveBodyToCCDBody == nullptr);
  1303. ioStep->mNumActiveBodyToCCDBody = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody);
  1304. ioStep->mActiveBodyToCCDBody = (int *)temp_allocator->Allocate(ioStep->mNumActiveBodyToCCDBody * sizeof(int));
  1305. // Prepare the split island builder for solving the position constraints
  1306. mLargeIslandSplitter.PrepareForSolvePositions();
  1307. }
  1308. void PhysicsSystem::JobIntegrateVelocity(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  1309. {
  1310. #ifdef JPH_ENABLE_ASSERTS
  1311. // We update positions and need velocity to do so, we also clamp velocities so need to write to them
  1312. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::ReadWrite);
  1313. #endif
  1314. float delta_time = ioContext->mStepDeltaTime;
  1315. const BodyID *active_bodies = mBodyManager.GetActiveBodiesUnsafe(EBodyType::RigidBody);
  1316. uint32 num_active_bodies = mBodyManager.GetNumActiveBodies(EBodyType::RigidBody);
  1317. uint32 num_active_bodies_after_find_collisions = ioStep->mActiveBodyReadIdx;
  1318. // We can move bodies that are not part of an island. In this case we need to notify the broadphase of the movement.
  1319. static constexpr int cBodiesBatch = 64;
  1320. BodyID *bodies_to_update_bounds = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  1321. int num_bodies_to_update_bounds = 0;
  1322. for (;;)
  1323. {
  1324. // Atomically fetch a batch of bodies
  1325. uint32 active_body_idx = ioStep->mIntegrateVelocityReadIdx.fetch_add(cIntegrateVelocityBatchSize);
  1326. if (active_body_idx >= num_active_bodies)
  1327. break;
  1328. // Calculate the end of the batch
  1329. uint32 active_body_idx_end = min(num_active_bodies, active_body_idx + cIntegrateVelocityBatchSize);
  1330. // Process the batch
  1331. while (active_body_idx < active_body_idx_end)
  1332. {
  1333. // Update the positions using an Symplectic Euler step (which integrates using the updated velocity v1' rather
  1334. // than the original velocity v1):
  1335. // x1' = x1 + h * v1'
  1336. // At this point the active bodies list does not change, so it is safe to access the array.
  1337. BodyID body_id = active_bodies[active_body_idx];
  1338. Body &body = mBodyManager.GetBody(body_id);
  1339. MotionProperties *mp = body.GetMotionProperties();
  1340. JPH_DET_LOG("JobIntegrateVelocity: id: " << body_id << " v: " << body.GetLinearVelocity() << " w: " << body.GetAngularVelocity());
  1341. // Clamp velocities (not for kinematic bodies)
  1342. if (body.IsDynamic())
  1343. {
  1344. mp->ClampLinearVelocity();
  1345. mp->ClampAngularVelocity();
  1346. }
  1347. // Update the rotation of the body according to the angular velocity
  1348. // For motion type discrete we need to do this anyway, for motion type linear cast we have multiple choices
  1349. // 1. Rotate the body first and then sweep
  1350. // 2. First sweep and then rotate the body at the end
  1351. // 3. Pick some in between rotation (e.g. half way), then sweep and finally rotate the remainder
  1352. // (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.
  1353. // 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
  1354. // 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
  1355. // 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
  1356. // too as simulating the rotation first may cause it to tunnel through a small object that the linear cast might have otherwise detected. In any case a linear cast is not good for detecting
  1357. // 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).
  1358. body.AddRotationStep(body.GetAngularVelocity() * delta_time);
  1359. // Get delta position
  1360. Vec3 delta_pos = body.GetLinearVelocity() * delta_time;
  1361. // If the position should be updated (or if it is delayed because of CCD)
  1362. bool update_position = true;
  1363. switch (mp->GetMotionQuality())
  1364. {
  1365. case EMotionQuality::Discrete:
  1366. // No additional collision checking to be done
  1367. break;
  1368. case EMotionQuality::LinearCast:
  1369. if (body.IsDynamic() // Kinematic bodies cannot be stopped
  1370. && !body.IsSensor()) // We don't support CCD sensors
  1371. {
  1372. // Determine inner radius (the smallest sphere that fits into the shape)
  1373. float inner_radius = body.GetShape()->GetInnerRadius();
  1374. 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.");
  1375. // Measure translation in this step and check if it above the threshold to perform a linear cast
  1376. float linear_cast_threshold_sq = Square(mPhysicsSettings.mLinearCastThreshold * inner_radius);
  1377. if (delta_pos.LengthSq() > linear_cast_threshold_sq)
  1378. {
  1379. // This body needs a cast
  1380. uint32 ccd_body_idx = ioStep->mNumCCDBodies++;
  1381. JPH_ASSERT(active_body_idx < ioStep->mNumActiveBodyToCCDBody);
  1382. ioStep->mActiveBodyToCCDBody[active_body_idx] = ccd_body_idx;
  1383. new (&ioStep->mCCDBodies[ccd_body_idx]) CCDBody(body_id, delta_pos, linear_cast_threshold_sq, min(mPhysicsSettings.mPenetrationSlop, mPhysicsSettings.mLinearCastMaxPenetration * inner_radius));
  1384. update_position = false;
  1385. }
  1386. }
  1387. break;
  1388. }
  1389. if (update_position)
  1390. {
  1391. // Move the body now
  1392. body.AddPositionStep(delta_pos);
  1393. // If the body was activated due to an earlier CCD step it will have an index in the active
  1394. // body list that it higher than the highest one we processed during FindCollisions
  1395. // which means it hasn't been assigned an island and will not be updated by an island
  1396. // this means that we need to update its bounds manually
  1397. if (mp->GetIndexInActiveBodiesInternal() >= num_active_bodies_after_find_collisions)
  1398. {
  1399. body.CalculateWorldSpaceBoundsInternal();
  1400. bodies_to_update_bounds[num_bodies_to_update_bounds++] = body.GetID();
  1401. if (num_bodies_to_update_bounds == cBodiesBatch)
  1402. {
  1403. // Buffer full, flush now
  1404. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  1405. num_bodies_to_update_bounds = 0;
  1406. }
  1407. }
  1408. // We did not create a CCD body
  1409. ioStep->mActiveBodyToCCDBody[active_body_idx] = -1;
  1410. }
  1411. active_body_idx++;
  1412. }
  1413. }
  1414. // Notify change bounds on requested bodies
  1415. if (num_bodies_to_update_bounds > 0)
  1416. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  1417. }
  1418. void PhysicsSystem::JobPostIntegrateVelocity(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep) const
  1419. {
  1420. // Validate that our reservations were correct
  1421. JPH_ASSERT(ioStep->mNumCCDBodies <= mBodyManager.GetNumActiveCCDBodies());
  1422. if (ioStep->mNumCCDBodies == 0)
  1423. {
  1424. // No continuous collision detection jobs -> kick the next job ourselves
  1425. ioStep->mContactRemovedCallbacks.RemoveDependency();
  1426. }
  1427. else
  1428. {
  1429. // Run the continuous collision detection jobs
  1430. int num_continuous_collision_jobs = min(int(ioStep->mNumCCDBodies + cNumCCDBodiesPerJob - 1) / cNumCCDBodiesPerJob, ioContext->GetMaxConcurrency());
  1431. ioStep->mResolveCCDContacts.AddDependency(num_continuous_collision_jobs);
  1432. ioStep->mContactRemovedCallbacks.AddDependency(num_continuous_collision_jobs - 1); // Already had 1 dependency
  1433. for (int i = 0; i < num_continuous_collision_jobs; ++i)
  1434. {
  1435. JobHandle job = ioContext->mJobSystem->CreateJob("FindCCDContacts", cColorFindCCDContacts, [ioContext, ioStep]()
  1436. {
  1437. ioContext->mPhysicsSystem->JobFindCCDContacts(ioContext, ioStep);
  1438. ioStep->mResolveCCDContacts.RemoveDependency();
  1439. ioStep->mContactRemovedCallbacks.RemoveDependency();
  1440. });
  1441. ioContext->mBarrier->AddJob(job);
  1442. }
  1443. }
  1444. }
  1445. // Helper function to calculate the motion of a body during this CCD step
  1446. inline static Vec3 sCalculateBodyMotion(const Body &inBody, float inDeltaTime)
  1447. {
  1448. // If the body is linear casting, the body has not yet moved so we need to calculate its motion
  1449. if (inBody.IsDynamic() && inBody.GetMotionProperties()->GetMotionQuality() == EMotionQuality::LinearCast)
  1450. return inDeltaTime * inBody.GetLinearVelocity();
  1451. // Body has already moved, so we don't need to correct for anything
  1452. return Vec3::sZero();
  1453. }
  1454. // Helper function that finds the CCD body corresponding to a body (if it exists)
  1455. inline static PhysicsUpdateContext::Step::CCDBody *sGetCCDBody(const Body &inBody, PhysicsUpdateContext::Step *inStep)
  1456. {
  1457. // Only rigid bodies can have a CCD body
  1458. if (!inBody.IsRigidBody())
  1459. return nullptr;
  1460. // If the body has no motion properties it cannot have a CCD body
  1461. const MotionProperties *motion_properties = inBody.GetMotionPropertiesUnchecked();
  1462. if (motion_properties == nullptr)
  1463. return nullptr;
  1464. // If it is not active it cannot have a CCD body
  1465. uint32 active_index = motion_properties->GetIndexInActiveBodiesInternal();
  1466. if (active_index == Body::cInactiveIndex)
  1467. return nullptr;
  1468. // Check if the active body has a corresponding CCD body
  1469. JPH_ASSERT(active_index < inStep->mNumActiveBodyToCCDBody); // Ensure that the body has a mapping to CCD body
  1470. int ccd_index = inStep->mActiveBodyToCCDBody[active_index];
  1471. if (ccd_index < 0)
  1472. return nullptr;
  1473. PhysicsUpdateContext::Step::CCDBody *ccd_body = &inStep->mCCDBodies[ccd_index];
  1474. JPH_ASSERT(ccd_body->mBodyID1 == inBody.GetID(), "We found the wrong CCD body!");
  1475. return ccd_body;
  1476. }
  1477. void PhysicsSystem::JobFindCCDContacts(const PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  1478. {
  1479. #ifdef JPH_ENABLE_ASSERTS
  1480. // We only read positions, but the validate callback may read body positions and velocities
  1481. BodyAccess::Grant grant(BodyAccess::EAccess::Read, BodyAccess::EAccess::Read);
  1482. #endif
  1483. // Allocation context for allocating new contact points
  1484. ContactAllocator contact_allocator(mContactManager.GetContactAllocator());
  1485. // Settings
  1486. ShapeCastSettings settings;
  1487. settings.mUseShrunkenShapeAndConvexRadius = true;
  1488. settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  1489. settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  1490. settings.mReturnDeepestPoint = true;
  1491. settings.mCollectFacesMode = ECollectFacesMode::CollectFaces;
  1492. settings.mActiveEdgeMode = mPhysicsSettings.mCheckActiveEdges? EActiveEdgeMode::CollideOnlyWithActive : EActiveEdgeMode::CollideWithAll;
  1493. for (;;)
  1494. {
  1495. // Fetch the next body to cast
  1496. uint32 idx = ioStep->mNextCCDBody++;
  1497. if (idx >= ioStep->mNumCCDBodies)
  1498. break;
  1499. CCDBody &ccd_body = ioStep->mCCDBodies[idx];
  1500. Body &body = mBodyManager.GetBody(ccd_body.mBodyID1);
  1501. // Filter out layers
  1502. DefaultBroadPhaseLayerFilter broadphase_layer_filter = GetDefaultBroadPhaseLayerFilter(body.GetObjectLayer());
  1503. DefaultObjectLayerFilter object_layer_filter = GetDefaultLayerFilter(body.GetObjectLayer());
  1504. #ifdef JPH_DEBUG_RENDERER
  1505. // Draw start and end shape of cast
  1506. if (sDrawMotionQualityLinearCast)
  1507. {
  1508. RMat44 com = body.GetCenterOfMassTransform();
  1509. body.GetShape()->Draw(DebugRenderer::sInstance, com, Vec3::sOne(), Color::sGreen, false, true);
  1510. DebugRenderer::sInstance->DrawArrow(com.GetTranslation(), com.GetTranslation() + ccd_body.mDeltaPosition, Color::sGreen, 0.1f);
  1511. body.GetShape()->Draw(DebugRenderer::sInstance, com.PostTranslated(ccd_body.mDeltaPosition), Vec3::sOne(), Color::sRed, false, true);
  1512. }
  1513. #endif // JPH_DEBUG_RENDERER
  1514. // Create a collector that will find the maximum distance allowed to travel while not penetrating more than 'max penetration'
  1515. class CCDNarrowPhaseCollector : public CastShapeCollector
  1516. {
  1517. public:
  1518. CCDNarrowPhaseCollector(const BodyManager &inBodyManager, ContactConstraintManager &inContactConstraintManager, CCDBody &inCCDBody, ShapeCastResult &inResult, float inDeltaTime) :
  1519. mBodyManager(inBodyManager),
  1520. mContactConstraintManager(inContactConstraintManager),
  1521. mCCDBody(inCCDBody),
  1522. mResult(inResult),
  1523. mDeltaTime(inDeltaTime)
  1524. {
  1525. }
  1526. virtual void AddHit(const ShapeCastResult &inResult) override
  1527. {
  1528. JPH_PROFILE_FUNCTION();
  1529. // Check if this is a possible earlier hit than the one before
  1530. float fraction = inResult.mFraction;
  1531. if (fraction < mCCDBody.mFractionPlusSlop)
  1532. {
  1533. // Normalize normal
  1534. Vec3 normal = inResult.mPenetrationAxis.Normalized();
  1535. // Calculate how much we can add to the fraction to penetrate the collision point by mMaxPenetration.
  1536. // Note that the normal is pointing towards body 2!
  1537. // 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|
  1538. // <=> dist = mMaxPenetration * |delta_pos| / normal . delta_pos
  1539. // Converting to a faction: delta_fraction = dist / |delta_pos| = mLinearCastTreshold / normal . delta_pos
  1540. float denominator = normal.Dot(mCCDBody.mDeltaPosition);
  1541. if (denominator > mCCDBody.mMaxPenetration) // Avoid dividing by zero, if extra hit fraction > 1 there's also no point in continuing
  1542. {
  1543. float fraction_plus_slop = fraction + mCCDBody.mMaxPenetration / denominator;
  1544. if (fraction_plus_slop < mCCDBody.mFractionPlusSlop)
  1545. {
  1546. const Body &body2 = mBodyManager.GetBody(inResult.mBodyID2);
  1547. // Check if we've already accepted all hits from this body
  1548. if (mValidateBodyPair)
  1549. {
  1550. // Validate the contact result
  1551. const Body &body1 = mBodyManager.GetBody(mCCDBody.mBodyID1);
  1552. ValidateResult validate_result = mContactConstraintManager.ValidateContactPoint(body1, body2, body1.GetCenterOfMassPosition(), inResult); // Note that the center of mass of body 1 is the start of the sweep and is used as base offset below
  1553. switch (validate_result)
  1554. {
  1555. case ValidateResult::AcceptContact:
  1556. // Just continue
  1557. break;
  1558. case ValidateResult::AcceptAllContactsForThisBodyPair:
  1559. // Accept this and all following contacts from this body
  1560. mValidateBodyPair = false;
  1561. break;
  1562. case ValidateResult::RejectContact:
  1563. return;
  1564. case ValidateResult::RejectAllContactsForThisBodyPair:
  1565. // Reject this and all following contacts from this body
  1566. mRejectAll = true;
  1567. ForceEarlyOut();
  1568. return;
  1569. }
  1570. }
  1571. // This is the earliest hit so far, store it
  1572. mCCDBody.mContactNormal = normal;
  1573. mCCDBody.mBodyID2 = inResult.mBodyID2;
  1574. mCCDBody.mSubShapeID2 = inResult.mSubShapeID2;
  1575. mCCDBody.mFraction = fraction;
  1576. mCCDBody.mFractionPlusSlop = fraction_plus_slop;
  1577. mResult = inResult;
  1578. // Result was assuming body 2 is not moving, but it is, so we need to correct for it
  1579. Vec3 movement2 = fraction * sCalculateBodyMotion(body2, mDeltaTime);
  1580. if (!movement2.IsNearZero())
  1581. {
  1582. mResult.mContactPointOn1 += movement2;
  1583. mResult.mContactPointOn2 += movement2;
  1584. for (Vec3 &v : mResult.mShape1Face)
  1585. v += movement2;
  1586. for (Vec3 &v : mResult.mShape2Face)
  1587. v += movement2;
  1588. }
  1589. // Update early out fraction
  1590. UpdateEarlyOutFraction(fraction_plus_slop);
  1591. }
  1592. }
  1593. }
  1594. }
  1595. bool mValidateBodyPair; ///< If we still have to call the ValidateContactPoint for this body pair
  1596. bool mRejectAll; ///< Reject all further contacts between this body pair
  1597. private:
  1598. const BodyManager & mBodyManager;
  1599. ContactConstraintManager & mContactConstraintManager;
  1600. CCDBody & mCCDBody;
  1601. ShapeCastResult & mResult;
  1602. float mDeltaTime;
  1603. BodyID mAcceptedBodyID;
  1604. };
  1605. // Narrowphase collector
  1606. ShapeCastResult cast_shape_result;
  1607. CCDNarrowPhaseCollector np_collector(mBodyManager, mContactManager, ccd_body, cast_shape_result, ioContext->mStepDeltaTime);
  1608. // This collector wraps the narrowphase collector and collects the closest hit
  1609. class CCDBroadPhaseCollector : public CastShapeBodyCollector
  1610. {
  1611. public:
  1612. CCDBroadPhaseCollector(const CCDBody &inCCDBody, const Body &inBody1, const RShapeCast &inShapeCast, ShapeCastSettings &inShapeCastSettings, SimShapeFilterWrapper &inShapeFilter, CCDNarrowPhaseCollector &ioCollector, const BodyManager &inBodyManager, PhysicsUpdateContext::Step *inStep, float inDeltaTime) :
  1613. mCCDBody(inCCDBody),
  1614. mBody1(inBody1),
  1615. mBody1Extent(inShapeCast.mShapeWorldBounds.GetExtent()),
  1616. mShapeCast(inShapeCast),
  1617. mShapeCastSettings(inShapeCastSettings),
  1618. mShapeFilter(inShapeFilter),
  1619. mCollector(ioCollector),
  1620. mBodyManager(inBodyManager),
  1621. mStep(inStep),
  1622. mDeltaTime(inDeltaTime)
  1623. {
  1624. }
  1625. virtual void AddHit(const BroadPhaseCastResult &inResult) override
  1626. {
  1627. JPH_PROFILE_FUNCTION();
  1628. JPH_ASSERT(inResult.mFraction <= GetEarlyOutFraction(), "This hit should not have been passed on to the collector");
  1629. // Test if we're colliding with ourselves
  1630. if (mBody1.GetID() == inResult.mBodyID)
  1631. return;
  1632. // Avoid treating duplicates, if both bodies are doing CCD then only consider collision if body ID < other body ID
  1633. const Body &body2 = mBodyManager.GetBody(inResult.mBodyID);
  1634. const CCDBody *ccd_body2 = sGetCCDBody(body2, mStep);
  1635. if (ccd_body2 != nullptr && mCCDBody.mBodyID1 > ccd_body2->mBodyID1)
  1636. return;
  1637. // Test group filter
  1638. if (!mBody1.GetCollisionGroup().CanCollide(body2.GetCollisionGroup()))
  1639. return;
  1640. // TODO: For now we ignore sensors
  1641. if (body2.IsSensor())
  1642. return;
  1643. // Get relative movement of these two bodies
  1644. Vec3 direction = mShapeCast.mDirection - sCalculateBodyMotion(body2, mDeltaTime);
  1645. // Test if the remaining movement is less than our movement threshold
  1646. if (direction.LengthSq() < mCCDBody.mLinearCastThresholdSq)
  1647. return;
  1648. // 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
  1649. AABox bounds = body2.GetWorldSpaceBounds();
  1650. bounds.mMin -= mBody1Extent;
  1651. bounds.mMax += mBody1Extent;
  1652. float hit_fraction = RayAABox(Vec3(mShapeCast.mCenterOfMassStart.GetTranslation()), RayInvDirection(direction), bounds.mMin, bounds.mMax);
  1653. if (hit_fraction > GetPositiveEarlyOutFraction()) // If early out fraction <= 0, we have the possibility of finding a deeper hit so we need to clamp the early out fraction
  1654. return;
  1655. // Reset collector (this is a new body pair)
  1656. mCollector.ResetEarlyOutFraction(GetEarlyOutFraction());
  1657. mCollector.mValidateBodyPair = true;
  1658. mCollector.mRejectAll = false;
  1659. // Set body ID on shape filter
  1660. mShapeFilter.SetBody2(&body2);
  1661. // Provide direction as hint for the active edges algorithm
  1662. mShapeCastSettings.mActiveEdgeMovementDirection = direction;
  1663. // Do narrow phase collision check
  1664. RShapeCast relative_cast(mShapeCast.mShape, mShapeCast.mScale, mShapeCast.mCenterOfMassStart, direction, mShapeCast.mShapeWorldBounds);
  1665. body2.GetTransformedShape().CastShape(relative_cast, mShapeCastSettings, mShapeCast.mCenterOfMassStart.GetTranslation(), mCollector, mShapeFilter.GetFilter());
  1666. // Update early out fraction based on narrow phase collector
  1667. if (!mCollector.mRejectAll)
  1668. UpdateEarlyOutFraction(mCollector.GetEarlyOutFraction());
  1669. }
  1670. const CCDBody & mCCDBody;
  1671. const Body & mBody1;
  1672. Vec3 mBody1Extent;
  1673. RShapeCast mShapeCast;
  1674. ShapeCastSettings & mShapeCastSettings;
  1675. SimShapeFilterWrapper & mShapeFilter;
  1676. CCDNarrowPhaseCollector & mCollector;
  1677. const BodyManager & mBodyManager;
  1678. PhysicsUpdateContext::Step *mStep;
  1679. float mDeltaTime;
  1680. };
  1681. // Create shape filter
  1682. SimShapeFilterWrapper shape_filter(mSimShapeFilter, &body);
  1683. #ifdef JPH_TRACK_SIMULATION_STATS
  1684. uint64 start_tick = GetProcessorTickCount();
  1685. #endif
  1686. // Check if we collide with any other body. Note that we use the non-locking interface as we know the broadphase cannot be modified at this point.
  1687. RShapeCast shape_cast(body.GetShape(), Vec3::sOne(), body.GetCenterOfMassTransform(), ccd_body.mDeltaPosition);
  1688. CCDBroadPhaseCollector bp_collector(ccd_body, body, shape_cast, settings, shape_filter, np_collector, mBodyManager, ioStep, ioContext->mStepDeltaTime);
  1689. mBroadPhase->CastAABoxNoLock({ shape_cast.mShapeWorldBounds, shape_cast.mDirection }, bp_collector, broadphase_layer_filter, object_layer_filter);
  1690. #ifdef JPH_TRACK_SIMULATION_STATS
  1691. uint64 num_ticks = GetProcessorTickCount() - start_tick;
  1692. body.GetMotionPropertiesUnchecked()->GetSimulationStats().mCCDTicks.fetch_add(num_ticks, memory_order_relaxed);
  1693. #endif
  1694. // Check if there was a hit
  1695. if (ccd_body.mFractionPlusSlop < 1.0f)
  1696. {
  1697. const Body &body2 = mBodyManager.GetBody(ccd_body.mBodyID2);
  1698. // Determine contact manifold
  1699. ContactManifold manifold;
  1700. manifold.mBaseOffset = shape_cast.mCenterOfMassStart.GetTranslation();
  1701. ManifoldBetweenTwoFaces(cast_shape_result.mContactPointOn1, cast_shape_result.mContactPointOn2, cast_shape_result.mPenetrationAxis, mPhysicsSettings.mManifoldTolerance, cast_shape_result.mShape1Face, cast_shape_result.mShape2Face, manifold.mRelativeContactPointsOn1, manifold.mRelativeContactPointsOn2 JPH_IF_DEBUG_RENDERER(, manifold.mBaseOffset));
  1702. manifold.mSubShapeID1 = cast_shape_result.mSubShapeID1;
  1703. manifold.mSubShapeID2 = cast_shape_result.mSubShapeID2;
  1704. manifold.mPenetrationDepth = cast_shape_result.mPenetrationDepth;
  1705. manifold.mWorldSpaceNormal = ccd_body.mContactNormal;
  1706. // Call contact point callbacks
  1707. mContactManager.OnCCDContactAdded(contact_allocator, body, body2, manifold, ccd_body.mContactSettings);
  1708. if (ccd_body.mContactSettings.mIsSensor)
  1709. {
  1710. // If this is a sensor, we don't want to solve the contact
  1711. ccd_body.mFractionPlusSlop = 1.0f;
  1712. ccd_body.mBodyID2 = BodyID();
  1713. }
  1714. else
  1715. {
  1716. // Calculate the average position from the manifold (this will result in the same impulse applied as when we apply impulses to all contact points)
  1717. if (manifold.mRelativeContactPointsOn2.size() > 1)
  1718. {
  1719. Vec3 average_contact_point = Vec3::sZero();
  1720. for (const Vec3 &v : manifold.mRelativeContactPointsOn2)
  1721. average_contact_point += v;
  1722. average_contact_point /= (float)manifold.mRelativeContactPointsOn2.size();
  1723. ccd_body.mContactPointOn2 = manifold.mBaseOffset + average_contact_point;
  1724. }
  1725. else
  1726. ccd_body.mContactPointOn2 = manifold.mBaseOffset + cast_shape_result.mContactPointOn2;
  1727. }
  1728. }
  1729. }
  1730. // Collect information from the contact allocator and accumulate it in the step.
  1731. sFinalizeContactAllocator(*ioStep, contact_allocator);
  1732. }
  1733. void PhysicsSystem::JobResolveCCDContacts(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  1734. {
  1735. #ifdef JPH_ENABLE_ASSERTS
  1736. // Read/write body access
  1737. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::ReadWrite);
  1738. // We activate bodies that we collide with
  1739. BodyManager::GrantActiveBodiesAccess grant_active(true, false);
  1740. #endif
  1741. uint32 num_active_bodies_after_find_collisions = ioStep->mActiveBodyReadIdx;
  1742. TempAllocator *temp_allocator = ioContext->mTempAllocator;
  1743. // Check if there's anything to do
  1744. uint num_ccd_bodies = ioStep->mNumCCDBodies;
  1745. if (num_ccd_bodies > 0)
  1746. {
  1747. // Sort on fraction so that we process earliest collisions first
  1748. // This is needed to make the simulation deterministic and also to be able to stop contact processing
  1749. // between body pairs if an earlier hit was found involving the body by another CCD body
  1750. // (if it's body ID < this CCD body's body ID - see filtering logic in CCDBroadPhaseCollector)
  1751. CCDBody **sorted_ccd_bodies = (CCDBody **)temp_allocator->Allocate(num_ccd_bodies * sizeof(CCDBody *));
  1752. JPH_SCOPE_EXIT([temp_allocator, sorted_ccd_bodies, num_ccd_bodies]{ temp_allocator->Free(sorted_ccd_bodies, num_ccd_bodies * sizeof(CCDBody *)); });
  1753. {
  1754. JPH_PROFILE("Sort");
  1755. // We don't want to copy the entire struct (it's quite big), so we create a pointer array first
  1756. CCDBody *src_ccd_bodies = ioStep->mCCDBodies;
  1757. CCDBody **dst_ccd_bodies = sorted_ccd_bodies;
  1758. CCDBody **dst_ccd_bodies_end = dst_ccd_bodies + num_ccd_bodies;
  1759. while (dst_ccd_bodies < dst_ccd_bodies_end)
  1760. *(dst_ccd_bodies++) = src_ccd_bodies++;
  1761. // Which we then sort
  1762. QuickSort(sorted_ccd_bodies, sorted_ccd_bodies + num_ccd_bodies, [](const CCDBody *inBody1, const CCDBody *inBody2)
  1763. {
  1764. if (inBody1->mFractionPlusSlop != inBody2->mFractionPlusSlop)
  1765. return inBody1->mFractionPlusSlop < inBody2->mFractionPlusSlop;
  1766. return inBody1->mBodyID1 < inBody2->mBodyID1;
  1767. });
  1768. }
  1769. // We can collide with bodies that are not active, we track them here so we can activate them in one go at the end.
  1770. // This is also needed because we can't modify the active body array while we iterate it.
  1771. static constexpr int cBodiesBatch = 64;
  1772. BodyID *bodies_to_activate = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  1773. int num_bodies_to_activate = 0;
  1774. // We can move bodies that are not part of an island. In this case we need to notify the broadphase of the movement.
  1775. BodyID *bodies_to_update_bounds = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  1776. int num_bodies_to_update_bounds = 0;
  1777. for (uint i = 0; i < num_ccd_bodies; ++i)
  1778. {
  1779. const CCDBody *ccd_body = sorted_ccd_bodies[i];
  1780. Body &body1 = mBodyManager.GetBody(ccd_body->mBodyID1);
  1781. MotionProperties *body_mp = body1.GetMotionProperties();
  1782. // If there was a hit
  1783. if (!ccd_body->mBodyID2.IsInvalid())
  1784. {
  1785. Body &body2 = mBodyManager.GetBody(ccd_body->mBodyID2);
  1786. // Determine if the other body has a CCD body
  1787. CCDBody *ccd_body2 = sGetCCDBody(body2, ioStep);
  1788. if (ccd_body2 != nullptr)
  1789. {
  1790. JPH_ASSERT(ccd_body2->mBodyID2 != ccd_body->mBodyID1, "If we collided with another body, that other body should have ignored collisions with us!");
  1791. // Check if the other body found a hit that is further away
  1792. if (ccd_body2->mFraction > ccd_body->mFraction)
  1793. {
  1794. // Reset the colliding body of the other CCD body. The other body will shorten its distance traveled and will not do any collision response (we'll do that).
  1795. // This means that at this point we have triggered a contact point add/persist for our further hit by accident for the other body.
  1796. // 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.
  1797. ccd_body2->mBodyID2 = BodyID();
  1798. ccd_body2->mFractionPlusSlop = ccd_body->mFraction;
  1799. }
  1800. }
  1801. // 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.
  1802. // 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.
  1803. if (ccd_body2 == nullptr || ccd_body2->mFraction >= ccd_body->mFraction)
  1804. {
  1805. const ContactSettings &contact_settings = ccd_body->mContactSettings;
  1806. // Calculate contact point velocity for body 1
  1807. Vec3 r1_plus_u = Vec3(ccd_body->mContactPointOn2 - (body1.GetCenterOfMassPosition() + ccd_body->mFraction * ccd_body->mDeltaPosition));
  1808. Vec3 v1 = body1.GetPointVelocityCOM(r1_plus_u);
  1809. // Calculate inverse mass for body 1
  1810. float inv_m1 = contact_settings.mInvMassScale1 * body_mp->GetInverseMass();
  1811. if (body2.IsRigidBody())
  1812. {
  1813. // Calculate contact point velocity for body 2
  1814. Vec3 r2 = Vec3(ccd_body->mContactPointOn2 - body2.GetCenterOfMassPosition());
  1815. Vec3 v2 = body2.GetPointVelocityCOM(r2);
  1816. // Calculate relative contact velocity
  1817. Vec3 relative_velocity = v2 - v1;
  1818. float normal_velocity = relative_velocity.Dot(ccd_body->mContactNormal);
  1819. // Calculate velocity bias due to restitution
  1820. float normal_velocity_bias;
  1821. if (contact_settings.mCombinedRestitution > 0.0f && normal_velocity < -mPhysicsSettings.mMinVelocityForRestitution)
  1822. normal_velocity_bias = contact_settings.mCombinedRestitution * normal_velocity;
  1823. else
  1824. normal_velocity_bias = 0.0f;
  1825. // Get inverse mass of body 2
  1826. float inv_m2 = body2.GetMotionPropertiesUnchecked() != nullptr? contact_settings.mInvMassScale2 * body2.GetMotionPropertiesUnchecked()->GetInverseMassUnchecked() : 0.0f;
  1827. // Solve contact constraint
  1828. AxisConstraintPart contact_constraint;
  1829. contact_constraint.CalculateConstraintPropertiesWithMassOverride(body1, inv_m1, contact_settings.mInvInertiaScale1, r1_plus_u, body2, inv_m2, contact_settings.mInvInertiaScale2, r2, ccd_body->mContactNormal, normal_velocity_bias);
  1830. contact_constraint.SolveVelocityConstraintWithMassOverride(body1, inv_m1, body2, inv_m2, ccd_body->mContactNormal, -FLT_MAX, FLT_MAX);
  1831. // Apply friction
  1832. if (contact_settings.mCombinedFriction > 0.0f)
  1833. {
  1834. // Calculate friction direction by removing normal velocity from the relative velocity
  1835. Vec3 friction_direction = relative_velocity - normal_velocity * ccd_body->mContactNormal;
  1836. float friction_direction_len_sq = friction_direction.LengthSq();
  1837. if (friction_direction_len_sq > 1.0e-12f)
  1838. {
  1839. // Normalize friction direction
  1840. friction_direction /= sqrt(friction_direction_len_sq);
  1841. // Calculate max friction impulse
  1842. float max_lambda_f = contact_settings.mCombinedFriction * contact_constraint.GetTotalLambda();
  1843. AxisConstraintPart friction;
  1844. friction.CalculateConstraintPropertiesWithMassOverride(body1, inv_m1, contact_settings.mInvInertiaScale1, r1_plus_u, body2, inv_m2, contact_settings.mInvInertiaScale2, r2, friction_direction);
  1845. friction.SolveVelocityConstraintWithMassOverride(body1, inv_m1, body2, inv_m2, friction_direction, -max_lambda_f, max_lambda_f);
  1846. }
  1847. }
  1848. // Clamp velocity of body 2
  1849. if (body2.IsDynamic())
  1850. {
  1851. MotionProperties *body2_mp = body2.GetMotionProperties();
  1852. body2_mp->ClampLinearVelocity();
  1853. body2_mp->ClampAngularVelocity();
  1854. }
  1855. }
  1856. else
  1857. {
  1858. SoftBodyMotionProperties *soft_mp = static_cast<SoftBodyMotionProperties *>(body2.GetMotionProperties());
  1859. const SoftBodyShape *soft_shape = static_cast<const SoftBodyShape *>(body2.GetShape());
  1860. // Convert the sub shape ID of the soft body to a face
  1861. uint32 face_idx = soft_shape->GetFaceIndex(ccd_body->mSubShapeID2);
  1862. const SoftBodyMotionProperties::Face &face = soft_mp->GetFace(face_idx);
  1863. // Get vertices of the face
  1864. SoftBodyMotionProperties::Vertex &vtx0 = soft_mp->GetVertex(face.mVertex[0]);
  1865. SoftBodyMotionProperties::Vertex &vtx1 = soft_mp->GetVertex(face.mVertex[1]);
  1866. SoftBodyMotionProperties::Vertex &vtx2 = soft_mp->GetVertex(face.mVertex[2]);
  1867. // Inverse mass of the face
  1868. float vtx0_mass = vtx0.mInvMass > 0.0f? 1.0f / vtx0.mInvMass : 1.0e10f;
  1869. float vtx1_mass = vtx1.mInvMass > 0.0f? 1.0f / vtx1.mInvMass : 1.0e10f;
  1870. float vtx2_mass = vtx2.mInvMass > 0.0f? 1.0f / vtx2.mInvMass : 1.0e10f;
  1871. float inv_m2 = 1.0f / (vtx0_mass + vtx1_mass + vtx2_mass);
  1872. // Calculate barycentric coordinates of the contact point on the soft body's face
  1873. float u, v, w;
  1874. RMat44 inv_body2_transform = body2.GetInverseCenterOfMassTransform();
  1875. Vec3 local_contact = Vec3(inv_body2_transform * ccd_body->mContactPointOn2);
  1876. ClosestPoint::GetBaryCentricCoordinates(vtx0.mPosition - local_contact, vtx1.mPosition - local_contact, vtx2.mPosition - local_contact, u, v, w);
  1877. // Calculate contact point velocity for the face
  1878. Vec3 v2 = inv_body2_transform.Multiply3x3Transposed(u * vtx0.mVelocity + v * vtx1.mVelocity + w * vtx2.mVelocity);
  1879. float normal_velocity = (v2 - v1).Dot(ccd_body->mContactNormal);
  1880. // Calculate velocity bias due to restitution
  1881. float normal_velocity_bias;
  1882. if (contact_settings.mCombinedRestitution > 0.0f && normal_velocity < -mPhysicsSettings.mMinVelocityForRestitution)
  1883. normal_velocity_bias = contact_settings.mCombinedRestitution * normal_velocity;
  1884. else
  1885. normal_velocity_bias = 0.0f;
  1886. // Calculate resulting velocity change (the math here is similar to AxisConstraintPart but without an inertia term for body 2 as we treat it as a point mass)
  1887. Vec3 r1_plus_u_x_n = r1_plus_u.Cross(ccd_body->mContactNormal);
  1888. Vec3 invi1_r1_plus_u_x_n = contact_settings.mInvInertiaScale1 * body1.GetInverseInertia().Multiply3x3(r1_plus_u_x_n);
  1889. float jv = r1_plus_u_x_n.Dot(body_mp->GetAngularVelocity()) - normal_velocity - normal_velocity_bias;
  1890. float inv_effective_mass = inv_m1 + inv_m2 + invi1_r1_plus_u_x_n.Dot(r1_plus_u_x_n);
  1891. float lambda = jv / inv_effective_mass;
  1892. body_mp->SubLinearVelocityStep((lambda * inv_m1) * ccd_body->mContactNormal);
  1893. body_mp->SubAngularVelocityStep(lambda * invi1_r1_plus_u_x_n);
  1894. Vec3 delta_v2 = inv_body2_transform.Multiply3x3(lambda * ccd_body->mContactNormal);
  1895. vtx0.mVelocity += delta_v2 * vtx0.mInvMass;
  1896. vtx1.mVelocity += delta_v2 * vtx1.mInvMass;
  1897. vtx2.mVelocity += delta_v2 * vtx2.mInvMass;
  1898. }
  1899. // Clamp velocity of body 1
  1900. body_mp->ClampLinearVelocity();
  1901. body_mp->ClampAngularVelocity();
  1902. // Activate the 2nd body if it is not already active
  1903. if (body2.IsDynamic() && !body2.IsActive())
  1904. {
  1905. bodies_to_activate[num_bodies_to_activate++] = ccd_body->mBodyID2;
  1906. if (num_bodies_to_activate == cBodiesBatch)
  1907. {
  1908. // Batch is full, activate now
  1909. mBodyManager.ActivateBodies(bodies_to_activate, num_bodies_to_activate);
  1910. num_bodies_to_activate = 0;
  1911. }
  1912. }
  1913. #ifdef JPH_DEBUG_RENDERER
  1914. if (sDrawMotionQualityLinearCast)
  1915. {
  1916. // Draw the collision location
  1917. RMat44 collision_transform = body1.GetCenterOfMassTransform().PostTranslated(ccd_body->mFraction * ccd_body->mDeltaPosition);
  1918. body1.GetShape()->Draw(DebugRenderer::sInstance, collision_transform, Vec3::sOne(), Color::sYellow, false, true);
  1919. // Draw the collision location + slop
  1920. RMat44 collision_transform_plus_slop = body1.GetCenterOfMassTransform().PostTranslated(ccd_body->mFractionPlusSlop * ccd_body->mDeltaPosition);
  1921. body1.GetShape()->Draw(DebugRenderer::sInstance, collision_transform_plus_slop, Vec3::sOne(), Color::sOrange, false, true);
  1922. // Draw contact normal
  1923. DebugRenderer::sInstance->DrawArrow(ccd_body->mContactPointOn2, ccd_body->mContactPointOn2 - ccd_body->mContactNormal, Color::sYellow, 0.1f);
  1924. // Draw post contact velocity
  1925. DebugRenderer::sInstance->DrawArrow(collision_transform.GetTranslation(), collision_transform.GetTranslation() + body1.GetLinearVelocity(), Color::sOrange, 0.1f);
  1926. DebugRenderer::sInstance->DrawArrow(collision_transform.GetTranslation(), collision_transform.GetTranslation() + body1.GetAngularVelocity(), Color::sPurple, 0.1f);
  1927. }
  1928. #endif // JPH_DEBUG_RENDERER
  1929. }
  1930. }
  1931. // Update body position
  1932. body1.AddPositionStep(ccd_body->mDeltaPosition * ccd_body->mFractionPlusSlop);
  1933. // If the body was activated due to an earlier CCD step it will have an index in the active
  1934. // body list that it higher than the highest one we processed during FindCollisions
  1935. // which means it hasn't been assigned an island and will not be updated by an island
  1936. // this means that we need to update its bounds manually
  1937. if (body_mp->GetIndexInActiveBodiesInternal() >= num_active_bodies_after_find_collisions)
  1938. {
  1939. body1.CalculateWorldSpaceBoundsInternal();
  1940. bodies_to_update_bounds[num_bodies_to_update_bounds++] = body1.GetID();
  1941. if (num_bodies_to_update_bounds == cBodiesBatch)
  1942. {
  1943. // Buffer full, flush now
  1944. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  1945. num_bodies_to_update_bounds = 0;
  1946. }
  1947. }
  1948. }
  1949. // Activate the requested bodies
  1950. if (num_bodies_to_activate > 0)
  1951. mBodyManager.ActivateBodies(bodies_to_activate, num_bodies_to_activate);
  1952. // Notify change bounds on requested bodies
  1953. if (num_bodies_to_update_bounds > 0)
  1954. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  1955. }
  1956. // Ensure we free the CCD bodies array now, will not call the destructor!
  1957. temp_allocator->Free(ioStep->mActiveBodyToCCDBody, ioStep->mNumActiveBodyToCCDBody * sizeof(int));
  1958. ioStep->mActiveBodyToCCDBody = nullptr;
  1959. ioStep->mNumActiveBodyToCCDBody = 0;
  1960. temp_allocator->Free(ioStep->mCCDBodies, ioStep->mCCDBodiesCapacity * sizeof(CCDBody));
  1961. ioStep->mCCDBodies = nullptr;
  1962. ioStep->mCCDBodiesCapacity = 0;
  1963. }
  1964. void PhysicsSystem::JobContactRemovedCallbacks(const PhysicsUpdateContext::Step *ioStep)
  1965. {
  1966. #ifdef JPH_ENABLE_ASSERTS
  1967. // We don't touch any bodies
  1968. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::None);
  1969. #endif
  1970. // Reset the Body::EFlags::InvalidateContactCache flag for all bodies
  1971. mBodyManager.ValidateContactCacheForAllBodies();
  1972. // Finalize the contact cache (this swaps the read and write versions of the contact cache)
  1973. // Trigger all contact removed callbacks by looking at last step contact points that have not been flagged as reused
  1974. mContactManager.FinalizeContactCacheAndCallContactPointRemovedCallbacks(ioStep->mNumBodyPairs, ioStep->mNumManifolds);
  1975. }
  1976. class PhysicsSystem::BodiesToSleep : public NonCopyable
  1977. {
  1978. public:
  1979. static constexpr int cBodiesToSleepSize = 512;
  1980. static constexpr int cMaxBodiesToPutInBuffer = 128;
  1981. inline BodiesToSleep(BodyManager &inBodyManager, BodyID *inBodiesToSleepBuffer) : mBodyManager(inBodyManager), mBodiesToSleepBuffer(inBodiesToSleepBuffer), mBodiesToSleepCur(inBodiesToSleepBuffer) { }
  1982. inline ~BodiesToSleep()
  1983. {
  1984. // Flush the bodies to sleep buffer
  1985. int num_bodies_in_buffer = int(mBodiesToSleepCur - mBodiesToSleepBuffer);
  1986. if (num_bodies_in_buffer > 0)
  1987. mBodyManager.DeactivateBodies(mBodiesToSleepBuffer, num_bodies_in_buffer);
  1988. }
  1989. inline void PutToSleep(const BodyID *inBegin, const BodyID *inEnd)
  1990. {
  1991. int num_bodies_to_sleep = int(inEnd - inBegin);
  1992. if (num_bodies_to_sleep > cMaxBodiesToPutInBuffer)
  1993. {
  1994. // Too many bodies, deactivate immediately
  1995. mBodyManager.DeactivateBodies(inBegin, num_bodies_to_sleep);
  1996. }
  1997. else
  1998. {
  1999. // Check if there's enough space in the bodies to sleep buffer
  2000. int num_bodies_in_buffer = int(mBodiesToSleepCur - mBodiesToSleepBuffer);
  2001. if (num_bodies_in_buffer + num_bodies_to_sleep > cBodiesToSleepSize)
  2002. {
  2003. // Flush the bodies to sleep buffer
  2004. mBodyManager.DeactivateBodies(mBodiesToSleepBuffer, num_bodies_in_buffer);
  2005. mBodiesToSleepCur = mBodiesToSleepBuffer;
  2006. }
  2007. // Copy the bodies in the buffer
  2008. memcpy(mBodiesToSleepCur, inBegin, num_bodies_to_sleep * sizeof(BodyID));
  2009. mBodiesToSleepCur += num_bodies_to_sleep;
  2010. }
  2011. }
  2012. private:
  2013. BodyManager & mBodyManager;
  2014. BodyID * mBodiesToSleepBuffer;
  2015. BodyID * mBodiesToSleepCur;
  2016. };
  2017. void PhysicsSystem::CheckSleepAndUpdateBounds(uint32 inIslandIndex, const PhysicsUpdateContext *ioContext, const PhysicsUpdateContext::Step *ioStep, BodiesToSleep &ioBodiesToSleep)
  2018. {
  2019. // Get the bodies that belong to this island
  2020. BodyID *bodies_begin, *bodies_end;
  2021. mIslandBuilder.GetBodiesInIsland(inIslandIndex, bodies_begin, bodies_end);
  2022. // Only check sleeping in the last step
  2023. // Also resets force and torque used during the apply gravity phase
  2024. if (ioStep->mIsLast)
  2025. {
  2026. JPH_PROFILE("Check Sleeping");
  2027. static_assert(int(ECanSleep::CannotSleep) == 0 && int(ECanSleep::CanSleep) == 1, "Loop below makes this assumption");
  2028. int all_can_sleep = mPhysicsSettings.mAllowSleeping? int(ECanSleep::CanSleep) : int(ECanSleep::CannotSleep);
  2029. float time_before_sleep = mPhysicsSettings.mTimeBeforeSleep;
  2030. float max_movement = mPhysicsSettings.mPointVelocitySleepThreshold * time_before_sleep;
  2031. for (const BodyID *body_id = bodies_begin; body_id < bodies_end; ++body_id)
  2032. {
  2033. Body &body = mBodyManager.GetBody(*body_id);
  2034. // Update bounding box
  2035. body.CalculateWorldSpaceBoundsInternal();
  2036. // Update sleeping
  2037. all_can_sleep &= int(body.UpdateSleepStateInternal(ioContext->mStepDeltaTime, max_movement, time_before_sleep));
  2038. // Reset force and torque
  2039. MotionProperties *mp = body.GetMotionProperties();
  2040. mp->ResetForce();
  2041. mp->ResetTorque();
  2042. }
  2043. // If all bodies indicate they can sleep we can deactivate them
  2044. if (all_can_sleep == int(ECanSleep::CanSleep))
  2045. ioBodiesToSleep.PutToSleep(bodies_begin, bodies_end);
  2046. }
  2047. else
  2048. {
  2049. JPH_PROFILE("Update Bounds");
  2050. // Update bounding box only for all other steps
  2051. for (const BodyID *body_id = bodies_begin; body_id < bodies_end; ++body_id)
  2052. {
  2053. Body &body = mBodyManager.GetBody(*body_id);
  2054. body.CalculateWorldSpaceBoundsInternal();
  2055. }
  2056. }
  2057. // Notify broadphase of changed objects (find ccd contacts can do linear casts in the next step, so we need to do this every step)
  2058. // Note: Shuffles the BodyID's around!!!
  2059. mBroadPhase->NotifyBodiesAABBChanged(bodies_begin, int(bodies_end - bodies_begin), false);
  2060. }
  2061. void PhysicsSystem::JobSolvePositionConstraints(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  2062. {
  2063. #ifdef JPH_ENABLE_ASSERTS
  2064. // We fix up position errors
  2065. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::ReadWrite);
  2066. // Can only deactivate bodies
  2067. BodyManager::GrantActiveBodiesAccess grant_active(false, true);
  2068. #endif
  2069. float delta_time = ioContext->mStepDeltaTime;
  2070. float baumgarte = mPhysicsSettings.mBaumgarte;
  2071. Constraint **active_constraints = ioContext->mActiveConstraints;
  2072. // Keep a buffer of bodies that need to go to sleep in order to not constantly lock the active bodies mutex and create contention between all solving threads
  2073. BodiesToSleep bodies_to_sleep(mBodyManager, (BodyID *)JPH_STACK_ALLOC(BodiesToSleep::cBodiesToSleepSize * sizeof(BodyID)));
  2074. bool check_islands = true, check_split_islands = mPhysicsSettings.mUseLargeIslandSplitter;
  2075. for (;;)
  2076. {
  2077. // First try to get work from large islands
  2078. if (check_split_islands)
  2079. {
  2080. bool first_iteration;
  2081. uint split_island_index;
  2082. uint32 *constraints_begin, *constraints_end, *contacts_begin, *contacts_end;
  2083. switch (mLargeIslandSplitter.FetchNextBatch(split_island_index, constraints_begin, constraints_end, contacts_begin, contacts_end, first_iteration))
  2084. {
  2085. case LargeIslandSplitter::EStatus::BatchRetrieved:
  2086. {
  2087. #ifdef JPH_TRACK_SIMULATION_STATS
  2088. uint64 start_tick = GetProcessorTickCount();
  2089. #endif
  2090. // Solve the batch
  2091. ConstraintManager::sSolvePositionConstraints(active_constraints, constraints_begin, constraints_end, delta_time, baumgarte);
  2092. mContactManager.SolvePositionConstraints(contacts_begin, contacts_end);
  2093. // Mark the batch as processed
  2094. bool last_iteration, final_batch;
  2095. mLargeIslandSplitter.MarkBatchProcessed(split_island_index, constraints_begin, constraints_end, contacts_begin, contacts_end, last_iteration, final_batch);
  2096. // The final batch will update all bounds and check sleeping
  2097. if (final_batch)
  2098. CheckSleepAndUpdateBounds(mLargeIslandSplitter.GetIslandIndex(split_island_index), ioContext, ioStep, bodies_to_sleep);
  2099. #ifdef JPH_TRACK_SIMULATION_STATS
  2100. uint64 num_ticks = GetProcessorTickCount() - start_tick;
  2101. mIslandBuilder.GetIslandStats(mLargeIslandSplitter.GetIslandIndex(split_island_index)).mPositionConstraintTicks.fetch_add(num_ticks, memory_order_relaxed);
  2102. #endif
  2103. // We processed work, loop again
  2104. continue;
  2105. }
  2106. case LargeIslandSplitter::EStatus::WaitingForBatch:
  2107. break;
  2108. case LargeIslandSplitter::EStatus::AllBatchesDone:
  2109. check_split_islands = false;
  2110. break;
  2111. }
  2112. }
  2113. // If that didn't succeed try to process an island
  2114. if (check_islands)
  2115. {
  2116. // Next island
  2117. uint32 island_idx = ioStep->mSolvePositionConstraintsNextIsland++;
  2118. if (island_idx >= mIslandBuilder.GetNumIslands())
  2119. {
  2120. // We processed all islands, stop checking islands
  2121. check_islands = false;
  2122. continue;
  2123. }
  2124. JPH_PROFILE("Island");
  2125. // Get iterators for this island
  2126. uint32 *constraints_begin, *constraints_end, *contacts_begin, *contacts_end;
  2127. mIslandBuilder.GetConstraintsInIsland(island_idx, constraints_begin, constraints_end);
  2128. mIslandBuilder.GetContactsInIsland(island_idx, contacts_begin, contacts_end);
  2129. // If this island is a large island, it will be picked up as a batch and we don't need to do anything here
  2130. uint num_items = uint(constraints_end - constraints_begin) + uint(contacts_end - contacts_begin);
  2131. if (mPhysicsSettings.mUseLargeIslandSplitter
  2132. && num_items >= LargeIslandSplitter::cLargeIslandTreshold)
  2133. continue;
  2134. #ifdef JPH_TRACK_SIMULATION_STATS
  2135. uint64 start_tick = GetProcessorTickCount();
  2136. #endif
  2137. // Check if this island needs solving
  2138. if (num_items > 0)
  2139. {
  2140. // Iterate
  2141. uint num_position_steps = mIslandBuilder.GetNumPositionSteps(island_idx);
  2142. for (uint position_step = 0; position_step < num_position_steps; ++position_step)
  2143. {
  2144. bool applied_impulse = ConstraintManager::sSolvePositionConstraints(active_constraints, constraints_begin, constraints_end, delta_time, baumgarte);
  2145. applied_impulse |= mContactManager.SolvePositionConstraints(contacts_begin, contacts_end);
  2146. if (!applied_impulse)
  2147. break;
  2148. }
  2149. }
  2150. // After solving we will update all bounds and check sleeping
  2151. CheckSleepAndUpdateBounds(island_idx, ioContext, ioStep, bodies_to_sleep);
  2152. #ifdef JPH_TRACK_SIMULATION_STATS
  2153. // Average the total ticks spent over the number of bodies and assign each the average number of ticks per body
  2154. uint64 num_ticks = GetProcessorTickCount() - start_tick;
  2155. mIslandBuilder.GetIslandStats(island_idx).mPositionConstraintTicks.fetch_add(num_ticks, memory_order_relaxed);
  2156. #endif
  2157. // We processed work, loop again
  2158. continue;
  2159. }
  2160. if (check_islands)
  2161. {
  2162. // If there are islands, we don't need to wait and can pick up new work
  2163. continue;
  2164. }
  2165. else if (check_split_islands)
  2166. {
  2167. // If there are split islands, but we didn't do any work, give up a time slice
  2168. std::this_thread::yield();
  2169. }
  2170. else
  2171. {
  2172. // No more work
  2173. break;
  2174. }
  2175. }
  2176. }
  2177. void PhysicsSystem::JobSoftBodyPrepare(PhysicsUpdateContext *ioContext, PhysicsUpdateContext::Step *ioStep)
  2178. {
  2179. JPH_PROFILE_FUNCTION();
  2180. {
  2181. #ifdef JPH_ENABLE_ASSERTS
  2182. // Reading soft body positions
  2183. BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
  2184. #endif
  2185. // Get the active soft bodies
  2186. BodyIDVector active_bodies;
  2187. mBodyManager.GetActiveBodies(EBodyType::SoftBody, active_bodies);
  2188. // Quit if there are no active soft bodies
  2189. if (active_bodies.empty())
  2190. {
  2191. // Kick the next step
  2192. if (ioStep->mStartNextStep.IsValid())
  2193. ioStep->mStartNextStep.RemoveDependency();
  2194. return;
  2195. }
  2196. // Sort to get a deterministic update order
  2197. QuickSort(active_bodies.begin(), active_bodies.end());
  2198. // Allocate soft body contexts
  2199. ioContext->mNumSoftBodies = (uint)active_bodies.size();
  2200. ioContext->mSoftBodyUpdateContexts = (SoftBodyUpdateContext *)ioContext->mTempAllocator->Allocate(ioContext->mNumSoftBodies * sizeof(SoftBodyUpdateContext));
  2201. // Initialize soft body contexts
  2202. for (SoftBodyUpdateContext *sb_ctx = ioContext->mSoftBodyUpdateContexts, *sb_ctx_end = ioContext->mSoftBodyUpdateContexts + ioContext->mNumSoftBodies; sb_ctx < sb_ctx_end; ++sb_ctx)
  2203. {
  2204. new (sb_ctx) SoftBodyUpdateContext;
  2205. Body &body = mBodyManager.GetBody(active_bodies[sb_ctx - ioContext->mSoftBodyUpdateContexts]);
  2206. SoftBodyMotionProperties *mp = static_cast<SoftBodyMotionProperties *>(body.GetMotionProperties());
  2207. mp->InitializeUpdateContext(ioContext->mStepDeltaTime, body, *this, *sb_ctx);
  2208. }
  2209. }
  2210. // We're ready to collide the first soft body
  2211. ioContext->mSoftBodyToCollide.store(0, memory_order_release);
  2212. // Determine number of jobs to spawn
  2213. int num_soft_body_jobs = ioContext->GetMaxConcurrency();
  2214. // Create finalize job
  2215. ioStep->mSoftBodyFinalize = ioContext->mJobSystem->CreateJob("SoftBodyFinalize", cColorSoftBodyFinalize, [ioContext, ioStep]()
  2216. {
  2217. ioContext->mPhysicsSystem->JobSoftBodyFinalize(ioContext);
  2218. // Kick the next step
  2219. if (ioStep->mStartNextStep.IsValid())
  2220. ioStep->mStartNextStep.RemoveDependency();
  2221. }, num_soft_body_jobs); // depends on: soft body simulate
  2222. ioContext->mBarrier->AddJob(ioStep->mSoftBodyFinalize);
  2223. // Create simulate jobs
  2224. ioStep->mSoftBodySimulate.resize(num_soft_body_jobs);
  2225. for (int i = 0; i < num_soft_body_jobs; ++i)
  2226. ioStep->mSoftBodySimulate[i] = ioContext->mJobSystem->CreateJob("SoftBodySimulate", cColorSoftBodySimulate, [ioStep, i]()
  2227. {
  2228. ioStep->mContext->mPhysicsSystem->JobSoftBodySimulate(ioStep->mContext, i);
  2229. ioStep->mSoftBodyFinalize.RemoveDependency();
  2230. }, num_soft_body_jobs); // depends on: soft body collide
  2231. ioContext->mBarrier->AddJobs(ioStep->mSoftBodySimulate.data(), ioStep->mSoftBodySimulate.size());
  2232. // Create collision jobs
  2233. ioStep->mSoftBodyCollide.resize(num_soft_body_jobs);
  2234. for (int i = 0; i < num_soft_body_jobs; ++i)
  2235. ioStep->mSoftBodyCollide[i] = ioContext->mJobSystem->CreateJob("SoftBodyCollide", cColorSoftBodyCollide, [ioContext, ioStep]()
  2236. {
  2237. ioContext->mPhysicsSystem->JobSoftBodyCollide(ioContext);
  2238. for (const JobHandle &h : ioStep->mSoftBodySimulate)
  2239. h.RemoveDependency();
  2240. }); // depends on: nothing
  2241. ioContext->mBarrier->AddJobs(ioStep->mSoftBodyCollide.data(), ioStep->mSoftBodyCollide.size());
  2242. }
  2243. void PhysicsSystem::JobSoftBodyCollide(PhysicsUpdateContext *ioContext) const
  2244. {
  2245. #ifdef JPH_ENABLE_ASSERTS
  2246. // Reading rigid body positions and velocities
  2247. BodyAccess::Grant grant(BodyAccess::EAccess::Read, BodyAccess::EAccess::Read);
  2248. #endif
  2249. for (;;)
  2250. {
  2251. // Fetch the next soft body
  2252. uint sb_idx = ioContext->mSoftBodyToCollide.fetch_add(1, std::memory_order_acquire);
  2253. if (sb_idx >= ioContext->mNumSoftBodies)
  2254. break;
  2255. // Do a broadphase check
  2256. SoftBodyUpdateContext &sb_ctx = ioContext->mSoftBodyUpdateContexts[sb_idx];
  2257. sb_ctx.mMotionProperties->DetermineCollidingShapes(sb_ctx, *this, GetBodyLockInterfaceNoLock());
  2258. }
  2259. }
  2260. void PhysicsSystem::JobSoftBodySimulate(PhysicsUpdateContext *ioContext, uint inThreadIndex) const
  2261. {
  2262. #ifdef JPH_ENABLE_ASSERTS
  2263. // Updating velocities of soft bodies, allow the contact listener to read the soft body state
  2264. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::Read);
  2265. #endif
  2266. // Calculate at which body we start to distribute the workload across the threads
  2267. uint num_soft_bodies = ioContext->mNumSoftBodies;
  2268. uint start_idx = inThreadIndex * num_soft_bodies / ioContext->GetMaxConcurrency();
  2269. // Keep running partial updates until everything has been updated
  2270. uint status;
  2271. do
  2272. {
  2273. // Reset status
  2274. status = 0;
  2275. // Update all soft bodies
  2276. for (uint i = 0; i < num_soft_bodies; ++i)
  2277. {
  2278. // Fetch the soft body context
  2279. SoftBodyUpdateContext &sb_ctx = ioContext->mSoftBodyUpdateContexts[(start_idx + i) % num_soft_bodies];
  2280. // To avoid trashing the cache too much, we prefer to stick to one soft body until we cannot progress it any further
  2281. uint sb_status;
  2282. do
  2283. {
  2284. sb_status = (uint)sb_ctx.mMotionProperties->ParallelUpdate(sb_ctx, mPhysicsSettings);
  2285. status |= sb_status;
  2286. } while (sb_status == (uint)SoftBodyMotionProperties::EStatus::DidWork);
  2287. }
  2288. // If we didn't perform any work, yield the thread so that something else can run
  2289. if (!(status & (uint)SoftBodyMotionProperties::EStatus::DidWork))
  2290. std::this_thread::yield();
  2291. }
  2292. while (status != (uint)SoftBodyMotionProperties::EStatus::Done);
  2293. }
  2294. void PhysicsSystem::JobSoftBodyFinalize(PhysicsUpdateContext *ioContext)
  2295. {
  2296. #ifdef JPH_ENABLE_ASSERTS
  2297. // Updating rigid body velocities and soft body positions / velocities
  2298. BodyAccess::Grant grant(BodyAccess::EAccess::ReadWrite, BodyAccess::EAccess::ReadWrite);
  2299. // Can activate and deactivate bodies
  2300. BodyManager::GrantActiveBodiesAccess grant_active(true, true);
  2301. #endif
  2302. static constexpr int cBodiesBatch = 64;
  2303. BodyID *bodies_to_update_bounds = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  2304. int num_bodies_to_update_bounds = 0;
  2305. BodyID *bodies_to_put_to_sleep = (BodyID *)JPH_STACK_ALLOC(cBodiesBatch * sizeof(BodyID));
  2306. int num_bodies_to_put_to_sleep = 0;
  2307. for (SoftBodyUpdateContext *sb_ctx = ioContext->mSoftBodyUpdateContexts, *sb_ctx_end = ioContext->mSoftBodyUpdateContexts + ioContext->mNumSoftBodies; sb_ctx < sb_ctx_end; ++sb_ctx)
  2308. {
  2309. // Apply the rigid body velocity deltas
  2310. sb_ctx->mMotionProperties->UpdateRigidBodyVelocities(*sb_ctx, GetBodyInterfaceNoLock());
  2311. // Update the position
  2312. sb_ctx->mBody->SetPositionAndRotationInternal(sb_ctx->mBody->GetPosition() + sb_ctx->mDeltaPosition, sb_ctx->mBody->GetRotation(), false);
  2313. BodyID id = sb_ctx->mBody->GetID();
  2314. bodies_to_update_bounds[num_bodies_to_update_bounds++] = id;
  2315. if (num_bodies_to_update_bounds == cBodiesBatch)
  2316. {
  2317. // Buffer full, flush now
  2318. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  2319. num_bodies_to_update_bounds = 0;
  2320. }
  2321. if (sb_ctx->mCanSleep == ECanSleep::CanSleep)
  2322. {
  2323. // This body should go to sleep
  2324. bodies_to_put_to_sleep[num_bodies_to_put_to_sleep++] = id;
  2325. if (num_bodies_to_put_to_sleep == cBodiesBatch)
  2326. {
  2327. mBodyManager.DeactivateBodies(bodies_to_put_to_sleep, num_bodies_to_put_to_sleep);
  2328. num_bodies_to_put_to_sleep = 0;
  2329. }
  2330. }
  2331. }
  2332. // Notify change bounds on requested bodies
  2333. if (num_bodies_to_update_bounds > 0)
  2334. mBroadPhase->NotifyBodiesAABBChanged(bodies_to_update_bounds, num_bodies_to_update_bounds, false);
  2335. // Notify bodies to go to sleep
  2336. if (num_bodies_to_put_to_sleep > 0)
  2337. mBodyManager.DeactivateBodies(bodies_to_put_to_sleep, num_bodies_to_put_to_sleep);
  2338. // Free soft body contexts
  2339. ioContext->mTempAllocator->Free(ioContext->mSoftBodyUpdateContexts, ioContext->mNumSoftBodies * sizeof(SoftBodyUpdateContext));
  2340. }
  2341. void PhysicsSystem::SaveState(StateRecorder &inStream, EStateRecorderState inState, const StateRecorderFilter *inFilter) const
  2342. {
  2343. JPH_PROFILE_FUNCTION();
  2344. inStream.Write(inState);
  2345. if (uint8(inState) & uint8(EStateRecorderState::Global))
  2346. {
  2347. inStream.Write(mPreviousStepDeltaTime);
  2348. inStream.Write(mGravity);
  2349. }
  2350. if (uint8(inState) & uint8(EStateRecorderState::Bodies))
  2351. mBodyManager.SaveState(inStream, inFilter);
  2352. if (uint8(inState) & uint8(EStateRecorderState::Contacts))
  2353. mContactManager.SaveState(inStream, inFilter);
  2354. if (uint8(inState) & uint8(EStateRecorderState::Constraints))
  2355. mConstraintManager.SaveState(inStream, inFilter);
  2356. }
  2357. bool PhysicsSystem::RestoreState(StateRecorder &inStream, const StateRecorderFilter *inFilter)
  2358. {
  2359. JPH_PROFILE_FUNCTION();
  2360. EStateRecorderState state = EStateRecorderState::All; // Set this value for validation. If a partial state is saved, validation will not work anyway.
  2361. inStream.Read(state);
  2362. if (uint8(state) & uint8(EStateRecorderState::Global))
  2363. {
  2364. inStream.Read(mPreviousStepDeltaTime);
  2365. inStream.Read(mGravity);
  2366. }
  2367. if (uint8(state) & uint8(EStateRecorderState::Bodies))
  2368. {
  2369. if (!mBodyManager.RestoreState(inStream))
  2370. return false;
  2371. // Update bounding boxes for all bodies in the broadphase
  2372. if (inStream.IsLastPart())
  2373. {
  2374. Array<BodyID> bodies;
  2375. for (const Body *b : mBodyManager.GetBodies())
  2376. if (BodyManager::sIsValidBodyPointer(b) && b->IsInBroadPhase())
  2377. bodies.push_back(b->GetID());
  2378. if (!bodies.empty())
  2379. mBroadPhase->NotifyBodiesAABBChanged(&bodies[0], (int)bodies.size());
  2380. }
  2381. }
  2382. if (uint8(state) & uint8(EStateRecorderState::Contacts))
  2383. {
  2384. if (!mContactManager.RestoreState(inStream, inFilter))
  2385. return false;
  2386. }
  2387. if (uint8(state) & uint8(EStateRecorderState::Constraints))
  2388. {
  2389. if (!mConstraintManager.RestoreState(inStream))
  2390. return false;
  2391. }
  2392. return true;
  2393. }
  2394. void PhysicsSystem::SaveBodyState(const Body &inBody, StateRecorder &inStream) const
  2395. {
  2396. mBodyManager.SaveBodyState(inBody, inStream);
  2397. }
  2398. void PhysicsSystem::RestoreBodyState(Body &ioBody, StateRecorder &inStream)
  2399. {
  2400. mBodyManager.RestoreBodyState(ioBody, inStream);
  2401. BodyID id = ioBody.GetID();
  2402. mBroadPhase->NotifyBodiesAABBChanged(&id, 1);
  2403. }
  2404. JPH_NAMESPACE_END