BGE patch: logic optimization part 2: remove inactive sensors from logic manager.
authorBenoit Bolsee <benoit.bolsee@online.be>
Wed, 30 Jul 2008 17:41:47 +0000 (17:41 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Wed, 30 Jul 2008 17:41:47 +0000 (17:41 +0000)
With this patch, only sensors that are connected to
active states are actually registered in the logic
manager. Inactive sensors won't take any CPU,
especially the Radar and Near sensors that use a
physical object for the detection: these objects
are removed from the physics engine.

To take advantage of this optimization patch, you
need to define very light idle state when the
objects are inactive: make them transparent, suspend
the physics, keep few sensors active (e,g a message
sensor to wake up), etc.

43 files changed:
source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Converter/KX_ConvertSensors.cpp
source/gameengine/GameLogic/SCA_ActuatorEventManager.cpp
source/gameengine/GameLogic/SCA_ActuatorEventManager.h
source/gameengine/GameLogic/SCA_AlwaysEventManager.cpp
source/gameengine/GameLogic/SCA_AlwaysEventManager.h
source/gameengine/GameLogic/SCA_EventManager.cpp
source/gameengine/GameLogic/SCA_EventManager.h
source/gameengine/GameLogic/SCA_IObject.cpp
source/gameengine/GameLogic/SCA_ISensor.cpp
source/gameengine/GameLogic/SCA_ISensor.h
source/gameengine/GameLogic/SCA_JoystickManager.cpp
source/gameengine/GameLogic/SCA_JoystickManager.h
source/gameengine/GameLogic/SCA_KeyboardManager.cpp
source/gameengine/GameLogic/SCA_KeyboardManager.h
source/gameengine/GameLogic/SCA_LogicManager.cpp
source/gameengine/GameLogic/SCA_MouseManager.cpp
source/gameengine/GameLogic/SCA_MouseManager.h
source/gameengine/GameLogic/SCA_PropertyEventManager.cpp
source/gameengine/GameLogic/SCA_PropertyEventManager.h
source/gameengine/GameLogic/SCA_RandomEventManager.cpp
source/gameengine/GameLogic/SCA_RandomEventManager.h
source/gameengine/GameLogic/SCA_TimeEventManager.cpp
source/gameengine/GameLogic/SCA_TimeEventManager.h
source/gameengine/Ketsji/KXNetwork/KX_NetworkEventManager.cpp
source/gameengine/Ketsji/KXNetwork/KX_NetworkEventManager.h
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_NearSensor.cpp
source/gameengine/Ketsji/KX_NearSensor.h
source/gameengine/Ketsji/KX_RayEventManager.cpp
source/gameengine/Ketsji/KX_RayEventManager.h
source/gameengine/Ketsji/KX_TouchEventManager.cpp
source/gameengine/Ketsji/KX_TouchEventManager.h
source/gameengine/Ketsji/KX_TouchSensor.cpp
source/gameengine/Ketsji/KX_TouchSensor.h
source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h
source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h
source/gameengine/Physics/Sumo/Fuzzics/src/SM_Scene.cpp
source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.cpp
source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h
source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h

index da1767793bb26640b7f83dd99114667589805255..f44318120e804f09d285e28793562cf3411b177a 100644 (file)
@@ -2441,13 +2441,13 @@ void BL_ConvertBlenderObjects(struct Main* maggie,
                int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
                bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
                BL_ConvertSensors(blenderobj,gameobj,logicmgr,kxscene,keydev,executePriority,layerMask,isInActiveLayer,canvas,converter);
+               // set the init state to all objects
+               gameobj->SetInitState((blenderobj->init_state)?blenderobj->init_state:blenderobj->state);
        }
-       // apply the initial state to controllers
-       for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
+       // apply the initial state to controllers, only on the active objects as this registers the sensors
+       for ( i=0;i<objectlist->GetCount();i++)
        {
-               KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
-               struct Object* blenderobj = converter->FindBlenderObject(gameobj);
-               gameobj->SetInitState((blenderobj->init_state)?blenderobj->init_state:blenderobj->state);
+               KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
                gameobj->ResetState();
        }
 
index 7c9df688d45ca4819915b8071b2b5cdb4db09b47..5e433bb821b8ece88070a937e10139e90ed4c1d1 100644 (file)
@@ -733,9 +733,9 @@ void BL_ConvertSensors(struct Object* blenderobject,
                        gameobj->AddSensor(gamesensor);
                        
                        // only register to manager if it's in an active layer
-                       
-                       if (isInActiveLayer)
-                               gamesensor->RegisterToManager();
+                       // Make registration dynamic: only when sensor is activated
+                       //if (isInActiveLayer)
+                       //      gamesensor->RegisterToManager();
                        
                        
                        for (int i=0;i<sens->totlinks;i++)
index 28ca1fd673f1fa246b77af79c4d12e8afff59ba0..768a3a45937ad844f8b337179d5c4028f01c00c4 100644 (file)
@@ -48,19 +48,10 @@ SCA_ActuatorEventManager::~SCA_ActuatorEventManager()
 
 }
 
-
-
-void SCA_ActuatorEventManager::RegisterSensor(SCA_ISensor* sensor)
-{
-       m_sensors.push_back(sensor);
-}
-
-
-
 void SCA_ActuatorEventManager::NextFrame()
 {
        // check for changed actuator
-       for (vector<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++)
+       for (set<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++)
        {
                (*it)->Activate(m_logicmgr,NULL);
        }
@@ -69,7 +60,7 @@ void SCA_ActuatorEventManager::NextFrame()
 void SCA_ActuatorEventManager::UpdateFrame()
 {
        // update the state of actuator before executing them
-       for (vector<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++)
+       for (set<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++)
        {
                ((SCA_ActuatorSensor*)(*it))->Update();
        }
index b5108764197b216cc6c877fab20228d93e14c024..a7d61627c23390eb6e913f4bd04ce53c0c6cd2b3 100644 (file)
@@ -44,7 +44,6 @@ public:
        virtual ~SCA_ActuatorEventManager();
        virtual void NextFrame();
        virtual void UpdateFrame();
-       virtual void    RegisterSensor(SCA_ISensor* sensor);
        //SCA_LogicManager* GetLogicManager() { return m_logicmgr;}
 };
 
index ab3bc2cc4eeaef9a8496b9d4f91367b245fbc4d5..4cd2dfba99406661d0ea6d4702fe9b3121c2de27 100644 (file)
@@ -51,16 +51,9 @@ SCA_AlwaysEventManager::SCA_AlwaysEventManager(class SCA_LogicManager* logicmgr)
 
 void SCA_AlwaysEventManager::NextFrame()
 {
-       for (vector<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++)
+       for (set<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++)
        {
-               SCA_ISensor* sensor = *i;
-               sensor->Activate(m_logicmgr, NULL);
+               (*i)->Activate(m_logicmgr, NULL);
        }
 }
 
-
-
-void SCA_AlwaysEventManager::RegisterSensor(SCA_ISensor* sensor)
-{
-       m_sensors.push_back(sensor);
-}
index 28c71512875676fb9f02193e7f25d1ebd98e7873..a619eecddd4cb377886d04ce5dbec9e83b9a8a73 100644 (file)
@@ -39,7 +39,6 @@ class SCA_AlwaysEventManager : public SCA_EventManager
 public:
        SCA_AlwaysEventManager(class SCA_LogicManager* logicmgr);
        virtual void NextFrame();
-       virtual void    RegisterSensor(SCA_ISensor* sensor);
 
 
 };
index 0169864a1338ff42f4c92b5692b1f32222653acc..e4fd03795977359f777c2e4f3bee1a5921673a93 100644 (file)
@@ -45,17 +45,14 @@ SCA_EventManager::~SCA_EventManager()
 {
 }
 
-
+void SCA_EventManager::RegisterSensor(class SCA_ISensor* sensor)
+{
+       m_sensors.insert(sensor);
+}
 
 void SCA_EventManager::RemoveSensor(class SCA_ISensor* sensor)
 {
-       std::vector<SCA_ISensor*>::iterator i =
-       std::find(m_sensors.begin(), m_sensors.end(), sensor);
-       if (!(i == m_sensors.end()))
-       {
-               std::swap(*i, m_sensors.back());
-               m_sensors.pop_back();
-       }
+       m_sensors.erase(sensor);
 }
 
 void SCA_EventManager::NextFrame(double curtime, double fixedtime)
index 9cc1718cd1e4a7af1090cf91561bcd9910645a1d..9dbb5a6d24f44b1e68769d221fbc0984d827f46d 100644 (file)
 #define __KX_EVENTMANAGER
 
 #include <vector>
+#include <set>
 #include <algorithm>
 
 class SCA_EventManager
 {
 protected:
-       std::vector <class SCA_ISensor*>                                m_sensors;
+       // use a set to speed-up insertion/removal
+       std::set <class SCA_ISensor*>                           m_sensors;
 
 public:
        enum EVENT_MANAGER_TYPE {
@@ -61,7 +63,7 @@ public:
        virtual void    NextFrame();
        virtual void    UpdateFrame();
        virtual void    EndFrame();
-       virtual void    RegisterSensor(class SCA_ISensor* sensor)=0;
+       virtual void    RegisterSensor(class SCA_ISensor* sensor);
        int             GetType();
 
 protected:
index 25b4af696ea321fbcca6ebe5a35d597c06f1efaf..b0f8decee26ba11b32997b89bb6063f38adba9d3 100644 (file)
@@ -337,12 +337,31 @@ void SCA_IObject::Resume(void)
 
 void SCA_IObject::SetState(unsigned int state)
 {
-       m_state = state;
-       // update the status of the controllers
+       unsigned int tmpstate;
        SCA_ControllerList::iterator contit;
-       for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++)
+
+       // we will update the state in two steps:
+       // 1) set the new state bits that are 1
+       // 2) clr the new state bits that are 0
+       // This to ensure continuity if a sensor is attached to two states
+       // that are switching state: no need to deactive and reactive the sensor 
+       
+       tmpstate = m_state | state;
+       if (tmpstate != m_state)
+       {
+               // update the status of the controllers
+               for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++)
+               {
+                       (*contit)->ApplyState(tmpstate);
+               }
+       }
+       m_state = state;
+       if (m_state != tmpstate)
        {
-               (*contit)->ApplyState(m_state);
+               for (contit = m_controllers.begin(); contit != m_controllers.end(); contit++)
+               {
+                       (*contit)->ApplyState(m_state);
+               }
        }
 }
 
index f11c8047fac317898300a5ff281e03a455912681..2dc49924062e28fcbceaa5801ccf1410f365bd69 100644 (file)
@@ -41,7 +41,8 @@
 void   SCA_ISensor::ReParent(SCA_IObject* parent)
 {
        SCA_ILogicBrick::ReParent(parent);
-       m_eventmgr->RegisterSensor(this);
+       // will be done when the sensor is activated
+       //m_eventmgr->RegisterSensor(this);
        this->SetActive(false);
 }
 
@@ -133,6 +134,7 @@ void SCA_ISensor::DecLink() {
        {
                // sensor is detached from all controllers, initialize it so that it
                // is fresh as at startup when it is reattached again.
+               UnregisterToManager();
                Init();
        }
 }
@@ -203,6 +205,11 @@ void SCA_ISensor::RegisterToManager()
        m_eventmgr->RegisterSensor(this);
 }
 
