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