Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / gameengine / Ketsji / KX_RadarSensor.cpp
index 66ff1e1e7caf7cf61d4c9fe7f1647d32f92962bb..d020a2544d2ee0c79237642ff86ddae3d7949c6d 100644 (file)
@@ -28,7 +28,9 @@
 
 #include "KX_RadarSensor.h"
 #include "KX_GameObject.h"
+#include "KX_PyMath.h"
 #include "PHY_IPhysicsController.h"
+#include "PHY_IMotionState.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -46,9 +48,7 @@ KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
                        double margin,
                        double resetmargin,
                        bool bFindMaterial,
-                       const STR_String& touchedpropname,
-                       class KX_Scene* kxscene,
-                       PyTypeObject* T)
+                       const STR_String& touchedpropname)
 
                        : KX_NearSensor(
                                eventmgr,
@@ -58,20 +58,18 @@ KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
                                resetmargin,
                                bFindMaterial,
                                touchedpropname,
-                               kxscene,
-                               physCtrl,
-                               T),
+                               physCtrl),
+
                                m_coneradius(coneradius),
                                m_coneheight(coneheight),
                                m_axis(axis)
 {
-       m_client_info->m_type = KX_ClientObjectInfo::RADAR;
+       m_client_info->m_type = KX_ClientObjectInfo::SENSOR;
        //m_client_info->m_clientobject = gameobj;
        //m_client_info->m_auxilary_info = NULL;
        //sumoObj->setClientObject(&m_client_info);
 }
                        
-
 KX_RadarSensor::~KX_RadarSensor()
 {
        
@@ -80,33 +78,10 @@ KX_RadarSensor::~KX_RadarSensor()
 CValue* KX_RadarSensor::GetReplica()
 {
        KX_RadarSensor* replica = new KX_RadarSensor(*this);
-       replica->m_colliders = new CListValue();
-       replica->m_bCollision = false;
-       replica->m_bTriggered= false;
-       replica->m_hitObject = NULL;
-       replica->m_bLastTriggered = false;
-       // this will copy properties and so on...
-       CValue::AddDataToReplica(replica);
-       
-       replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::RADAR);
-       
-       if (replica->m_physCtrl)
-       {
-               replica->m_physCtrl = replica->m_physCtrl->GetReplica();
-       }
-
-       //todo: make sure replication works fine!
-       //>m_sumoObj = new SM_Object(DT_NewCone(m_coneradius, m_coneheight),NULL,NULL,NULL);
-       //replica->m_sumoObj->setMargin(m_Margin);
-       //replica->m_sumoObj->setClientObject(replica->m_client_info);
-       
-       ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
-       replica->SynchronizeTransform();
-       
+       replica->ProcessReplica();
        return replica;
 }
 
-
 /**
  *     Transforms the collision object. A cone is not correctly centered
  *     for usage.  */
@@ -124,133 +99,172 @@ void KX_RadarSensor::SynchronizeTransform()
        // depends on the radar 'axis'
        switch (m_axis)
        {
-       case 0: // X Axis
+       case 0: // +X Axis
                {
                        MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
                        trans.rotate(rotquatje);
                        trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
                        break;
                };
-       case 1: // Y Axis
+       case 1: // +Y Axis
                {
                        MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
                        trans.rotate(rotquatje);
                        trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
                        break;
                };
-       case 2: // Z Axis
+       case 2: // +Z Axis
                {
                        MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
                        trans.rotate(rotquatje);
                        trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
                        break;
                };
+       case 3: // -X Axis
+               {
+                       MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
+                       trans.rotate(rotquatje);
+                       trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
+                       break;
+               };
+       case 4: // -Y Axis
+               {
+                       //MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
+                       //trans.rotate(rotquatje);
+                       trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
+                       break;
+               };
+       case 5: // -Z Axis
+               {
+                       MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
+                       trans.rotate(rotquatje);
+                       trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
+                       break;
+               };
        default:
                {
                }
        }
-       m_cone_origin = trans.getOrigin();
-       m_cone_target = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
+       
+       //Using a temp variable to translate MT_Point3 to float[3].
+       //float[3] works better for the Python interface.
+       MT_Point3 temp = trans.getOrigin();
+       m_cone_origin[0] = temp[0];
+       m_cone_origin[1] = temp[1];
+       m_cone_origin[2] = temp[2];
+
+       temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
+       m_cone_target[0] = temp[0];
+       m_cone_target[1] = temp[1];
+       m_cone_target[2] = temp[2];
 
 
        if (m_physCtrl)
        {
-               m_physCtrl->setPosition(trans.getOrigin().x(),trans.getOrigin().y(),trans.getOrigin().z());
-               m_physCtrl->setOrientation(trans.getRotation().x(),trans.getRotation().y(),trans.getRotation().z(),trans.getRotation().w());
-               m_physCtrl->calcXform();
+               PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
+               const MT_Point3& pos = trans.getOrigin();
+               float ori[12];
+               trans.getBasis().getValue(ori);
+               motionState->setWorldPosition(pos[0], pos[1], pos[2]);
+               motionState->setWorldOrientation(ori);
+               m_physCtrl->WriteMotionStateToDynamics(true);
        }
 
 }
 
 /* ------------------------------------------------------------------------- */