+void SCA_ISensor::UnregisterToManager()
+{
+       m_eventmgr->RemoveSensor(this);
+}
+
 void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr,     CValue* event)
 {
        
index 51837755ba4e4b2fd64ec19b06938d81e5bcc666..d5dabbce3ee9d3a5a8341d18af082ef66cfa4703 100644 (file)
@@ -115,6 +115,8 @@ public:
        void SetLevel(bool lvl);
 
        void RegisterToManager();
+       void UnregisterToManager();
+
        virtual float GetNumber();
 
        /** Stop sensing for a while. */
@@ -129,7 +131,7 @@ public:
        void ClrLink()
                { m_links = 0; }
        void IncLink()
-               { m_links++; }
+               { if (!m_links++) RegisterToManager(); }
        void DecLink();
        bool IsNoLink() const 
                { return !m_links; }
index 7bf2049e56e86bff61f161a1d8e4f7a5d796826b..8ff28ba0b519247925ebd5cf27cf4fcac570a31a 100644 (file)
@@ -52,9 +52,10 @@ SCA_JoystickManager::~SCA_JoystickManager()
 
 void SCA_JoystickManager::NextFrame(double curtime,double deltatime)
 {
-       for (unsigned int i = 0; i < m_sensors.size(); i++)
+       set<SCA_ISensor*>::iterator it;
+       for (it = m_sensors.begin(); it != m_sensors.end(); it++)
        {
-               SCA_JoystickSensor* joysensor = (SCA_JoystickSensor*) m_sensors[i];
+               SCA_JoystickSensor* joysensor = (SCA_JoystickSensor*)(*it);
                if(!joysensor->IsSuspended())
                {
                        m_joystick->HandleEvents();
@@ -64,12 +65,6 @@ void SCA_JoystickManager::NextFrame(double curtime,double deltatime)
 }
 
 
-void SCA_JoystickManager::RegisterSensor(SCA_ISensor* sensor)
-{
-       m_sensors.push_back(sensor);
-}
-
-
 SCA_Joystick *SCA_JoystickManager::GetJoystickDevice()
 {
        /* 
index 3c4df64677ec5f1512f0f1bb93975992dd212d74..f2bb27965fa660ff825ca621a8a0e49e2b93c4f9 100644 (file)
@@ -45,7 +45,6 @@ public:
        SCA_JoystickManager(class SCA_LogicManager* logicmgr);
        virtual ~SCA_JoystickManager();
        virtual void NextFrame(double curtime,double deltatime);
-       virtual void    RegisterSensor(SCA_ISensor* sensor);
        SCA_Joystick* GetJoystickDevice(void);
 
 };
index 259b06134d7b965628c8e079129ee9cd1c672a97..6a96442b124337be7bd620678a9fd50a5e6641a4 100644 (file)
@@ -62,23 +62,14 @@ void SCA_KeyboardManager::NextFrame()
 {
        //const SCA_InputEvent& event = GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)=0;
   //   cerr << "SCA_KeyboardManager::NextFrame"<< endl;
-       for (unsigned int i=0;i<m_sensors.size();i++)
+       set<SCA_ISensor*>::iterator it;
+       for (it=m_sensors.begin(); it != m_sensors.end(); it++)
        {
-               SCA_KeyboardSensor* keysensor = (SCA_KeyboardSensor*)m_sensors[i];
-               keysensor->Activate(m_logicmanager,NULL);
+               (*it)->Activate(m_logicmanager,NULL);
        }
 
 }
 
-
-
-void  SCA_KeyboardManager::RegisterSensor(SCA_ISensor* keysensor)
-{
-       m_sensors.push_back(keysensor);
-}
-
-
-
 bool SCA_KeyboardManager::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
 {
        return false;
index b4a50f560257abdc8122591ca01ba8f7f8e63b41..8f3cc0ab715c0a085d4af3883f69dd3d4d56934b 100644 (file)
@@ -55,7 +55,6 @@ public:
        bool                    IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode);
        
        virtual void    NextFrame();    
-       virtual void    RegisterSensor(class SCA_ISensor* sensor);
        SCA_IInputDevice* GetInputDevice();
 };
 
index d1c5917f0ce922b7dcd60714bf0f7aded0bc8dbb..91e66aea35941e29977fa2fb6ccd9172f533c9c6 100644 (file)
@@ -176,12 +176,7 @@ void SCA_LogicManager::RemoveSensor(SCA_ISensor* sensor)
                (*c)->UnlinkSensor(sensor);
        }
     m_sensorcontrollermapje.erase(sensor);
-       
-       for (vector<SCA_EventManager*>::const_iterator ie=m_eventmanagers.begin();
-       !(ie==m_eventmanagers.end());ie++)
-       {
-               (*ie)->RemoveSensor(sensor);
-       }
+       sensor->UnregisterToManager();
 }
 
 void SCA_LogicManager::RemoveController(SCA_IController* controller)
index b4251d8ab53f019870552672cc8cf311de140d61..ca875dad07c833a26a924582b3f194a637c6d83f 100644 (file)
@@ -75,9 +75,10 @@ void SCA_MouseManager::NextFrame()
 {
        if (m_mousedevice)
        {
-               for (unsigned int i = 0; i < m_sensors.size(); i++)
+               set<SCA_ISensor*>::iterator it;
+               for (it=m_sensors.begin(); it!=m_sensors.end(); it++)
                {
-                       SCA_MouseSensor* mousesensor = (SCA_MouseSensor*) m_sensors[i];
+                       SCA_MouseSensor* mousesensor = (SCA_MouseSensor*)(*it);
                        // (0,0) is the Upper Left corner in our local window
                        // coordinates
                        if (!mousesensor->IsSuspended())
@@ -98,15 +99,6 @@ void SCA_MouseManager::NextFrame()
        }
 }
 
-
-
-void  SCA_MouseManager::RegisterSensor(SCA_ISensor* keysensor)
-{
-       m_sensors.push_back(keysensor);
-}
-
-
-
 bool SCA_MouseManager::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
 {
        /* We should guard for non-mouse events maybe? A rather silly side       */
index bc8254486add4042967c271d3a608c0c245018ac..efa4c639ce7017907c66be30a1dd8a7353be553f 100644 (file)
@@ -62,7 +62,6 @@ public:
         */
        bool IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode);
        virtual void    NextFrame();    
-       virtual void    RegisterSensor(class SCA_ISensor* sensor);
        SCA_IInputDevice* GetInputDevice();
 };
 
index fc56d1017282663f747f9d1ab2ab62709b713cf0..e5e3f9cced5756265066ec7dcbb275469a3954ba 100644 (file)
@@ -47,19 +47,10 @@ SCA_PropertyEventManager::~SCA_PropertyEventManager()
 
 }
 
-
-
-void SCA_PropertyEventManager::RegisterSensor(SCA_ISensor* sensor)
-{
-       m_sensors.push_back(sensor);
-}
-
-
-
 void SCA_PropertyEventManager::NextFrame()
 {
        // check for changed properties
-       for (vector<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++)
+       for (set<SCA_ISensor*>::const_iterator it = m_sensors.begin();!(it==m_sensors.end());it++)
        {
                (*it)->Activate(m_logicmgr,NULL);
        }
index aaa303a52c80e7d41417175be452db7910797396..f166065b198b62f7ef3c21ae47f9e96f1f1d688a 100644 (file)
@@ -43,7 +43,6 @@ public:
        SCA_PropertyEventManager(class SCA_LogicManager* logicmgr);
        virtual ~SCA_PropertyEventManager();
        virtual void NextFrame();
-       virtual void    RegisterSensor(SCA_ISensor* sensor);
        //SCA_LogicManager* GetLogicManager() { return m_logicmgr;}
 };
 
index 02020a52a17129590b2c9db9d2d4f0ab31993210..156478d866d577ac2027760d2bfaa507bccd1548 100644 (file)
@@ -50,16 +50,9 @@ SCA_RandomEventManager::SCA_RandomEventManager(class SCA_LogicManager* logicmgr)
 
 void SCA_RandomEventManager::NextFrame()
 {
-       for (vector<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++)
+       for (set<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++)
        {
-               SCA_ISensor *sensor = *i;
-               sensor->Activate(m_logicmgr, NULL);
+               (*i)->Activate(m_logicmgr, NULL);
        }
 }
 
-
-
-void   SCA_RandomEventManager::RegisterSensor(SCA_ISensor* sensor)
-{
-       m_sensors.push_back(sensor);
-};
index 8c75ef665fa02f604a2ea4c5bee57844aeebb814..79138c23c628089053efd577bf9b61d2ccda1bbf 100644 (file)
@@ -45,7 +45,6 @@ public:
        SCA_RandomEventManager(class SCA_LogicManager* logicmgr);
 
        virtual void NextFrame();
-       virtual void    RegisterSensor(SCA_ISensor* sensor);
 };
 
 #endif //__KX_RANDOMEVENTMGR
