| 109 | } |
| 110 | |
| 111 | void PhysicsSystem::Update(float inDeltaTime, int inCollisionSteps, int inIntegrationSubSteps, TempAllocator *inTempAllocator, JobSystem *inJobSystem) |
| 112 | { |
| 113 | JPH_PROFILE_FUNCTION(); |
| 114 | |
| 115 | JPH_ASSERT(inDeltaTime >= 0.0f); |
| 116 | JPH_ASSERT(inIntegrationSubSteps <= PhysicsUpdateContext::cMaxSubSteps); |
| 117 | |
| 118 | // Sync point for the broadphase. This will allow it to do clean up operations without having any mutexes locked yet. |
| 119 | mBroadPhase->FrameSync(); |
| 120 | |
| 121 | // If there are no active bodies or there's no time delta |
| 122 | uint32 num_active_bodies = mBodyManager.GetNumActiveBodies(); |
| 123 | if (num_active_bodies == 0 || inDeltaTime <= 0.0f) |
| 124 | { |
| 125 | mBodyManager.LockAllBodies(); |
| 126 | |
| 127 | // Update broadphase |
| 128 | mBroadPhase->LockModifications(); |
| 129 | BroadPhase::UpdateState update_state = mBroadPhase->UpdatePrepare(); |
| 130 | mBroadPhase->UpdateFinalize(update_state); |
| 131 | mBroadPhase->UnlockModifications(); |
| 132 | |
| 133 | // Call contact removal callbacks from contacts that existed in the previous update |
| 134 | mContactManager.ContactPointRemovedCallbacks(); |
| 135 | mContactManager.FinalizeContactCache(0, 0); |
| 136 | |
| 137 | mBodyManager.UnlockAllBodies(); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | // Calculate ratio between current and previous frame delta time to scale initial constraint forces |
| 142 | float sub_step_delta_time = inDeltaTime / (inCollisionSteps * inIntegrationSubSteps); |
| 143 | float warm_start_impulse_ratio = mPhysicsSettings.mConstraintWarmStart && mPreviousSubStepDeltaTime > 0.0f? sub_step_delta_time / mPreviousSubStepDeltaTime : 0.0f; |
| 144 | mPreviousSubStepDeltaTime = sub_step_delta_time; |
| 145 | |
| 146 | // Create the context used for passing information between jobs |
| 147 | PhysicsUpdateContext context; |
| 148 | context.mPhysicsSystem = this; |
| 149 | context.mTempAllocator = inTempAllocator; |
| 150 | context.mJobSystem = inJobSystem; |
| 151 | context.mBarrier = inJobSystem->CreateBarrier(); |
| 152 | context.mIslandBuilder = &mIslandBuilder; |
| 153 | context.mStepDeltaTime = inDeltaTime / inCollisionSteps; |
| 154 | context.mSubStepDeltaTime = sub_step_delta_time; |
| 155 | context.mWarmStartImpulseRatio = warm_start_impulse_ratio; |
| 156 | |
| 157 | // Allocate space for body pairs |
| 158 | JPH_ASSERT(context.mBodyPairs == nullptr); |
| 159 | context.mBodyPairs = static_cast<BodyPair *>(inTempAllocator->Allocate(sizeof(BodyPair) * mPhysicsSettings.mMaxInFlightBodyPairs)); |
| 160 | |
| 161 | // Lock all bodies for write so that we can freely touch them |
| 162 | mStepListenersMutex.lock(); |
| 163 | mBodyManager.LockAllBodies(); |
| 164 | mBroadPhase->LockModifications(); |
| 165 | |
| 166 | // Get max number of concurrent jobs |
| 167 | int max_concurrency = context.GetMaxConcurrency(); |
| 168 | |