-/* Python functions                                                          */
+/* Python Functions                                                                                                                     */
 /* ------------------------------------------------------------------------- */
 
-/* Integration hooks ------------------------------------------------------- */
-PyTypeObject KX_RadarSensor::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
-       0,
-       "KX_RadarSensor",
-       sizeof(KX_RadarSensor),
-       0,
-       PyDestructor,
-       0,
-       __getattr,
-       __setattr,
-       0, //&MyPyCompare,
-       __repr,
-       0, //&cvalue_as_number,
-       0,
-       0,
-       0,
-       0
-};
-
-PyParentObject KX_RadarSensor::Parents[] = {
-       &KX_RadarSensor::Type,
-       &KX_NearSensor::Type,
-       &KX_TouchSensor::Type,
-       &SCA_ISensor::Type,
-       &SCA_ILogicBrick::Type,
-       &CValue::Type,
-       NULL
-};
-
-PyMethodDef KX_RadarSensor::Methods[] = {
-       {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
-        METH_VARARGS, GetConeOrigin_doc},
-       {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
-        METH_VARARGS, GetConeTarget_doc},
-       {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
-        METH_VARARGS, GetConeHeight_doc},
-       {NULL,NULL,NULL,NULL} //Sentinel
-};
-
-PyObject* KX_RadarSensor::_getattr(const STR_String& attr) {
-       _getattr_up(KX_TouchSensor);
-}
-
+//Deprecated ----->
 /* getConeOrigin */
-char KX_RadarSensor::GetConeOrigin_doc[] = 
+const char KX_RadarSensor::GetConeOrigin_doc[] = 
 "getConeOrigin()\n"
 "\tReturns the origin of the cone with which to test. The origin\n"
 "\tis in the middle of the cone.";
-PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self, 
-                                                                                 PyObject* args, 
-                                                                                 PyObject* kwds) {
+PyObject* KX_RadarSensor::PyGetConeOrigin() {
+       ShowDeprecationWarning("getConeOrigin()", "the coneOrigin property");
+
        PyObject *retVal = PyList_New(3);
        
-       PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
-       PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
-       PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
+       PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
+       PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
+       PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
        
        return retVal;
 }
 
 /* getConeOrigin */
-char KX_RadarSensor::GetConeTarget_doc[] = 
+const char KX_RadarSensor::GetConeTarget_doc[] = 
 "getConeTarget()\n"
 "\tReturns the center of the bottom face of the cone with which to test.\n";
-PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self, 
-                                                                                 PyObject* args, 
-                                                                                 PyObject* kwds) {
+PyObject* KX_RadarSensor::PyGetConeTarget() {
+       ShowDeprecationWarning("getConeTarget()", "the coneTarget property");
+
        PyObject *retVal = PyList_New(3);
        
-       PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
-       PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
-       PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
+       PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
+       PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
+       PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
        
        return retVal;
 }
 
-/* getConeOrigin */
-char KX_RadarSensor::GetConeHeight_doc[] = 
+/* getConeHeight */
+const char KX_RadarSensor::GetConeHeight_doc[] = 
 "getConeHeight()\n"
 "\tReturns the height of the cone with which to test.\n";
-PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self, 
-                                                                                 PyObject* args, 
-                                                                                 PyObject* kwds) {
+PyObject* KX_RadarSensor::PyGetConeHeight() {
+                                                                                         
+       ShowDeprecationWarning("getConeHeight()", "the distance property");
+
        return PyFloat_FromDouble(m_coneheight);
 }
+//<----- Deprecated
+
+/* ------------------------------------------------------------------------- */
+/* Python Integration Hooks                                                  */
+/* ------------------------------------------------------------------------- */
+PyTypeObject KX_RadarSensor::Type = {
+       PyVarObject_HEAD_INIT(NULL, 0)
+       "KX_RadarSensor",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,0,0,0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &KX_NearSensor::Type,
+       0,0,0,0,0,0,
+       py_base_new
+};
 
+PyMethodDef KX_RadarSensor::Methods[] = {
+       //Deprecated ----->
+       {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
+        METH_VARARGS, (const char *)GetConeOrigin_doc},
+       {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
+        METH_VARARGS, (const char *)GetConeTarget_doc},
+       {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
+        METH_VARARGS, (const char *)GetConeHeight_doc},
+        //<-----
+       {NULL} //Sentinel
+};
+
+PyAttributeDef KX_RadarSensor::Attributes[] = {
+       KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
+       KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
+       KX_PYATTRIBUTE_FLOAT_RO("distance", KX_RadarSensor, m_coneheight),
+       KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
+       KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
+       {NULL} //Sentinel
+};