index 643f1247a52ed5f997bd1a7cb5e039be9d7eadfa..b7fadd3d62cdd600fba78745bbf2220d03746e80 100644 (file)
@@ -65,6 +65,11 @@ void SCA_TimeEventManager::RegisterSensor(SCA_ISensor* sensor)
        // not yet
 }
 
+void SCA_TimeEventManager::RemoveSensor(SCA_ISensor* sensor)
+{
+       // empty
+}
+
 
 
 void SCA_TimeEventManager::NextFrame(double curtime, double fixedtime)
index 2fd39661a2db494219be2b35d36ad2a132e9ab1e..bd57e12eb4474dd0a7eafcc31a4e8d1483b9d166 100644 (file)
@@ -45,6 +45,7 @@ public:
 
        virtual void    NextFrame(double curtime, double fixedtime);
        virtual void    RegisterSensor(class SCA_ISensor* sensor);
+       virtual void    RemoveSensor(class SCA_ISensor* sensor);
        void                    AddTimeProperty(CValue* timeval);
        void                    RemoveTimeProperty(CValue* timeval);
 };
index 7b5b7fdf78c74930680926ef1e25064bec073937..eee8e9f6827106ca4361c6fbbdb84f95ca9a1685 100644 (file)
@@ -55,28 +55,13 @@ KX_NetworkEventManager::~KX_NetworkEventManager()
        //printf("KX_NetworkEventManager destructor\n");
 }
 
