removing files that should not be in blender2.5, added (by mistake?) r19226
[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                         PyTypeObject* T)
54
55                         : KX_NearSensor(
56                                 eventmgr,
57                                 gameobj,
58                                 //DT_NewCone(coneradius,coneheight),
59                                 margin,
60                                 resetmargin,
61                                 bFindMaterial,
62                                 touchedpropname,
63                                 kxscene,
64                                 physCtrl,
65                                 T),
66                                 m_coneradius(coneradius),
67                                 m_coneheight(coneheight),
68                                 m_axis(axis)
69 {
70         m_client_info->m_type = KX_ClientObjectInfo::SENSOR;
71         //m_client_info->m_clientobject = gameobj;
72         //m_client_info->m_auxilary_info = NULL;
73         //sumoObj->setClientObject(&m_client_info);
74 }
75                         
76 KX_RadarSensor::~KX_RadarSensor()
77 {
78         
79 }
80
81 CValue* KX_RadarSensor::GetReplica()
82 {
83         KX_RadarSensor* replica = new KX_RadarSensor(*this);
84         replica->ProcessReplica();
85         return replica;
86 }
87
88 /**
89  *      Transforms the collision object. A cone is not correctly centered
90  *      for usage.  */
91 void KX_RadarSensor::SynchronizeTransform()
92 {
93         // Getting the parent location was commented out. Why?
94         MT_Transform trans;
95         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
96         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
97         // What is the default orientation? pointing in the -y direction?
98         // is the geometry correctly converted?
99
100         // a collision cone is oriented
101         // center the cone correctly 
102         // depends on the radar 'axis'
103         switch (m_axis)
104         {
105         case 0: // +X Axis
106                 {
107                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
108                         trans.rotate(rotquatje);
109                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
110                         break;
111                 };
112         case 1: // +Y Axis
113                 {
114                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
115                         trans.rotate(rotquatje);
116                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
117                         break;
118                 };
119         case 2: // +Z Axis
120                 {
121                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
122                         trans.rotate(rotquatje);
123                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
124                         break;
125                 };
126         case 3: // -X Axis
127                 {
128                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
129                         trans.rotate(rotquatje);
130                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
131                         break;
132                 };
133         case 4: // -Y Axis
134                 {
135                         //MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
136                         //trans.rotate(rotquatje);
137                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
138                         break;
139                 };
140         case 5: // -Z Axis
141                 {
142                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
143                         trans.rotate(rotquatje);
144                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
145                         break;
146                 };
147         default:
148                 {
149                 }
150         }
151         
152         //Using a temp variable to translate MT_Point3 to float[3].
153         //float[3] works better for the Python interface.
154         MT_Point3 temp = trans.getOrigin();
155         m_cone_origin[0] = temp[0];
156         m_cone_origin[1] = temp[1];
157         m_cone_origin[2] = temp[2];
158
159         temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
160         m_cone_target[0] = temp[0];
161         m_cone_target[1] = temp[1];
162         m_cone_target[2] = temp[2];
163
164
165         if (m_physCtrl)
166         {
167                 PHY_IMotionState* motionState = m_physCtrl->GetMotionState();
168                 const MT_Point3& pos = trans.getOrigin();
169                 float ori[12];
170                 trans.getBasis().getValue(ori);
171                 motionState->setWorldPosition(pos[0], pos[1], pos[2]);
172                 motionState->setWorldOrientation(ori);
173                 m_physCtrl->WriteMotionStateToDynamics(true);
174         }
175
176 }
177
178 /* ------------------------------------------------------------------------- */
179 /* Python Functions                                                                                                                      */
180 /* ------------------------------------------------------------------------- */
181
182 //Deprecated ----->
183 /* getConeOrigin */
184 const char KX_RadarSensor::GetConeOrigin_doc[] = 
185 "getConeOrigin()\n"
186 "\tReturns the origin of the cone with which to test. The origin\n"
187 "\tis in the middle of the cone.";
188 PyObject* KX_RadarSensor::PyGetConeOrigin() {
189         ShowDeprecationWarning("getConeOrigin()", "the coneOrigin property");
190
191         PyObject *retVal = PyList_New(3);
192         
193         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
194         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
195         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
196         
197         return retVal;
198 }
199
200 /* getConeOrigin */
201 const char KX_RadarSensor::GetConeTarget_doc[] = 
202 "getConeTarget()\n"
203 "\tReturns the center of the bottom face of the cone with which to test.\n";
204 PyObject* KX_RadarSensor::PyGetConeTarget() {
205         ShowDeprecationWarning("getConeTarget()", "the coneTarget property");
206
207         PyObject *retVal = PyList_New(3);
208         
209         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
210         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
211         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
212         
213         return retVal;
214 }
215
216 /* getConeHeight */
217 const char KX_RadarSensor::GetConeHeight_doc[] = 
218 "getConeHeight()\n"
219 "\tReturns the height of the cone with which to test.\n";
220 PyObject* KX_RadarSensor::PyGetConeHeight() {
221                                                                                           
222         ShowDeprecationWarning("getConeHeight()", "the distance property");
223
224         return PyFloat_FromDouble(m_coneheight);
225 }
226 //<----- Deprecated
227
228 /* ------------------------------------------------------------------------- */
229 /* Python Integration Hooks                                                  */
230 /* ------------------------------------------------------------------------- */
231 PyTypeObject KX_RadarSensor::Type = {
232 #if (PY_VERSION_HEX >= 0x02060000)
233         PyVarObject_HEAD_INIT(NULL, 0)
234 #else
235         /* python 2.5 and below */
236         PyObject_HEAD_INIT( NULL )  /* required py macro */
237         0,                          /* ob_size */
238 #endif
239         "KX_RadarSensor",
240         sizeof(PyObjectPlus_Proxy),
241         0,
242         py_base_dealloc,
243         0,
244         0,
245         0,
246         0,
247         py_base_repr,
248         0,0,0,0,0,0,
249         py_base_getattro,
250         py_base_setattro,
251         0,0,0,0,0,0,0,0,0,
252         Methods
253 };
254
255 PyParentObject KX_RadarSensor::Parents[] = {
256         &KX_RadarSensor::Type,
257         &KX_NearSensor::Type,
258         &KX_TouchSensor::Type,
259         &SCA_ISensor::Type,
260         &SCA_ILogicBrick::Type,
261         &CValue::Type,
262         NULL
263 };
264
265 PyMethodDef KX_RadarSensor::Methods[] = {
266         //Deprecated ----->
267         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
268          METH_VARARGS, (PY_METHODCHAR)GetConeOrigin_doc},
269         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
270          METH_VARARGS, (PY_METHODCHAR)GetConeTarget_doc},
271         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
272          METH_VARARGS, (PY_METHODCHAR)GetConeHeight_doc},
273          //<-----
274         {NULL} //Sentinel
275 };
276
277 PyAttributeDef KX_RadarSensor::Attributes[] = {
278         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
279         KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
280         KX_PYATTRIBUTE_FLOAT_RO("distance", KX_RadarSensor, m_coneheight),
281         KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
282         KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
283         {NULL} //Sentinel
284 };
285
286 PyObject* KX_RadarSensor::py_getattro(PyObject *attr)
287 {
288         py_getattro_up(KX_NearSensor);
289 }
290
291 PyObject* KX_RadarSensor::py_getattro_dict() {
292         py_getattro_dict_up(KX_NearSensor);
293 }
294
295 int KX_RadarSensor::py_setattro(PyObject *attr, PyObject* value)
296 {
297         py_setattro_up(KX_NearSensor);
298 }