svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / Ketsji / KX_NearSensor.cpp
index 0489b7090e9dfa4aee642b0c702b85c3bbdcfd5c..1a211a64b35ff87ed683c0b8e994233902475c97 100644 (file)
@@ -36,6 +36,7 @@
 #include "KX_Scene.h" // needed to create a replica
 #include "PHY_IPhysicsEnvironment.h"
 #include "PHY_IPhysicsController.h"
+#include "PHY_IMotionState.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -47,22 +48,20 @@ KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
                                                         bool bFindMaterial,
                                                         const STR_String& touchedpropname,
                                                         class KX_Scene* scene,
-                                                        PHY_IPhysicsController*        ctrl,
-                                                        PyTypeObject* T)
+                                                        PHY_IPhysicsController* ctrl)
                         :KX_TouchSensor(eventmgr,
                                                         gameobj,
                                                         bFindMaterial,
                                                         false,
-                                                        touchedpropname,
-                                                        /* scene, */
-                                                        T),
+                                                        touchedpropname
+                                                        /*, scene */),
                         m_Margin(margin),
                         m_ResetMargin(resetmargin)
 
 {
 
        gameobj->getClientInfo()->m_sensors.remove(this);
-       m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
+       m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::SENSOR);
        m_client_info->m_sensors.push_back(this);
        
        //DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
@@ -81,81 +80,50 @@ void KX_NearSensor::SynchronizeTransform()
        // not linked to the parent object, must synchronize it.
        if (m_physCtrl)
        {
+               PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
                KX_GameObject* parent = ((KX_GameObject*)GetParent());
-               MT_Vector3 pos = parent->NodeGetWorldPosition();
-               MT_Quaternion orn = parent->NodeGetWorldOrientation().getRotation();
-               m_physCtrl->setPosition(pos.x(),pos.y(),pos.z());
-               m_physCtrl->setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
-               m_physCtrl->calcXform();
+               const MT_Point3& pos = parent->NodeGetWorldPosition();
+               float ori[12];
+               parent->NodeGetWorldOrientation().getValue(ori);
+               motionState->setWorldPosition(pos[0], pos[1], pos[2]);
+               motionState->setWorldOrientation(ori);
+               m_physCtrl->WriteMotionStateToDynamics(true);
        }
 }
 
-void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
+CValue* KX_NearSensor::GetReplica()
 {
-       if (m_physCtrl)
-       {
-               touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
-       }
+       KX_NearSensor* replica = new KX_NearSensor(*this);
+       replica->ProcessReplica();
+       return replica;
 }
 
-void KX_NearSensor::UnregisterSumo(KX_TouchEventManager* touchman)
+void KX_NearSensor::ProcessReplica()
 {
-       if (m_physCtrl)
-       {
-               touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
-       }
-}
-
-CValue* KX_NearSensor::GetReplica()
-{
-       KX_NearSensor* replica = new KX_NearSensor(*this);
-       replica->m_colliders = new CListValue();
-       replica->Init();
-       // this will copy properties and so on...
-       CValue::AddDataToReplica(replica);
+       KX_TouchSensor::ProcessReplica();
        
-       replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
+       m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::SENSOR);
        
-       if (replica->m_physCtrl)
+       if (m_physCtrl)
        {
-               replica->m_physCtrl = replica->m_physCtrl->GetReplica();
-               if (replica->m_physCtrl)
+               m_physCtrl = m_physCtrl->GetReplica();
+               if (m_physCtrl)
                {
                        //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
-                       replica->m_physCtrl->SetMargin(m_Margin);
-                       replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
+                       m_physCtrl->SetMargin(m_Margin);
+                       m_physCtrl->setNewClientInfo(m_client_info);
                }
                
        }
-       //Wrong: the parent object could be a child, this code works only if it is a root parent.
-       //Anyway, at this stage, the parent object is already synchronized, nothing to do.
-       //bool parentUpdated = false;
-       //((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL, parentUpdated);
-       replica->SynchronizeTransform();
-       
-       return replica;
 }
 
-
-
 void KX_NearSensor::ReParent(SCA_IObject* parent)
 {
+       SCA_ISensor::ReParent(parent);
        m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
        m_client_info->m_sensors.push_back(this);
-       
-
-/*     KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
-       client_info->m_gameobject = gameobj;
-       client_info->m_auxilary_info = NULL;
-       
-       client_info->m_sensors.push_back(this);
-       SCA_ISensor::ReParent(parent);
-*/
-       //Not needed, was done in GetReplica() already
-       //bool parentUpdated = false;
-       //((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL,parentUpdated);
-       //SynchronizeTransform();
-       SCA_ISensor::ReParent(parent);
+       //Synchronize here with the actual parent.
+       SynchronizeTransform();
 }
 
 
@@ -177,7 +145,7 @@ KX_NearSensor::~KX_NearSensor()
 }
 
 
-bool KX_NearSensor::Evaluate(CValue* event)
+bool KX_NearSensor::Evaluate()
 {
        bool result = false;
 //     KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
@@ -286,8 +254,13 @@ bool       KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData
 /* ------------------------------------------------------------------------- */
 
 PyTypeObject KX_NearSensor::Type = {
-       PyObject_HEAD_INIT(NULL)
-       0,
+#if (PY_VERSION_HEX >= 0x02060000)
+       PyVarObject_HEAD_INIT(NULL, 0)
+#else
+       /* python 2.5 and below */
+       PyObject_HEAD_INIT( NULL )  /* required py macro */
+       0,                          /* ob_size */
+#endif
        "KX_NearSensor",
        sizeof(PyObjectPlus_Proxy),
        0,
@@ -297,26 +270,17 @@ PyTypeObject KX_NearSensor::Type = {
        0,
        0,
        py_base_repr,
-       0,0,0,0,0,0,
-       py_base_getattro,
-       py_base_setattro,
        0,0,0,0,0,0,0,0,0,
-       Methods
-};
-
-
-
-PyParentObject KX_NearSensor::Parents[] = {
-       &KX_NearSensor::Type,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
        &KX_TouchSensor::Type,
-       &SCA_ISensor::Type,
-       &SCA_ILogicBrick::Type,
-       &CValue::Type,
-       NULL
+       0,0,0,0,0,0,
+       py_base_new
 };
 
-
-
 PyMethodDef KX_NearSensor::Methods[] = {
        //No methods
        {NULL,NULL} //Sentinel
@@ -327,14 +291,3 @@ PyAttributeDef KX_NearSensor::Attributes[] = {
        KX_PYATTRIBUTE_FLOAT_RW_CHECK("resetDistance", 0, 100, KX_NearSensor, m_ResetMargin, CheckResetDistance),
        {NULL} //Sentinel
 };
-
-
-PyObject* KX_NearSensor::py_getattro(PyObject *attr)
-{
-       py_getattro_up(KX_TouchSensor);
-}
-
-int KX_NearSensor::py_setattro(PyObject*attr, PyObject* value)
-{
-       py_setattro_up(KX_TouchSensor);
-}