-void KX_NetworkEventManager::RegisterSensor(class SCA_ISensor* sensor)
-{
-       //printf("KX_NetworkEventManager RegisterSensor\n");
-       m_sensors.push_back(sensor);
-}
-
-void KX_NetworkEventManager::RemoveSensor(class SCA_ISensor* sensor)
-{
-       //printf("KX_NetworkEventManager RemoveSensor\n");
-       // Network specific RemoveSensor stuff goes here
-
-       // parent
-       SCA_EventManager::RemoveSensor(sensor);
-}
-
 void KX_NetworkEventManager::NextFrame()
 {
 // printf("KX_NetworkEventManager::proceed %.2f - %.2f\n", curtime, deltatime);
        // each frame, the logicmanager will call the network
        // eventmanager to look for network events, and process it's
        // 'network' sensors
-       vector<class SCA_ISensor*>::iterator it;
+       set<class SCA_ISensor*>::iterator it;
 
        for (it = m_sensors.begin(); !(it==m_sensors.end()); it++) {
 //         printf("KX_NetworkEventManager::proceed sensor %.2f\n", curtime);
index 0b097ba2ef6323797ac465aed436563e3c5f18fa..ae88f1d4987fb5e0f1e804052d8f80b58ff3e9c4 100644 (file)
@@ -42,9 +42,6 @@ public:
                               class NG_NetworkDeviceInterface *ndi);
        virtual ~KX_NetworkEventManager ();
 
-       virtual void RegisterSensor(class SCA_ISensor* sensor);
-       virtual void RemoveSensor(class SCA_ISensor* sensor);
-
        virtual void NextFrame();
        virtual void EndFrame();
 
index df478be5ca21e563b78b151a4c9cd2cd560e8f94..02d1ad1b12bf6be7e97fbbea281196de82bdf546 100644 (file)
@@ -275,6 +275,7 @@ void KX_GameObject::ProcessReplica(KX_GameObject* replica)
        replica->m_pSGNode = NULL;
        replica->m_pClient_info = new KX_ClientObjectInfo(*m_pClient_info);
        replica->m_pClient_info->m_gameobject = replica;
+       replica->m_state = 0;
 }
 
 
