2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r18677...
[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
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 /**
39  *      RadarSensor constructor. Creates a near-sensor derived class, with a cone collision shape.
40  */
41 KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
42                 KX_GameObject* gameobj,
43                 PHY_IPhysicsController* physCtrl,
44                         double coneradius,
45                         double coneheight,
46                         int     axis,
47                         double margin,
48                         double resetmargin,
49                         bool bFindMaterial,
50                         const STR_String& touchedpropname,
51                         class KX_Scene* kxscene,
52                         PyTypeObject* T)
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                                 T),
65                                 m_coneradius(coneradius),
66                                 m_coneheight(coneheight),
67                                 m_axis(axis)
68 {
69         m_client_info->m_type = KX_ClientObjectInfo::RADAR;
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->m_colliders = new CListValue();
84         replica->Init();
85         // this will copy properties and so on...
86         CValue::AddDataToReplica(replica);
87         
88         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::RADAR);
89         
90         if (replica->m_physCtrl)
91         {
92                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
93                 if (replica->m_physCtrl)
94                 {
95                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
96                 }
97         }
98
99         //todo: make sure replication works fine!
100         //>m_sumoObj = new SM_Object(DT_NewCone(m_coneradius, m_coneheight),NULL,NULL,NULL);
101         //replica->m_sumoObj->setMargin(m_Margin);
102         //replica->m_sumoObj->setClientObject(replica->m_client_info);
103         
104         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
105         replica->SynchronizeTransform();
106         
107         return replica;
108 }
109
110
111 /**
112  *      Transforms the collision object. A cone is not correctly centered
113  *      for usage.  */
114 void KX_RadarSensor::SynchronizeTransform()
115 {
116         // Getting the parent location was commented out. Why?
117         MT_Transform trans;
118         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
119         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
120         // What is the default orientation? pointing in the -y direction?
121         // is the geometry correctly converted?
122
123         // a collision cone is oriented
124         // center the cone correctly 
125         // depends on the radar 'axis'
126         switch (m_axis)
127         {
128         case 0: // +X Axis
129                 {
130                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
131                         trans.rotate(rotquatje);
132                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
133                         break;
134                 };
135         case 1: // +Y Axis
136                 {
137                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
138                         trans.rotate(rotquatje);
139                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
140                         break;
141                 };
142         case 2: // +Z Axis
143                 {
144                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
145                         trans.rotate(rotquatje);
146                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
147                         break;
148                 };
149         case 3: // -X Axis
150                 {
151                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
152                         trans.rotate(rotquatje);
153                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
154                         break;
155                 };
156         case 4: // -Y Axis
157                 {
158                         //MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
159                         //trans.rotate(rotquatje);
160                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
161                         break;
162                 };
163         case 5: // -Z Axis
164                 {
165                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
166                         trans.rotate(rotquatje);
167                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
168                         break;
169                 };
170         default:
171                 {
172                 }
173         }
174         
175         //Using a temp variable to translate MT_Point3 to float[3].
176         //float[3] works better for the Python interface.
177         MT_Point3 temp = trans.getOrigin();
178         m_cone_origin[0] = temp[0];
179         m_cone_origin[1] = temp[1];
180         m_cone_origin[2] = temp[2];
181
182         temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
183         m_cone_target[0] = temp[0];
184         m_cone_target[1] = temp[1];
185         m_cone_target[2] = temp[2];
186
187
188         if (m_physCtrl)
189         {
190                 MT_Quaternion orn = trans.getRotation();
191                 MT_Point3 pos = trans.getOrigin();
192                 m_physCtrl->setPosition(pos[0],pos[1],pos[2]);
193                 m_physCtrl->setOrientation(orn[0],orn[1],orn[2],orn[3]);
194                 m_physCtrl->calcXform();
195         }
196
197 }
198
199 /* ------------------------------------------------------------------------- */
200 /* Python Functions                                                                                                                      */
201 /* ------------------------------------------------------------------------- */
202
203 //Deprecated ----->
204 /* getConeOrigin */
205 const char KX_RadarSensor::GetConeOrigin_doc[] = 
206 "getConeOrigin()\n"
207 "\tReturns the origin of the cone with which to test. The origin\n"
208 "\tis in the middle of the cone.";
209 PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self) {
210         ShowDeprecationWarning("getConeOrigin()", "the coneOrigin property");
211
212         PyObject *retVal = PyList_New(3);
213         
214         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
215         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
216         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
217         
218         return retVal;
219 }
220
221 /* getConeOrigin */
222 const char KX_RadarSensor::GetConeTarget_doc[] = 
223 "getConeTarget()\n"
224 "\tReturns the center of the bottom face of the cone with which to test.\n";
225 PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self) {
226         ShowDeprecationWarning("getConeTarget()", "the coneTarget property");
227
228         PyObject *retVal = PyList_New(3);
229         
230         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
231         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
232         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
233         
234         return retVal;
235 }
236
237 /* getConeHeight */
238 const char KX_RadarSensor::GetConeHeight_doc[] = 
239 "getConeHeight()\n"
240 "\tReturns the height of the cone with which to test.\n";
241 PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self) {
242                                                                                           
243         ShowDeprecationWarning("getConeHeight()", "the distance property");
244
245         return PyFloat_FromDouble(m_coneheight);
246 }
247 //<----- Deprecated
248
249 /* ------------------------------------------------------------------------- */
250 /* Python Integration Hooks                                                  */
251 /* ------------------------------------------------------------------------- */
252 PyTypeObject KX_RadarSensor::Type = {
253         PyObject_HEAD_INIT(&PyType_Type)
254         0,
255         "KX_RadarSensor",
256         sizeof(KX_RadarSensor),
257         0,
258         PyDestructor,
259         0,
260         __getattr,
261         __setattr,
262         0, //&MyPyCompare,
263         __repr,
264         0, //&cvalue_as_number,
265         0,
266         0,
267         0,
268         0
269 };
270
271 PyParentObject KX_RadarSensor::Parents[] = {
272         &KX_RadarSensor::Type,
273         &KX_NearSensor::Type,
274         &KX_TouchSensor::Type,
275         &SCA_ISensor::Type,
276         &SCA_ILogicBrick::Type,
277         &CValue::Type,
278         NULL
279 };
280
281 PyMethodDef KX_RadarSensor::Methods[] = {
282         //Deprecated ----->
283         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
284          METH_VARARGS, (PY_METHODCHAR)GetConeOrigin_doc},
285         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
286          METH_VARARGS, (PY_METHODCHAR)GetConeTarget_doc},
287         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
288          METH_VARARGS, (PY_METHODCHAR)GetConeHeight_doc},
289          //<-----
290         {NULL} //Sentinel
291 };
292
293 PyAttributeDef KX_RadarSensor::Attributes[] = {
294         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
295         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
296         KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
297         KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
298         {NULL} //Sentinel
299 };
300
301 PyObject* KX_RadarSensor::_getattr(const char *attr)
302 {
303         PyObject* object = _getattr_self(Attributes, this, attr);
304         if (object != NULL)
305                 return object;
306
307         _getattr_up(KX_NearSensor);
308 }
309
310 int KX_RadarSensor::_setattr(const char *attr, PyObject* value)
311 {
312         int ret = _setattr_self(Attributes, this, attr, value);
313         if (ret >= 0)
314                 return ret;
315
316         return KX_NearSensor::_setattr(attr, value);
317 }