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