2
0
Эх сурвалжийг харах

Made setting to turn off deterministic simulation in favor of performance (#463)

For simulations involving large islands this can make the simulation about 10% faster.
Jorrit Rouwe 2 жил өмнө
parent
commit
a212e57c35

+ 3 - 0
Jolt/Physics/PhysicsSettings.h

@@ -89,6 +89,9 @@ struct PhysicsSettings
 	/// Velocity of points on bounding box of object below which an object can be considered sleeping (unit: m/s)
 	float		mPointVelocitySleepThreshold = 0.03f;
 
+	/// By default the simulation is deterministic, it is possible to turn this off by setting this setting to false. This will make the simulation run faster but it will no longer be deterministic.
+	bool		mDeterministicSimulation = true;
+
 	///@name These variables are mainly for debugging purposes, they allow turning on/off certain subsystems. You probably want to leave them alone.
 	///@{
 

+ 8 - 4
Jolt/Physics/PhysicsSystem.cpp

@@ -1358,11 +1358,15 @@ void PhysicsSystem::JobSolveVelocityConstraints(PhysicsUpdateContext *ioContext,
 					continue;
 				}
 
-				// Sort constraints to give a deterministic simulation
-				ConstraintManager::sSortConstraints(active_constraints, constraints_begin, constraints_end);
+				// Sorting is costly but needed for a deterministic simulation, allow the user to turn this off
+				if (mPhysicsSettings.mDeterministicSimulation)
+				{
+					// Sort constraints to give a deterministic simulation
+					ConstraintManager::sSortConstraints(active_constraints, constraints_begin, constraints_end);
 
-				// Sort contacts to give a deterministic simulation
-				mContactManager.SortContacts(contacts_begin, contacts_end);
+					// Sort contacts to give a deterministic simulation
+					mContactManager.SortContacts(contacts_begin, contacts_end);
+				}
 			}
 			else
 			{

+ 1 - 0
Samples/SamplesApp.cpp

@@ -401,6 +401,7 @@ SamplesApp::SamplesApp()
 		#if defined(_DEBUG) && !defined(JPH_DISABLE_CUSTOM_ALLOCATOR) && !defined(JPH_COMPILER_MINGW)
 			mDebugUI->CreateCheckBox(phys_settings, "Enable Checking Memory Hook", IsCustomMemoryHookEnabled(), [](UICheckBox::EState inState) { EnableCustomMemoryHook(inState == UICheckBox::STATE_CHECKED); });
 		#endif
+			mDebugUI->CreateCheckBox(phys_settings, "Deterministic Simulation", mPhysicsSettings.mDeterministicSimulation, [this](UICheckBox::EState inState) { mPhysicsSettings.mDeterministicSimulation = inState == UICheckBox::STATE_CHECKED; mPhysicsSystem->SetPhysicsSettings(mPhysicsSettings); });
 			mDebugUI->CreateCheckBox(phys_settings, "Constraint Warm Starting", mPhysicsSettings.mConstraintWarmStart, [this](UICheckBox::EState inState) { mPhysicsSettings.mConstraintWarmStart = inState == UICheckBox::STATE_CHECKED; mPhysicsSystem->SetPhysicsSettings(mPhysicsSettings); });
 			mDebugUI->CreateCheckBox(phys_settings, "Use Body Pair Contact Cache", mPhysicsSettings.mUseBodyPairContactCache, [this](UICheckBox::EState inState) { mPhysicsSettings.mUseBodyPairContactCache = inState == UICheckBox::STATE_CHECKED; mPhysicsSystem->SetPhysicsSettings(mPhysicsSettings); });
 			mDebugUI->CreateCheckBox(phys_settings, "Contact Manifold Reduction", mPhysicsSettings.mUseManifoldReduction, [this](UICheckBox::EState inState) { mPhysicsSettings.mUseManifoldReduction = inState == UICheckBox::STATE_CHECKED; mPhysicsSystem->SetPhysicsSettings(mPhysicsSettings); });