index 89699d80031fc85894da93e49c5306a53e790279..140dd37f5c6c54cbcb3176a27d4ad84fe1419b65 100644 (file)
@@ -98,6 +98,14 @@ void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
        }
 }
 
+void KX_NearSensor::UnregisterSumo(KX_TouchEventManager* touchman)
+{
+       if (m_physCtrl)
+       {
+               touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
+       }
+}
+
 CValue* KX_NearSensor::GetReplica()
 {
        KX_NearSensor* replica = new KX_NearSensor(*this);
index c6724caccc332ef254c335ce26f76dd875238119..3f7078ef9fd5329ceb7350a7718755c000db4e01 100644 (file)
@@ -77,6 +77,7 @@ public:
                                                 const PHY_CollData * coll_data); 
        virtual bool    BroadPhaseFilterCollision(void*obj1,void*obj2);
        virtual void RegisterSumo(KX_TouchEventManager *touchman);
+       virtual void UnregisterSumo(KX_TouchEventManager* touchman);
        
        virtual PyObject* _getattr(const STR_String& attr);
 
index 4101c6b547e2b626522ad3e089bac1d4736fe5e4..1af29151adf654f11a1f462ac719f87c964ddf8f 100644 (file)
@@ -44,14 +44,9 @@ using namespace std;
 
 void KX_RayEventManager::NextFrame()
 {
-       for (vector<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++)
+       for (set<class SCA_ISensor*>::const_iterator i= m_sensors.begin();!(i==m_sensors.end());i++)
        {
-               SCA_ISensor *sensor = *i;
-               sensor->Activate(m_logicmgr, NULL);
+               (*i)->Activate(m_logicmgr, NULL);
        }
 }
 
-void   KX_RayEventManager::RegisterSensor(SCA_ISensor* sensor)
-{
-       m_sensors.push_back(sensor);
-};
index 3630f9682b94123f68c880c16f8f650432f978df..b816d4d5250fd272396607d6ac4d63f52e31d34f 100644 (file)
@@ -45,7 +45,6 @@ public:
                  m_logicmgr(logicmgr)
        {}
        virtual void NextFrame();
-       virtual void    RegisterSensor(SCA_ISensor* sensor);
 };
 
 #endif //__KX_RAYEVENTMGR
