svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r21908:22075
[blender.git] / source / gameengine / Ketsji / KX_RadarSensor.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "KX_RadarSensor.h"
30 #include "KX_GameObject.h"
31 #include "KX_PyMath.h"
32 #include "PHY_IPhysicsController.h"
33 #include "PHY_IMotionState.h"
34
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
38
39 /**
40  *      RadarSensor constructor. Creates a near-sensor derived class, with a cone collision shape.
41  */
42 KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
43                 KX_GameObject* gameobj,
44                 PHY_IPhysicsController* physCtrl,
45                         double coneradius,
46                         double coneheight,
47                         int     axis,
48                         double margin,
49                         double resetmargin,
50                         bool bFindMaterial,
51                         const STR_String& touchedpropname,
52                         class KX_Scene* kxscene)
53
54                         : KX_NearSensor(
55                                 eventmgr,
56                                 gameobj,
57                                 //DT_NewCone(coneradius,coneheight),
58                                 margin,
59                                 resetmargin,
60                                 bFindMaterial,
61                                 touchedpropname,
62                                 kxscene,
63                                 physCtrl),
64
65                                 m_coneradius(coneradius),
66                                 m_coneheight(coneheight),
67                                 m_axis(axis)
68 {
69         m_client_info->m_type = KX_ClientObjectInfo::SENSOR;
70         //m_client_info->m_clientobject = gameobj;
71         //m_client_info->m_auxilary_info = NULL;
72         //sumoObj->setClientObject(&m_client_info);
73 }
74                         
75 KX_RadarSensor::~KX_RadarSensor()
76 {
77         
78 }
79
80 CValue* KX_RadarSensor::GetReplica()
81 {
82         KX_RadarSensor* replica = new KX_RadarSensor(*this);
83         replica->ProcessReplica();
84         return replica;
85 }
86
87 /**
88  *      Transforms the collision object. A cone is not correctly centered
89  *      for usage.  */
90 void KX_RadarSensor::SynchronizeTransform()
91 {
92         // Getting the parent location was commented out. Why?
93         MT_Transform trans;
94         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
95         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
96         // What is the default orientation? pointing in the -y direction?
97         // is the geometry correctly converted?
98
99         // a collision cone is oriented
100         // center the cone correctly 
101         // depends on the radar 'axis'
102         switch (m_axis)
103         {
104         case 0: // +X Axis
105                 {
106                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
107                         trans.rotate(rotquatje);
108                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
109                         break;
110                 };
111         case 1: // +Y Axis
112                 {
113                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
114                         trans.rotate(rotquatje);
115                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
116                         break;
117                 };
118         case 2: // +Z Axis
119                 {
120                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
121                         trans.rotate(rotquatje);
122                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
123                         break;
124                 };
125         case 3: // -X Axis
126                 {
127                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
128                         trans.rotate(rotquatje);
129                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
130                         break;
131                 };
132         case 4: // -Y Axis
133                 {
134                         //MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
135                         //trans.rotate(rotquatje);
136                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
137                         break;
138                 };
139         case 5: // -Z Axis
140                 {
141                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
142                         trans.rotate(rotquatje);
143                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
144                         break;
145                 };
146         default:
147                 {
148                 }
149         }
150         
151         //Using a temp variable to translate MT_Point3 to float[3].
152         //float[3] works better for the Python interface.
153         MT_Point3 temp = trans.getOrigin();
154         m_cone_origin[0] = temp[0];
155         m_cone_origin[1] = temp[1];
156         m_cone_origin[2] = temp[2];
157
158         temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
159         m_cone_target[0] = temp[0];
160         m_cone_target[1] = temp[1];
161         m_cone_target[2] = temp[2];
162
163
164         if (m_physCtrl)
165         {
166                 PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
167                 const MT_Point3& pos = trans.getOrigin();
168                 float ori[12];
169                 trans.getBasis().getValue(ori);
170                 motionState->setWorldPosition(pos[0], pos[1], pos[2]);
171                 motionState->setWorldOrientation(ori);
172                 m_physCtrl->WriteMotionStateToDynamics(true);
173         }
174
175 }
176
177 /* ------------------------------------------------------------------------- */
178 /* Python Functions                                                                                                                      */
179 /* ------------------------------------------------------------------------- */
180
181 //Deprecated ----->
182 /* getConeOrigin */
183 const char KX_RadarSensor::GetConeOrigin_doc[] = 
184 "getConeOrigin()\n"
185 "\tReturns the origin of the cone with which to test. The origin\n"
186 "\tis in the middle of the cone.";
187 PyObject* KX_RadarSensor::PyGetConeOrigin() {
188         ShowDeprecationWarning("getConeOrigin()", "the coneOrigin property");
189
190         PyObject *retVal = PyList_New(3);
191         
192         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
193         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
194         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
195         
196         return retVal;
197 }
198
199 /* getConeOrigin */
200 const char KX_RadarSensor::GetConeTarget_doc[] = 
201 "getConeTarget()\n"
202 "\tReturns the center of the bottom face of the cone with which to test.\n";
203 PyObject* KX_RadarSensor::PyGetConeTarget() {
204         ShowDeprecationWarning("getConeTarget()", "the coneTarget property");
205
206         PyObject *retVal = PyList_New(3);
207         
208         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
209         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
210         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
211         
212         return retVal;
213 }
214
215 /* getConeHeight */
216 const char KX_RadarSensor::GetConeHeight_doc[] = 
217 "getConeHeight()\n"
218 "\tReturns the height of the cone with which to test.\n";
219 PyObject* KX_RadarSensor::PyGetConeHeight() {
220                                                                                           
221         ShowDeprecationWarning("getConeHeight()", "the distance property");
222
223         return PyFloat_FromDouble(m_coneheight);
224 }
225 //<----- Deprecated
226
227 /* ------------------------------------------------------------------------- */
228 /* Python Integration Hooks                                                  */
229 /* ------------------------------------------------------------------------- */
230 PyTypeObject KX_RadarSensor::Type = {
231 #if (PY_VERSION_HEX >= 0x02060000)
232         PyVarObject_HEAD_INIT(NULL, 0)
233 #else
234         /* python 2.5 and below */
235         PyObject_HEAD_INIT( NULL )  /* required py macro */
236         0,                          /* ob_size */
237 #endif
238         "KX_RadarSensor",
239         sizeof(PyObjectPlus_Proxy),
240         0,
241         py_base_dealloc,
242         0,
243         0,
244         0,
245         0,
246         py_base_repr,
247         0,0,0,0,0,0,0,0,0,
248         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
249         0,0,0,0,0,0,0,
250         Methods,
251         0,
252         0,
253         &KX_NearSensor::Type,
254         0,0,0,0,0,0,
255         py_base_new
256 };
257
258 PyMethodDef KX_RadarSensor::Methods[] = {
259         //Deprecated ----->
260         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
261          METH_VARARGS, (PY_METHODCHAR)GetConeOrigin_doc},
262         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
263          METH_VARARGS, (PY_METHODCHAR)GetConeTarget_doc},
264         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
265          METH_VARARGS, (PY_METHODCHAR)GetConeHeight_doc},
266          //<-----
267         {NULL} //Sentinel
268 };
269
270 PyAttributeDef KX_RadarSensor::Attributes[] = {
271         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
272         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
273         KX_PYATTRIBUTE_FLOAT_RO("distance", KX_RadarSensor, m_coneheight),
274         KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
275         KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
276         {NULL} //Sentinel
277 };
278