40 #ifndef _physicsphysicsmanager_cpp
41 #define _physicsphysicsmanager_cpp
45 #include "Physics/physicsmanager.h"
46 #include "Physics/collision.h"
48 #include "Physics/ghostproxy.h"
49 #include "Physics/rigidproxy.h"
50 #include "Physics/softproxy.h"
52 #include "Graphics/graphicsmanager.h"
55 #include "actormanager.h"
56 #include "areaeffectmanager.h"
59 #include "stringtool.h"
60 #include "linegroup.h"
62 #include "worldtrigger.h"
63 #include "worldobject.h"
64 #include "crossplatform.h"
67 #include "Physics/collisiondispatcher.h.cpp"
72 #include <btBulletDynamicsCommon.h>
73 #include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
74 #include <BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h>
75 #include <BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h>
76 #include <BulletCollision/CollisionDispatch/btGhostObject.h>
79 #define __PHYSICS_COMMON_H__ 1
82 #include <BulletMultiThreaded/Win32ThreadSupport.h>
84 #include <BulletMultiThreaded/PosixThreadSupport.h>
87 #include <BulletMultiThreaded/btParallelConstraintSolver.h>
88 #include <BulletMultiThreaded/SpuGatheringCollisionDispatcher.h>
89 #include <BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h>
125 virtual void PrepareForUpdate();
128 virtual void FinalizeUpdate();
136 virtual void drawLine(
const btVector3& from,
const btVector3& to,
const btVector3& color);
145 virtual void drawContactPoint(
const btVector3& PointOnB,
const btVector3& normalOnB,btScalar distance,
int lifeTime,
const btVector3& color);
151 virtual void draw3dText(
const btVector3& location,
const char* textString);
158 virtual void setDebugMode(
int debugMode);
163 virtual int getDebugMode()
const;
170 virtual void reportErrorWarning(
const char* warningString);
173 InternalDebugDrawer::InternalDebugDrawer()
175 this->DebugDrawing = Physics::DDM_NoDebug;
179 InternalDebugDrawer::~InternalDebugDrawer()
181 delete this->WireFrame;
184 void InternalDebugDrawer::PrepareForUpdate()
186 if( this->WireFrame != NULL ) {
187 this->WireFrame->AddToWorld();
188 this->WireFrame->ClearLines();
192 void InternalDebugDrawer::FinalizeUpdate()
194 if( this->WireFrame != NULL ) {
195 this->WireFrame->DrawLines();
199 void InternalDebugDrawer::drawLine(
const btVector3& from,
const btVector3& to,
const btVector3& color)
204 void InternalDebugDrawer::drawContactPoint(
const btVector3& PointOnB,
const btVector3& normalOnB,btScalar distance,
int lifeTime,
const btVector3& color)
208 void InternalDebugDrawer::draw3dText(
const btVector3& location,
const char* textString)
212 void InternalDebugDrawer::setDebugMode(
int debugMode)
214 this->DebugDrawing = debugMode;
215 if( this->WireFrame != NULL ) {
216 if( this->DebugDrawing != Physics::DDM_NoDebug ) {
217 this->WireFrame->AddToWorld();
219 this->WireFrame->RemoveFromWorld();
224 int InternalDebugDrawer::getDebugMode()
const
226 return this->DebugDrawing;
229 void InternalDebugDrawer::reportErrorWarning(
const char* warningString)
231 String temp(warningString);
251 TargetManager(Target) { }
284 TargetManager(Target) { }
327 TargetManager(Target) { }
351 TargetManager(Target) { }
375 SimulationPaused(false),
382 BulletSolverThreads(NULL),
383 BulletDispatcherThreads(NULL),
384 BulletBroadphase(NULL),
385 BulletCollisionConfiguration(NULL),
386 BulletDispatcher(NULL),
388 BulletDynamicsWorld(NULL),
391 SimulationWork(NULL),
392 WorldTriggerUpdateWork(NULL),
394 ThreadResources(NULL)
402 SimulationPaused(false),
409 BulletSolverThreads(NULL),
410 BulletDispatcherThreads(NULL),
411 BulletBroadphase(NULL),
412 BulletCollisionConfiguration(NULL),
413 BulletDispatcher(NULL),
415 BulletDynamicsWorld(NULL),
418 SimulationWork(NULL),
419 WorldTriggerUpdateWork(NULL),
421 ThreadResources(NULL)
427 SimulationPaused(false),
434 BulletSolverThreads(NULL),
435 BulletDispatcherThreads(NULL),
436 BulletBroadphase(NULL),
437 BulletCollisionConfiguration(NULL),
438 BulletDispatcher(NULL),
440 BulletDynamicsWorld(NULL),
443 SimulationWork(NULL),
444 WorldTriggerUpdateWork(NULL),
446 ThreadResources(NULL)
452 if(!WorldSettings.
Empty())
454 CurrAttrib = WorldSettings.
GetAttribute(
"LimitlessWorld");
455 if(!CurrAttrib.
Empty())
459 CurrAttrib = WorldSettings.
GetAttribute(
"WorldUpperBounds");
460 if(!CurrAttrib.
Empty())
462 CurrAttrib = WorldSettings.
GetAttribute(
"WorldLowerBounds");
463 if(!CurrAttrib.
Empty())
466 if(!CurrAttrib.
Empty())
469 CurrAttrib = WorldSettings.
GetAttribute(
"SoftRigidWorld");
470 if(!CurrAttrib.
Empty())
474 CurrAttrib = WorldSettings.
GetAttribute(
"MultiThreaded");
475 if(!CurrAttrib.
Empty())
484 if(!StepModifier.
Empty())
487 if(!CurrAttrib.
Empty()) {
493 if(!DebugRender.
Empty())
496 CurrAttrib = WorldSettings.
GetAttribute(
"RenderingMode");
497 if(!CurrAttrib.
Empty())
498 RenderMode = CurrAttrib.
AsInt();
500 if(0 != RenderMode) {
508 btCollisionObjectArray ObjectArray( BulletDynamicsWorld->getCollisionObjectArray() );
509 for(
Integer X = 0 ; X < BulletDynamicsWorld->getNumCollisionObjects() ; ++X )
531 this->BulletBroadphase =
new btDbvtBroadphase();
547 this->BulletCollisionConfiguration =
new btSoftBodyRigidBodyCollisionConfiguration();
555 Win32ThreadSupport::Win32ThreadConstructionInfo BulletThreadInfo(
"DispatcherThreads",
556 processCollisionTask,
557 createCollisionLocalStoreMemory,
559 this->BulletDispatcherThreads =
new Win32ThreadSupport(BulletThreadInfo);
561 PosixThreadSupport::ThreadConstructionInfo BulletThreadInfo(
"DispatcherThreads",
562 processCollisionTask,
563 createCollisionLocalStoreMemory,
565 this->BulletDispatcherThreads =
new PosixThreadSupport(BulletThreadInfo);
567 this->BulletDispatcher =
new ParallelCollisionDispatcher(this->BulletDispatcherThreads,ThreadCount,this->BulletCollisionConfiguration);
575 Win32ThreadSupport::Win32ThreadConstructionInfo BulletThreadInfo(
"SolverThreads",
579 this->BulletSolverThreads =
new Win32ThreadSupport(BulletThreadInfo);
580 this->BulletSolverThreads->startSPU();
582 PosixThreadSupport::ThreadConstructionInfo BulletThreadInfo(
"SolverThreads",
586 this->BulletSolverThreads =
new PosixThreadSupport(BulletThreadInfo);
589 this->BulletSolver =
new btSequentialImpulseConstraintSolver();
591 this->BulletSolver =
new btSequentialImpulseConstraintSolver();
596 this->BulletDynamicsWorld =
new btSoftRigidDynamicsWorld( this->BulletDispatcher,
597 this->BulletBroadphase,
599 this->BulletCollisionConfiguration);
617 btGImpactCollisionAlgorithm::registerAlgorithm(this->BulletDispatcher);
619 this->GhostCallback =
new btGhostPairCallback();
620 this->BulletBroadphase->getOverlappingPairCache()->setInternalGhostPairCallback(this->GhostCallback);
624 this->BulletDynamicsWorld->getWorldInfo().m_dispatcher = this->BulletDispatcher;
625 this->BulletDynamicsWorld->getWorldInfo().m_broadphase = this->BulletBroadphase;
626 this->BulletDynamicsWorld->getWorldInfo().m_sparsesdf.Initialize();
628 this->BulletDynamicsWorld->getDispatchInfo().m_enableSPU =
true;
629 this->BulletDynamicsWorld->getDispatchInfo().m_useContinuous =
true;
637 this->WorldConstructionInfo = Info;
642 delete this->BulletDynamicsWorld;
643 this->BulletDynamicsWorld = NULL;
644 delete this->BulletDispatcher;
645 this->BulletDispatcher = NULL;
646 delete this->BulletCollisionConfiguration;
647 this->BulletCollisionConfiguration = NULL;
648 delete this->BulletSolver;
649 this->BulletSolver = NULL;
650 delete this->BulletBroadphase;
651 this->BulletBroadphase = NULL;
652 delete this->GhostCallback;
653 this->GhostCallback = NULL;
655 delete this->BulletDrawer;
656 this->BulletDrawer = NULL;
658 if(BulletSolverThreads) {
659 delete this->BulletSolverThreads;
660 this->BulletSolverThreads = NULL;
662 if(BulletDispatcherThreads) {
663 delete this->BulletDispatcherThreads;
664 this->BulletDispatcherThreads = NULL;
667 if(this->ThreadCount)
687 if( !Triggers.empty() )
689 for( std::vector<WorldTrigger*>::iterator Trig = Triggers.begin() ; Trig != Triggers.end() ; Trig++ )
691 if((*Trig)->ConditionsAreMet())
692 (*Trig)->ApplyTrigger();
701 (*ColIt).second->Update();
706 if(AlgoQueue->empty())
714 btCollisionAlgorithm* NewAlgo = AlgoQueue->front();
715 while( NewAlgo != NULL )
721 btBroadphasePairArray& PairArray = BulletBroadphase->getOverlappingPairCache()->getOverlappingPairArray();
722 for(
Integer X = 0 ; X < PairArray.size() ; ++X )
724 if( NewAlgo == PairArray[X].m_algorithm )
726 btCollisionObject* COA = (btCollisionObject*)PairArray[X].m_pProxy0->m_clientObject;
727 ProxA = static_cast<CollidableProxy*>( COA->getUserPointer() );
728 btCollisionObject* COB = (btCollisionObject*)PairArray[X].m_pProxy1->m_clientObject;
729 ProxB = static_cast<CollidableProxy*>( COB->getUserPointer() );
739 if(ColIt == Collisions.end())
747 AlgoQueue->pop_front();
748 if(AlgoQueue->size() > 0) NewAlgo = AlgoQueue->front();
763 this->SimulationPaused =
Pause;
768 return this->SimulationPaused;
781 Vector3 grav(this->BulletDynamicsWorld->getGravity());
787 this->BulletDynamicsWorld->getWorldInfo().m_gravity = sgrav.
GetBulletVector3();
792 Vector3 sgrav(this->BulletDynamicsWorld->getWorldInfo().m_gravity);
802 this->Proxies.push_back(NewProxy);
809 this->Proxies.push_back(NewProxy);
816 this->Proxies.push_back(NewProxy);
823 this->Proxies.push_back(NewProxy);
830 this->Proxies.push_back(NewProxy);
837 this->Proxies.push_back(NewProxy);
845 {
return this->Proxies.at(Index); }
848 {
return this->Proxies.size(); }
852 for(
ProxyIterator ProxIt = this->Proxies.begin() ; ProxIt != this->Proxies.end() ; ++ProxIt )
854 if( ToBeDestroyed == (*ProxIt) ) {
855 WorldObject* Parent = (*ProxIt)->GetParentObject();
860 this->Proxies.erase(ProxIt);
868 for(
ProxyIterator ProxIt = this->Proxies.begin() ; ProxIt != this->Proxies.end() ; ++ProxIt )
870 WorldObject* Parent = (*ProxIt)->GetParentObject();
876 this->Proxies.clear();
884 this->BulletDynamicsWorld->addConstraint(Con->
GetConstraintBase(), DisableCollisions);
885 this->Constraints.push_back(Con);
890 return this->Constraints[Index];
895 return this->Constraints.size();
901 for(
ConstraintIterator ConIt = this->Constraints.begin() ; ConIt < this->Constraints.end() ; ConIt++ )
903 if( (*ConIt) == Con )
905 this->Constraints.erase(ConIt);
913 for(
ConstraintIterator Con = this->Constraints.begin() ; Con != this->Constraints.end() ; Con++ )
915 this->BulletDynamicsWorld->removeConstraint((*Con)->GetConstraintBase());
918 this->Constraints.clear();
926 this->Triggers.push_back(Trig);
933 if( Name == (*Trig)->GetName() )
943 return this->Triggers.at(Index);
948 return this->Triggers.size();
957 this->Triggers.erase(T);
965 for(
WorldTriggerIterator Trig = this->Triggers.begin() ; Trig != this->Triggers.end() ; Trig++ )
967 this->Triggers.clear();
976 if(ColIt != this->Collisions.end())
return (*ColIt).second;
982 return this->Collisions.size();
988 btBroadphasePair* btPair = this->BulletBroadphase->getOverlappingPairCache()->findPair(
991 this->BulletBroadphase->getOverlappingPairCache()->removeOverlappingPair(
994 this->BulletDispatcher);
995 this->BulletBroadphase->getOverlappingPairCache()->cleanOverlappingPair(*btPair,this->BulletDispatcher);
1004 this->BulletBroadphase->getOverlappingPairCache()->cleanProxyFromPairs( Proxy->
_GetBasePhysicsObject()->getBroadphaseHandle(), this->BulletDispatcher );
1007 while( ColIt != this->Collisions.end() )
1010 if( Proxy == (*ColIt).second->ProxyA || Proxy == (*ColIt).second->ProxyB ) {
1013 this->Collisions.erase(Delete);
1014 delete ToBeDestroyed;
1023 for(
CollisionIterator ColIt = this->Collisions.begin() ; ColIt != this->Collisions.end() ; ++ColIt )
1025 delete (*ColIt).second;
1027 this->Collisions.clear();
1032 return this->Collisions.begin();
1037 return this->Collisions.end();
1042 return this->Collisions.begin();
1047 return this->Collisions.end();
1055 if( this->BulletDrawer ) {
1056 this->DebugRenderMode = DebugRenderingMode;
1057 this->BulletDrawer->
setDebugMode( DebugRenderingMode );
1063 return this->DebugRenderMode;
1073 else this->
Construct(WorldConstructionInfo);
1075 if( this->
Initialized && this->BulletDrawer == NULL ) {
1077 this->BulletDrawer->
setDebugMode( this->DebugRenderMode );
1078 this->BulletDynamicsWorld->setDebugDrawer( this->BulletDrawer );
1085 btOverlappingPairCache* Pairs = this->BulletBroadphase->getOverlappingPairCache();
1086 int NumPairs = Pairs->getNumOverlappingPairs();
1087 btBroadphasePairArray PairArray = Pairs->getOverlappingPairArray();
1088 for(
Integer X = 0 ; X < NumPairs ; X++ )
1090 btBroadphasePair& CurrPair = PairArray.at(X);
1091 Pairs->cleanOverlappingPair(CurrPair,this->BulletDispatcher);
1092 Pairs->removeOverlappingPair(CurrPair.m_pProxy0,CurrPair.m_pProxy1,this->BulletDispatcher);
1096 int numManifolds = this->BulletDispatcher->getNumManifolds();
1097 for (
int i = 0 ; i < numManifolds ; i++ )
1099 this->BulletDispatcher->releaseManifold(this->BulletDispatcher->getManifoldByIndexInternal(i));
1102 this->BulletBroadphase->resetPool(this->BulletDispatcher);
1103 this->BulletSolver->reset();
1104 this->BulletDynamicsWorld->stepSimulation(1.f/60.f,1,1.f/60.f);
1109 SubstepModifier = Modifier;
1120 this->BulletDynamicsWorld->updateAabbs();
1121 this->BulletBroadphase->calculateOverlappingPairs(this->BulletDispatcher);
1122 this->BulletDispatcher->dispatchAllCollisionPairs(this->BulletDynamicsWorld->getPairCache(),this->BulletDynamicsWorld->getDispatchInfo(),this->BulletDispatcher);
1140 this->BulletDrawer->
setDebugMode( this->DebugRenderMode );
1141 this->BulletDynamicsWorld->setDebugDrawer( this->BulletDrawer );
1182 delete this->BulletDrawer;
1183 this->BulletDrawer = NULL;
1184 this->BulletDynamicsWorld->setDebugDrawer( this->BulletDrawer );
1226 {
return ManagerBase::MT_PhysicsManager; }
1229 {
return "DefaultPhysicsManager"; }
1235 {
return this->BulletDynamicsWorld; }
1238 {
return this->BulletDynamicsWorld; }
1253 return "DefaultPhysicsManager";
1262 for( NameValuePairList::iterator ParIt = Params.begin() ; ParIt != Params.end() ; ++ParIt )
1264 String Lower = (*ParIt).first;
1266 if(
"geographyupperbounds" == Lower )
1270 else if(
"geographylowerbounds" == Lower )
1274 else if(
"maxproxies" == Lower )
1278 else if(
"gravity" == Lower )
1282 else if(
"softrigidworld" == Lower )
1287 else if(
"limitlessworld" == Lower )
1292 else if(
"multithreaded" == Lower )
1309 delete ToBeDestroyed;