index 423543eef5cc07ab4a8c93cfd191364c7241a659..7528fdbbc34995db31a13d7f06ba377548dab92a 100644 (file)
@@ -100,16 +100,24 @@ bool       KX_TouchEventManager::newBroadphaseResponse(void *client_data,
 void KX_TouchEventManager::RegisterSensor(SCA_ISensor* sensor)
 {
        KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
-       m_sensors.push_back(touchsensor);
+       m_sensors.insert(touchsensor);
 
        touchsensor->RegisterSumo(this);
 }
 
+void KX_TouchEventManager::RemoveSensor(SCA_ISensor* sensor)
+{
+       KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor);
+       m_sensors.erase(touchsensor);
+
+       touchsensor->UnregisterSumo(this);
+}
+
 
 
 void KX_TouchEventManager::EndFrame()
 {
-       vector<SCA_ISensor*>::iterator it;
+       set<SCA_ISensor*>::iterator it;
        for ( it = m_sensors.begin();
        !(it==m_sensors.end());it++)
        {
@@ -124,7 +132,7 @@ void KX_TouchEventManager::NextFrame()
 {
        if (m_sensors.size() > 0)
        {
-               vector<SCA_ISensor*>::iterator it;
+               set<SCA_ISensor*>::iterator it;
                
                for (it = m_sensors.begin();!(it==m_sensors.end());++it)
                        static_cast<KX_TouchSensor*>(*it)->SynchronizeTransform();
@@ -157,20 +165,3 @@ void KX_TouchEventManager::NextFrame()
                        (*it)->Activate(m_logicmgr,NULL);
        }
 }
-
-
-
-void KX_TouchEventManager::RemoveSensor(class SCA_ISensor* sensor)
-{
-       std::vector<SCA_ISensor*>::iterator i =
-       std::find(m_sensors.begin(), m_sensors.end(), sensor);
-       if (!(i == m_sensors.end()))
-       {
-               std::swap(*i, m_sensors.back());
-               m_sensors.pop_back();
-       }
-       
-       // remove the sensor forever :)
-       SCA_EventManager::RemoveSensor(sensor);
-}
-
index 20ed6126bd03f76a3e19d6677180f565f50a9edc..cc77bccfc311a6db5212289d29cc3e78840ac1b9 100644 (file)
@@ -71,8 +71,8 @@ public:
                PHY_IPhysicsEnvironment* physEnv);
        virtual void NextFrame();
        virtual void    EndFrame();
-       virtual void    RemoveSensor(class SCA_ISensor* sensor);
        virtual void RegisterSensor(SCA_ISensor* sensor);
+       virtual void RemoveSensor(SCA_ISensor* sensor);
        SCA_LogicManager* GetLogicManager() { return m_logicmgr;}
        PHY_IPhysicsEnvironment *GetPhysicsEnvironment() { return m_physEnv; }
 
index ce3aa1de2ef63db400926eb2b2b0713707cbfa34..60e1d87d318c230bebb823d328a8c407c0337bc6 100644 (file)
@@ -153,6 +153,14 @@ void KX_TouchSensor::RegisterSumo(KX_TouchEventManager *touchman)
        }
 }
 
+void KX_TouchSensor::UnregisterSumo(KX_TouchEventManager* touchman)
+{
+       if (m_physCtrl)
+       {
+               touchman->GetPhysicsEnvironment()->removeCollisionCallback(m_physCtrl);
+       }
+}
+
 bool   KX_TouchSensor::NewHandleCollision(void*object1,void*object2,const PHY_CollData* colldata)
 {
 //     KX_TouchEventManager* toucheventmgr = (KX_TouchEventManager*)m_eventmgr;
index 056440ccd6c289b25e2bc50c992151ddaf5a858f..b611d296939fc29d0e68aa81b98d990d8a076c91 100644 (file)
@@ -76,6 +76,7 @@ public:
        virtual void ReParent(SCA_IObject* parent);
        
        virtual void RegisterSumo(KX_TouchEventManager* touchman);
+       virtual void UnregisterSumo(KX_TouchEventManager* touchman);
 
 //     virtual DT_Bool HandleCollision(void* obj1,void* obj2,
 //                                              const DT_CollData * coll_data); 
index ec1b7702ffda2bb15acdfafd064722eac1a3961f..7c61902f8e2b09733270376077e7e56e8f198c6c 100644 (file)
@@ -65,6 +65,7 @@ public:
        {
        }
        virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl) {}
+       virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl) {}
        virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) {return 0;}
        virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight) { return 0;}
 
index b773f40650b5c07097c151c91f6b9bb84fa5341d..ea14c5430e25ef62c3c51dc472603cc1b7f26430 100644 (file)
@@ -368,7 +368,7 @@ void        CcdPhysicsEnvironment::addCcdPhysicsController(CcdPhysicsController* ctrl)
        body->setUserPointer(ctrl);
 
        body->setGravity( m_gravity );
-       m_controllers.push_back(ctrl);
+       m_controllers.insert(ctrl);
 
        //use explicit group/filter for finer control over collision in bullet => near/radar sensor
        m_dynamicsWorld->addRigidBody(body, ctrl->GetCollisionFilterGroup(), ctrl->GetCollisionFilterMask());
@@ -434,36 +434,13 @@ void      CcdPhysicsEnvironment::addCcdPhysicsController(CcdPhysicsController* ctrl)
 
 void   CcdPhysicsEnvironment::removeCcdPhysicsController(CcdPhysicsController* ctrl)
 {
-
        //also remove constraint
 
-       
-
        m_dynamicsWorld->removeRigidBody(ctrl->GetRigidBody());
-
-
-       {
-               std::vector<CcdPhysicsController*>::iterator i =
-                       std::find(m_controllers.begin(), m_controllers.end(), ctrl);
-               if (!(i == m_controllers.end()))
-               {
-                       std::swap(*i, m_controllers.back());
-                       m_controllers.pop_back();
-               }
-       }
+       m_controllers.erase(ctrl);
 
        //remove it from the triggers
-       {
-               std::vector<CcdPhysicsController*>::iterator i =
-                       std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl);
-               if (!(i == m_triggerControllers.end()))
-               {
-                       std::swap(*i, m_triggerControllers.back());
-                       m_triggerControllers.pop_back();
-               }
-       }
-
-
+       m_triggerControllers.erase(ctrl);
 }
 
 void   CcdPhysicsEnvironment::updateCcdPhysicsController(CcdPhysicsController* ctrl, btScalar newMass, int newCollisionFlags, short int newCollisionGroup, short int newCollisionMask)
