* blender doesn't need the -w anymore to start in a window
[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         m_cone_origin = trans.getOrigin();
175         m_cone_target = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
176
177
178         if (m_physCtrl)
179         {
180                 MT_Quaternion orn = trans.getRotation();
181                 MT_Point3 pos = trans.getOrigin();
182                 m_physCtrl->setPosition(pos[0],pos[1],pos[2]);
183                 m_physCtrl->setOrientation(orn[0],orn[1],orn[2],orn[3]);
184                 m_physCtrl->calcXform();
185         }
186
187 }
188
189 /* ------------------------------------------------------------------------- */
190 /* Python functions                                                          */
191 /* ------------------------------------------------------------------------- */
192
193 /* Integration hooks ------------------------------------------------------- */
194 PyTypeObject KX_RadarSensor::Type = {
195         PyObject_HEAD_INIT(&PyType_Type)
196         0,
197         "KX_RadarSensor",
198         sizeof(KX_RadarSensor),
199         0,
200         PyDestructor,
201         0,
202         __getattr,
203         __setattr,
204         0, //&MyPyCompare,
205         __repr,
206         0, //&cvalue_as_number,
207         0,
208         0,
209         0,
210         0
211 };
212
213 PyParentObject KX_RadarSensor::Parents[] = {
214         &KX_RadarSensor::Type,
215         &KX_NearSensor::Type,
216         &KX_TouchSensor::Type,
217         &SCA_ISensor::Type,
218         &SCA_ILogicBrick::Type,
219         &CValue::Type,
220         NULL
221 };
222
223 PyMethodDef KX_RadarSensor::Methods[] = {
224         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
225          METH_NOARGS, (PY_METHODCHAR)GetConeOrigin_doc},
226         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
227          METH_NOARGS, (PY_METHODCHAR)GetConeTarget_doc},
228         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
229          METH_NOARGS, (PY_METHODCHAR)GetConeHeight_doc},
230         {NULL,NULL,NULL,NULL} //Sentinel
231 };
232
233 PyAttributeDef KX_RadarSensor::Attributes[] = {
234         { NULL }        //Sentinel
235 };
236
237 PyObject* KX_RadarSensor::_getattr(const char *attr) {
238         _getattr_up(KX_TouchSensor);
239 }
240
241 /* getConeOrigin */
242 const char KX_RadarSensor::GetConeOrigin_doc[] = 
243 "getConeOrigin()\n"
244 "\tReturns the origin of the cone with which to test. The origin\n"
245 "\tis in the middle of the cone.";
246 PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self) {
247         return PyObjectFrom(m_cone_origin);
248 }
249
250 /* getConeOrigin */
251 const char KX_RadarSensor::GetConeTarget_doc[] = 
252 "getConeTarget()\n"
253 "\tReturns the center of the bottom face of the cone with which to test.\n";
254 PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self) {
255         return PyObjectFrom(m_cone_target);
256 }
257
258 /* getConeOrigin */
259 const char KX_RadarSensor::GetConeHeight_doc[] = 
260 "getConeHeight()\n"
261 "\tReturns the height of the cone with which to test.\n";
262 PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self) {
263         return PyFloat_FromDouble(m_coneheight);
264 }
265
266