Blender V2.61 - r43446

KX_RadarSensor.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "KX_RadarSensor.h"
00034 #include "KX_GameObject.h"
00035 #include "KX_PyMath.h"
00036 #include "PHY_IPhysicsController.h"
00037 #include "PHY_IMotionState.h"
00038 #include "DNA_sensor_types.h"
00039 
00043 KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
00044         KX_GameObject* gameobj,
00045         PHY_IPhysicsController* physCtrl,
00046             double coneradius,
00047             double coneheight,
00048             int axis,
00049             double margin,
00050             double resetmargin,
00051             bool bFindMaterial,
00052             const STR_String& touchedpropname)
00053 
00054             : KX_NearSensor(
00055                 eventmgr,
00056                 gameobj,
00057                 //DT_NewCone(coneradius,coneheight),
00058                 margin,
00059                 resetmargin,
00060                 bFindMaterial,
00061                 touchedpropname,
00062                 physCtrl),
00063 
00064                 m_coneradius(coneradius),
00065                 m_coneheight(coneheight),
00066                 m_axis(axis)
00067 {
00068     m_client_info->m_type = KX_ClientObjectInfo::SENSOR;
00069     //m_client_info->m_clientobject = gameobj;
00070     //m_client_info->m_auxilary_info = NULL;
00071     //sumoObj->setClientObject(&m_client_info);
00072 }
00073             
00074 KX_RadarSensor::~KX_RadarSensor()
00075 {
00076     
00077 }
00078 
00079 CValue* KX_RadarSensor::GetReplica()
00080 {
00081     KX_RadarSensor* replica = new KX_RadarSensor(*this);
00082     replica->ProcessReplica();
00083     return replica;
00084 }
00085 
00089 void KX_RadarSensor::SynchronizeTransform()
00090 {
00091     // Getting the parent location was commented out. Why?
00092     MT_Transform trans;
00093     trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
00094     trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
00095     // What is the default orientation? pointing in the -y direction?
00096     // is the geometry correctly converted?
00097 
00098     // a collision cone is oriented
00099     // center the cone correctly 
00100     // depends on the radar 'axis'
00101     switch (m_axis)
00102     {
00103     case SENS_RADAR_X_AXIS: // +X Axis
00104         {
00105             MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
00106             trans.rotate(rotquatje);
00107             trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
00108             break;
00109         };
00110     case SENS_RADAR_Y_AXIS: // +Y Axis
00111         {
00112             MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
00113             trans.rotate(rotquatje);
00114             trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
00115             break;
00116         };
00117     case SENS_RADAR_Z_AXIS: // +Z Axis
00118         {
00119             MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
00120             trans.rotate(rotquatje);
00121             trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
00122             break;
00123         };
00124     case SENS_RADAR_NEG_X_AXIS: // -X Axis
00125         {
00126             MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
00127             trans.rotate(rotquatje);
00128             trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
00129             break;
00130         };
00131     case SENS_RADAR_NEG_Y_AXIS: // -Y Axis
00132         {
00133             //MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
00134             //trans.rotate(rotquatje);
00135             trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
00136             break;
00137         };
00138     case SENS_RADAR_NEG_Z_AXIS: // -Z Axis
00139         {
00140             MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
00141             trans.rotate(rotquatje);
00142             trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
00143             break;
00144         };
00145     default:
00146         {
00147         }
00148     }
00149     
00150     //Using a temp variable to translate MT_Point3 to float[3].
00151     //float[3] works better for the Python interface.
00152     MT_Point3 temp = trans.getOrigin();
00153     m_cone_origin[0] = temp[0];
00154     m_cone_origin[1] = temp[1];
00155     m_cone_origin[2] = temp[2];
00156 
00157     temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
00158     m_cone_target[0] = temp[0];
00159     m_cone_target[1] = temp[1];
00160     m_cone_target[2] = temp[2];
00161 
00162 
00163     if (m_physCtrl)
00164     {
00165         PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
00166         const MT_Point3& pos = trans.getOrigin();
00167         float ori[12];
00168         trans.getBasis().getValue(ori);
00169         motionState->setWorldPosition(pos[0], pos[1], pos[2]);
00170         motionState->setWorldOrientation(ori);
00171         m_physCtrl->WriteMotionStateToDynamics(true);
00172     }
00173 
00174 }
00175 
00176 /* ------------------------------------------------------------------------- */
00177 /* Python Functions                                                          */
00178 /* ------------------------------------------------------------------------- */
00179 
00180 #ifdef WITH_PYTHON
00181 
00182 /* ------------------------------------------------------------------------- */
00183 /* Python Integration Hooks                                                  */
00184 /* ------------------------------------------------------------------------- */
00185 PyTypeObject KX_RadarSensor::Type = {
00186     PyVarObject_HEAD_INIT(NULL, 0)
00187     "KX_RadarSensor",
00188     sizeof(PyObjectPlus_Proxy),
00189     0,
00190     py_base_dealloc,
00191     0,
00192     0,
00193     0,
00194     0,
00195     py_base_repr,
00196     0,0,0,0,0,0,0,0,0,
00197     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00198     0,0,0,0,0,0,0,
00199     Methods,
00200     0,
00201     0,
00202     &KX_NearSensor::Type,
00203     0,0,0,0,0,0,
00204     py_base_new
00205 };
00206 
00207 PyMethodDef KX_RadarSensor::Methods[] = {
00208     {NULL} //Sentinel
00209 };
00210 
00211 PyAttributeDef KX_RadarSensor::Attributes[] = {
00212     KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
00213     KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
00214     KX_PYATTRIBUTE_FLOAT_RO("distance", KX_RadarSensor, m_coneheight),
00215     KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
00216     KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
00217     {NULL} //Sentinel
00218 };
00219 
00220 #endif // WITH_PYTHON