@@ -487,11 +464,10 @@ void      CcdPhysicsEnvironment::updateCcdPhysicsController(CcdPhysicsController* ctr
 
 void CcdPhysicsEnvironment::enableCcdPhysicsController(CcdPhysicsController* ctrl)
 {
-       std::vector<CcdPhysicsController*>::iterator i =
-               std::find(m_controllers.begin(), m_controllers.end(), ctrl);
-       if (i == m_controllers.end())
+       if (m_controllers.insert(ctrl).second)
        {
                btRigidBody* body = ctrl->GetRigidBody();
+               body->setUserPointer(ctrl);
                m_dynamicsWorld->addCollisionObject(body, 
                        ctrl->GetCollisionFilterGroup(), ctrl->GetCollisionFilterMask());
        }
@@ -507,12 +483,12 @@ void      CcdPhysicsEnvironment::beginFrame()
 
 bool   CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep)
 {
+       std::set<CcdPhysicsController*>::iterator it;
+       int i;
 
-       int i,numCtrl = GetNumControllers();
-       for (i=0;i<numCtrl;i++)
+       for (it=m_controllers.begin(); it!=m_controllers.end(); it++)
        {
-               CcdPhysicsController* ctrl = GetPhysicsController(i);
-               ctrl->SynchronizeMotionStates(timeStep);
+               (*it)->SynchronizeMotionStates(timeStep);
        }
 
        float subStep = timeStep / float(m_numTimeSubSteps);
@@ -521,11 +497,9 @@ bool       CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep)
                m_dynamicsWorld->stepSimulation(subStep,0);//perform always a full simulation step
        }
 
-       numCtrl = GetNumControllers();
-       for (i=0;i<numCtrl;i++)
+       for (it=m_controllers.begin(); it!=m_controllers.end(); it++)
        {
-               CcdPhysicsController* ctrl = GetPhysicsController(i);
-               ctrl->SynchronizeMotionStates(timeStep);
+               (*it)->SynchronizeMotionStates(timeStep);
        }
 
        for (i=0;i<m_wrapperVehicles.size();i++)
@@ -852,20 +826,6 @@ CcdPhysicsEnvironment::~CcdPhysicsEnvironment()
 }
 
 
-int    CcdPhysicsEnvironment::GetNumControllers()
-{
-       return m_controllers.size();
-}
-
-
-CcdPhysicsController* CcdPhysicsEnvironment::GetPhysicsController( int index)
-{
-       return m_controllers[index];
-}
-
-
-
-
 void   CcdPhysicsEnvironment::setConstraintParam(int constraintId,int param,float value0,float value1)
 {
        btTypedConstraint* typedConstraint = getConstraintById(constraintId);
@@ -905,12 +865,14 @@ void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
 {
 
        CcdPhysicsController* ctrl1 = (CcdPhysicsController* )ctrl;
-       std::vector<CcdPhysicsController*>::iterator i =
-               std::find(m_controllers.begin(), m_controllers.end(), ctrl);
-       if ((i == m_controllers.end()))
-       {
-               addCcdPhysicsController(ctrl1);
-       }
+       // addSensor() is a "light" function for bullet because it is used
+       // dynamically when the sensor is activated. Use enableCcdPhysicsController() instead 
+       //if (m_controllers.insert(ctrl1).second)
+       //{
+       //      addCcdPhysicsController(ctrl1);
+       //}
+       enableCcdPhysicsController(ctrl1);
+
        //Collision filter/mask is now set at the time of the creation of the controller 
        //force collision detection with everything, including static objects (might hurt performance!)
        //ctrl1->GetRigidBody()->getBroadphaseHandle()->m_collisionFilterMask = btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::SensorTrigger;
@@ -923,21 +885,15 @@ void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
 
 void CcdPhysicsEnvironment::removeCollisionCallback(PHY_IPhysicsController* ctrl)
 {
-       std::vector<CcdPhysicsController*>::iterator i =
-               std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl);
-       if (!(i == m_triggerControllers.end()))
-       {
-               std::swap(*i, m_triggerControllers.back());
-               m_triggerControllers.pop_back();
-       }
+       m_triggerControllers.erase((CcdPhysicsController*)ctrl);
 }
 
 
 void CcdPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl)
 {
-       removeCollisionCallback(ctrl);
-       //printf("removeSensor\n");
+       removeCcdPhysicsController((CcdPhysicsController*)ctrl);
 }
