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