diff --git a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp index 0392960c8..9a176af91 100644 --- a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp +++ b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp @@ -82,7 +82,7 @@ const int maxOverlap = 65535; #ifdef _DEBUG -const int numObjects = 20; +const int numObjects = 120; #else const int numObjects = 120; #endif @@ -314,7 +314,7 @@ int main(int argc,char** argv) clientResetScene(); physicsEnvironmentPtr->SyncMotionStates(0.f); - + if (createConstraint) { //physObjects[i]->SetAngularVelocity(0,0,-2,true); int constraintId; diff --git a/Extras/PhysicsInterface/CcdPhysics/CcdPhysicsEnvironment.h b/Extras/PhysicsInterface/CcdPhysics/CcdPhysicsEnvironment.h index ccb4da1f9..aece219e2 100644 --- a/Extras/PhysicsInterface/CcdPhysics/CcdPhysicsEnvironment.h +++ b/Extras/PhysicsInterface/CcdPhysics/CcdPhysicsEnvironment.h @@ -49,12 +49,14 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment SimdVector3 m_gravity; IDebugDraw* m_debugDrawer; + +protected: //solver iterations int m_numIterations; //timestep subdivisions int m_numTimeSubSteps; -protected: + int m_ccdMode; int m_solverType; diff --git a/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.cpp b/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.cpp index 0ab99760a..89bcd1cad 100644 --- a/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.cpp +++ b/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.cpp @@ -40,9 +40,12 @@ ParallelPhysicsEnvironment::~ParallelPhysicsEnvironment() /// Perform an integration step of duration 'timeStep'. bool ParallelPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) { + // Make sure the broadphase / overlapping AABB paircache is up-to-date OverlappingPairCache* scene = m_collisionWorld->GetPairCache(); scene->RefreshOverlappingPairs(); + // Find the connected sets that can be simulated in parallel + // Using union find #ifdef USE_QUICKPROF Profiler::beginBlock("IslandUnionFind"); @@ -73,6 +76,7 @@ bool ParallelPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) } } + //Store the island id in each body GetSimulationIslandManager()->StoreIslandActivationState(GetCollisionWorld()); #ifdef USE_QUICKPROF @@ -80,7 +84,8 @@ bool ParallelPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) #endif //USE_QUICKPROF - ///calculate simulation islands + + ///build simulation islands #ifdef USE_QUICKPROF Profiler::beginBlock("BuildIslands"); @@ -103,9 +108,9 @@ bool ParallelPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) Dispatcher* dispatcher = GetCollisionWorld()->GetDispatcher(); - + //this is a brute force approach, will rethink later about more subtle ways int i; - for (int i=0;i< scene->GetNumOverlappingPairs();i++) + for (i=0;i< scene->GetNumOverlappingPairs();i++) { BroadphasePair* pair = &scene->GetOverlappingPair(i); @@ -114,12 +119,26 @@ bool ParallelPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) if (col0->m_islandTag1 > col1->m_islandTag1) { - simulationIslands[col0->m_islandTag1].m_overlappingPairs.push_back(*pair); + simulationIslands[col0->m_islandTag1].m_overlappingPairIndices.push_back(i); } else { - simulationIslands[col1->m_islandTag1].m_overlappingPairs.push_back(*pair); + simulationIslands[col1->m_islandTag1].m_overlappingPairIndices.push_back(i); } } + + //store constraint indices for each island + for (i=0;i constraint.GetRigidBodyB().m_islandTag1) + { + simulationIslands[constraint.GetRigidBodyA().m_islandTag1].m_constraintIndices.push_back(i); + } else + { + simulationIslands[constraint.GetRigidBodyB().m_islandTag1].m_constraintIndices.push_back(i); + } + + } //add all overlapping pairs for each island @@ -141,18 +160,35 @@ bool ParallelPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) } } - + + #ifdef USE_QUICKPROF + Profiler::endBlock("BuildIslands"); + #endif //USE_QUICKPROF +#ifdef USE_QUICKPROF + Profiler::beginBlock("SimulateIsland"); +#endif //USE_QUICKPROF - //Each simulation island can be processed in parallel + TypedConstraint** constraintBase = 0; + if (m_constraints.size()) + constraintBase = &m_constraints[0]; + + + + //Each simulation island can be processed in parallel (will be put on a job queue) for (k=0;kGetOverlappingPair(0),dispatcher,GetBroadphase(),m_solver,timeStep); } } + +#ifdef USE_QUICKPROF + Profiler::endBlock("SimulateIsland"); +#endif //USE_QUICKPROF + return true; } \ No newline at end of file diff --git a/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.h b/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.h index df56cde45..4c433bf52 100644 --- a/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.h +++ b/Extras/PhysicsInterface/CcdPhysics/ParallelPhysicsEnvironment.h @@ -37,6 +37,7 @@ class ParallelPhysicsEnvironment : public CcdPhysicsEnvironment /// Perform an integration step of duration 'timeStep'. virtual bool proceedDeltaTimeOneStep(float timeStep); + //void BuildSimulationIslands(); }; diff --git a/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.cpp b/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.cpp index 94d180584..d281d6809 100644 --- a/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.cpp +++ b/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.cpp @@ -21,51 +21,15 @@ subject to the following restrictions: #include "BroadphaseCollision/Dispatcher.h" #include "ConstraintSolver/ContactSolverInfo.h" #include "ConstraintSolver/ConstraintSolver.h" +#include "ConstraintSolver/TypedConstraint.h" extern float gContactBreakingTreshold; -bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broadphase,class ConstraintSolver* solver,float timeStep) +bool SimulationIsland::Simulate(int numSolverIterations,TypedConstraint** constraintsBaseAddress,BroadphasePair* overlappingPairBaseAddress, Dispatcher* dispatcher,BroadphaseInterface* broadphase,class ConstraintSolver* solver,float timeStep) { - //then execute all stuff below for each simulation island #ifdef USE_QUICKPROF - Profiler::endBlock("BuildIslands"); -#endif //USE_QUICKPROF - - - - - ///build simulation islands, and add them to a job queue, which can be processed in parallel - ///or on the GPU - - - //printf("CcdPhysicsEnvironment::proceedDeltaTime\n"); - - if (SimdFuzzyZero(timeStep)) - return true; - - // if (m_debugDrawer) - // { - // gDisableDeactivation = (m_debugDrawer->GetDebugMode() & IDebugDraw::DBG_NoDeactivation); - // } - - -#ifdef USE_QUICKPROF - Profiler::beginBlock("SyncMotionStates"); -#endif //USE_QUICKPROF - - - //this is needed because scaling is not known in advance, and scaling has to propagate to the shape - //if (!m_scalingPropagated) - //{ - // SyncMotionStates(timeStep); - // m_scalingPropagated = true; - //} - - -#ifdef USE_QUICKPROF - Profiler::endBlock("SyncMotionStates"); Profiler::beginBlock("predictIntegratedTransform"); #endif //USE_QUICKPROF @@ -119,10 +83,28 @@ bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broa dispatchInfo.m_stepCount = 0; dispatchInfo.m_enableSatConvex = false;//m_enableSatCollisionDetection; - //pairCache->RefreshOverlappingPairs(); - if (m_overlappingPairs.size()) + std::vector overlappingPairs; + overlappingPairs.resize(this->m_overlappingPairIndices.size()); + + //gather overlapping pair info + int i; + for (i=0;iDispatchAllCollisionPairs(&m_overlappingPairs[0],m_overlappingPairs.size(),dispatchInfo);///numsubstep,g); + overlappingPairs[i] = overlappingPairBaseAddress[m_overlappingPairIndices[i]]; + } + + + //pairCache->RefreshOverlappingPairs(); + if (overlappingPairs.size()) + { + dispatcher->DispatchAllCollisionPairs(&overlappingPairs[0],overlappingPairs.size(),dispatchInfo);///numsubstep,g); + } + + //scatter overlapping pair info, mainly the created algorithms/contact caches + + for (i=0;iBuildJacobian(); + constraint->SolveConstraint( timeStep ); - //point to point constraints - for (i=0;i< numConstraints ; i++ ) - { - TypedConstraint* constraint = m_constraints[i]; - - constraint->BuildJacobian(); - constraint->SolveConstraint( timeStep ); - - } + } } @@ -173,6 +153,8 @@ bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broa Profiler::endBlock("SolveConstraint"); #endif //USE_QUICKPROF + /* + //solve the vehicles #ifdef NEW_BULLET_VEHICLE_SUPPORT @@ -185,54 +167,10 @@ bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broa vehicle->UpdateVehicle( timeStep); } #endif //NEW_BULLET_VEHICLE_SUPPORT +*/ - - struct InplaceSolverIslandCallback : public ParallelIslandDispatcher::IslandCallback - { - - ContactSolverInfo& m_solverInfo; - ConstraintSolver* m_solver; - IDebugDraw* m_debugDrawer; - - InplaceSolverIslandCallback( - ContactSolverInfo& solverInfo, - ConstraintSolver* solver, - IDebugDraw* debugDrawer) - :m_solverInfo(solverInfo), - m_solver(solver), - m_debugDrawer(debugDrawer) - { - - } - - virtual void ProcessIsland(PersistentManifold** manifolds,int numManifolds) - { - m_solver->SolveGroup( manifolds, numManifolds,m_solverInfo,m_debugDrawer); - } - - }; - - - m_solverInfo.m_friction = 0.9f; - m_solverInfo.m_numIterations = m_numIterations; - m_solverInfo.m_timeStep = timeStep; - m_solverInfo.m_restitution = 0.f;//m_restitution; - - InplaceSolverIslandCallback solverCallback( - m_solverInfo, - m_solver, - m_debugDrawer); - - #ifdef USE_QUICKPROF - Profiler::beginBlock("BuildAndProcessIslands"); - #endif //USE_QUICKPROF - - /// solve all the contact points and contact friction - GetDispatcher()->BuildAndProcessIslands(m_collisionWorld->GetCollisionObjectArray(),&solverCallback); - - #ifdef USE_QUICKPROF - Profiler::endBlock("BuildAndProcessIslands"); - + /* + Profiler::beginBlock("CallbackTriggers"); #endif //USE_QUICKPROF @@ -249,7 +187,7 @@ bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broa ContactSolverInfo solverInfo; solverInfo.m_friction = 0.9f; - solverInfo.m_numIterations = 10;//m_numIterations; + solverInfo.m_numIterations = numSolverIterations; solverInfo.m_timeStep = timeStep; solverInfo.m_restitution = 0.f;//m_restitution; @@ -275,17 +213,17 @@ bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broa float toi = 1.f; - + //experimental continuous collision detection /* if (m_ccdMode == 3) { - DispatcherInfo dispatchInfo; - dispatchInfo.m_timeStep = timeStep; - dispatchInfo.m_stepCount = 0; - dispatchInfo.m_dispatchFunc = DispatcherInfo::DISPATCH_CONTINUOUS; + DispatcherInfo dispatchInfo; + dispatchInfo.m_timeStep = timeStep; + dispatchInfo.m_stepCount = 0; + dispatchInfo.m_dispatchFunc = DispatcherInfo::DISPATCH_CONTINUOUS; - // GetCollisionWorld()->GetDispatcher()->DispatchAllCollisionPairs(scene,dispatchInfo); - toi = dispatchInfo.m_timeOfImpact; + // GetCollisionWorld()->GetDispatcher()->DispatchAllCollisionPairs(scene,dispatchInfo); + toi = dispatchInfo.m_timeOfImpact; } */ @@ -386,7 +324,6 @@ bool SimulationIsland::Simulate(Dispatcher* dispatcher,BroadphaseInterface* broa #ifdef USE_QUICKPROF Profiler::endBlock("SyncMotionStates"); - Profiler::endProfilingCycle(); #endif //USE_QUICKPROF diff --git a/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.h b/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.h index 8e79e2ca7..fe6f32c64 100644 --- a/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.h +++ b/Extras/PhysicsInterface/CcdPhysics/SimulationIsland.h @@ -30,9 +30,11 @@ class SimulationIsland public: std::vector m_controllers; std::vector m_manifolds; - std::vector m_overlappingPairs; - bool Simulate(Dispatcher* dispatcher,BroadphaseInterface* broadphase, class ConstraintSolver* solver, float timeStep); + std::vector m_overlappingPairIndices; + std::vector m_constraintIndices; + + bool Simulate(int numSolverIterations,class TypedConstraint** constraintsBaseAddress,struct BroadphasePair* overlappingPairBaseAddress, Dispatcher* dispatcher,BroadphaseInterface* broadphase, class ConstraintSolver* solver, float timeStep); int GetNumControllers()