+
 void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)
 {
        /*      printf("addTouchCallback\n(response class = %i)\n",response_class);
@@ -975,10 +931,9 @@ void CcdPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctr
        CcdPhysicsController* ccdCtrl = static_cast<CcdPhysicsController*>(ctrl);
 
        //printf("requestCollisionCallback\n");
-       m_triggerControllers.push_back(ccdCtrl);
+       m_triggerControllers.insert(ccdCtrl);
 }
 
-
 void   CcdPhysicsEnvironment::CallbackTriggers()
 {
        
@@ -1011,11 +966,10 @@ void     CcdPhysicsEnvironment::CallbackTriggers()
                                CcdPhysicsController* ctrl0 = static_cast<CcdPhysicsController*>(obj0->getUserPointer());
                                CcdPhysicsController* ctrl1 = static_cast<CcdPhysicsController*>(obj1->getUserPointer());
 
-                               std::vector<CcdPhysicsController*>::iterator i =
-                                       std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl0);
+                               std::set<CcdPhysicsController*>::const_iterator i = m_triggerControllers.find(ctrl0);
                                if (i == m_triggerControllers.end())
                                {
-                                       i = std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl1);
+                                       i = m_triggerControllers.find(ctrl1);
                                }
 
                                if (!(i == m_triggerControllers.end()))
@@ -1125,7 +1079,6 @@ PHY_IPhysicsController*   CcdPhysicsEnvironment::CreateSphereController(float radi
 
        CcdPhysicsController* sphereController = new CcdPhysicsController(cinfo);
        
-
        return sphereController;
 }
 
index 453749b27b38d60d7617080b2651340b30f10c1d..fd96522037e0e7128806cc63fe2886d44fcbc851 100644 (file)
@@ -18,6 +18,7 @@ subject to the following restrictions:
 
 #include "PHY_IPhysicsEnvironment.h"
 #include <vector>
+#include <set>
 class CcdPhysicsController;
 #include "LinearMath/btVector3.h"
 #include "LinearMath/btTransform.h"
@@ -209,12 +210,6 @@ protected:
                }
 
        
-               int     GetNumControllers();
-
-               CcdPhysicsController* GetPhysicsController( int index);
-
-               
-
                const btPersistentManifold*     GetManifold(int index) const;
 
        
@@ -229,9 +224,9 @@ protected:
                
 
                
-               std::vector<CcdPhysicsController*> m_controllers;
+               std::set<CcdPhysicsController*> m_controllers;
                
-               std::vector<CcdPhysicsController*> m_triggerControllers;
+               std::set<CcdPhysicsController*> m_triggerControllers;
 
                PHY_ResponseCallback    m_triggerCallbacks[PHY_NUM_RESPONSE];
                void*                   m_triggerCallbacksUserPtrs[PHY_NUM_RESPONSE];
index f0761618e4ec8a91cd1300947a58109aa49ac96a..b5a61f72e4aa0cb179d4e1054d9e39e34330854e 100644 (file)
@@ -80,6 +80,7 @@ public:
                {
                }
                virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl) {}
+               virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl) {}
                virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) {return 0;}
                virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight) { return 0;}
 
index b5bf67b14eaf98186aa5213fede3410621e40f77..f0791bbf89ffe666771622ed5da6d2f55f60c017 100644 (file)
@@ -98,12 +98,17 @@ void SM_Scene::addTouchCallback(int response_class, DT_ResponseCallback callback
 
 void SM_Scene::addSensor(SM_Object& object) 
 {
-       object.calcXform();
-       m_objectList.push_back(&object);
-       DT_AddObject(m_scene, object.getObjectHandle());
-       DT_SetResponseClass(m_respTable, object.getObjectHandle(), m_ResponseClass[SENSOR_RESPONSE]);
-       DT_SetResponseClass(m_secondaryRespTable, object.getObjectHandle(), m_secondaryResponseClass[SENSOR_RESPONSE]);
-       DT_SetResponseClass(m_fixRespTable, object.getObjectHandle(), m_fixResponseClass[SENSOR_RESPONSE]);
+       T_ObjectList::iterator i =
+               std::find(m_objectList.begin(), m_objectList.end(), &object);
+       if (i == m_objectList.end())
+       {
+               object.calcXform();
+               m_objectList.push_back(&object);
+               DT_AddObject(m_scene, object.getObjectHandle());
+               DT_SetResponseClass(m_respTable, object.getObjectHandle(), m_ResponseClass[SENSOR_RESPONSE]);
+               DT_SetResponseClass(m_secondaryRespTable, object.getObjectHandle(), m_secondaryResponseClass    [SENSOR_RESPONSE]);
+               DT_SetResponseClass(m_fixRespTable, object.getObjectHandle(), m_fixResponseClass[SENSOR_RESPONSE]);
+       }
 }
 
 void SM_Scene::add(SM_Object& object) {
index 16ba45a0be5389509875d3a7182c67da4a727bad..65018d2523ebc1f472b77382508996202c4e6388 100644 (file)
@@ -228,6 +228,12 @@ void SumoPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ct
                m_sumoScene->requestCollisionCallback(*smObject);
        }
 }
+
+void SumoPhysicsEnvironment::removeCollisionCallback(PHY_IPhysicsController* ctrl)
+{
+       // intentionally empty
+}
+
 PHY_IPhysicsController*        SumoPhysicsEnvironment::CreateSphereController(float radius,const PHY__Vector3& position)
 {
        DT_ShapeHandle shape    =       DT_NewSphere(0.0);
index ce5cd70e8cc0c21e9cd28a0ecd425d8a175127d4..8b9fb4630345cc43fba493a5366f41e8b56f1217 100644 (file)
@@ -84,6 +84,7 @@ public:
        virtual void removeSensor(PHY_IPhysicsController* ctrl);
        virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user);
        virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl);
+       virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl);
        virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position);
        virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight);
 
index c148210903f670af93a9be5eb7ce427631784713..5b2750666657a3e1531966d6bb8142a70f2b2702 100644 (file)
@@ -104,6 +104,7 @@ class PHY_IPhysicsEnvironment
                virtual void removeSensor(PHY_IPhysicsController* ctrl)=0;
                virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)=0;
                virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl)=0;
+               virtual void removeCollisionCallback(PHY_IPhysicsController* ctrl)=0;
                //These two methods are *solely* used to create controllers for sensor! Don't use for anything else
                virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) =0;
                virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight)=0;