eae9784935c80b0d9149985fef6dc9f69a127eef
[blender-staging.git] / source / gameengine / Ketsji / KX_RadarSensor.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "KX_RadarSensor.h"
33 #include "KX_GameObject.h"
34 #include "PHY_IPhysicsController.h"
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 /**
41  *      RadarSensor constructor. Creates a near-sensor derived class, with a cone collision shape.
42  */
43 KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
44                 KX_GameObject* gameobj,
45                 PHY_IPhysicsController* physCtrl,
46                         double coneradius,
47                         double coneheight,
48                         int     axis,
49                         double margin,
50                         double resetmargin,
51                         bool bFindMaterial,
52                         const STR_String& touchedpropname,
53                         class KX_Scene* kxscene,
54                         PyTypeObject* T)
55
56                         : KX_NearSensor(
57                                 eventmgr,
58                                 gameobj,
59                                 //DT_NewCone(coneradius,coneheight),
60                                 margin,
61                                 resetmargin,
62                                 bFindMaterial,
63                                 touchedpropname,
64                                 kxscene,
65                                 physCtrl,
66                                 T),
67                                 m_coneradius(coneradius),
68                                 m_coneheight(coneheight),
69                                 m_axis(axis)
70 {
71         m_client_info->m_type = KX_ClientObjectInfo::RADAR;
72         //m_client_info->m_clientobject = gameobj;
73         //m_client_info->m_auxilary_info = NULL;
74         //sumoObj->setClientObject(&m_client_info);
75 }
76                         
77
78 KX_RadarSensor::~KX_RadarSensor()
79 {
80         
81 }
82
83 CValue* KX_RadarSensor::GetReplica()
84 {
85         KX_RadarSensor* replica = new KX_RadarSensor(*this);
86         replica->m_colliders = new CListValue();
87         replica->m_bCollision = false;
88         replica->m_bTriggered= false;
89         replica->m_hitObject = NULL;
90         replica->m_bLastTriggered = false;
91         // this will copy properties and so on...
92         CValue::AddDataToReplica(replica);
93         
94         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::RADAR);
95         
96         if (replica->m_physCtrl)
97         {
98                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
99         }
100
101         //todo: make sure replication works fine!
102         //>m_sumoObj = new SM_Object(DT_NewCone(m_coneradius, m_coneheight),NULL,NULL,NULL);
103         //replica->m_sumoObj->setMargin(m_Margin);
104         //replica->m_sumoObj->setClientObject(replica->m_client_info);
105         
106         replica->SynchronizeTransform();
107         
108         return replica;
109 }
110
111
112 /**
113  *      Transforms the collision object. A cone is not correctly centered
114  *      for usage.  */
115 void KX_RadarSensor::SynchronizeTransform()
116 {
117         // Getting the parent location was commented out. Why?
118         MT_Transform trans;
119         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
120         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
121         // What is the default orientation? pointing in the -y direction?
122         // is the geometry correctly converted?
123
124         // a collision cone is oriented
125         // center the cone correctly 
126         // depends on the radar 'axis'
127         switch (m_axis)
128         {
129         case 0: // X Axis
130                 {
131                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
132                         trans.rotate(rotquatje);
133                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
134                         break;
135                 };
136         case 1: // Y Axis
137                 {
138                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
139                         trans.rotate(rotquatje);
140                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
141                         break;
142                 };
143         case 2: // Z Axis
144                 {
145                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
146                         trans.rotate(rotquatje);
147                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
148                         break;
149                 };
150         default:
151                 {
152                 }
153         }
154         m_cone_origin = trans.getOrigin();
155         m_cone_target = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
156
157
158         if (m_physCtrl)
159         {
160                 m_physCtrl->setPosition(trans.getOrigin().x(),trans.getOrigin().y(),trans.getOrigin().z());
161                 m_physCtrl->setOrientation(trans.getRotation().x(),trans.getRotation().y(),trans.getRotation().z(),trans.getRotation().w());
162                 m_physCtrl->calcXform();
163         }
164
165 }
166
167 /* ------------------------------------------------------------------------- */
168 /* Python functions                                                          */
169 /* ------------------------------------------------------------------------- */
170
171 /* Integration hooks ------------------------------------------------------- */
172 PyTypeObject KX_RadarSensor::Type = {
173         PyObject_HEAD_INIT(&PyType_Type)
174         0,
175         "KX_RadarSensor",
176         sizeof(KX_RadarSensor),
177         0,
178         PyDestructor,
179         0,
180         __getattr,
181         __setattr,
182         0, //&MyPyCompare,
183         __repr,
184         0, //&cvalue_as_number,
185         0,
186         0,
187         0,
188         0
189 };
190
191 PyParentObject KX_RadarSensor::Parents[] = {
192         &KX_RadarSensor::Type,
193         &KX_NearSensor::Type,
194         &KX_TouchSensor::Type,
195         &SCA_ISensor::Type,
196         &SCA_ILogicBrick::Type,
197         &CValue::Type,
198         NULL
199 };
200
201 PyMethodDef KX_RadarSensor::Methods[] = {
202         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
203          METH_VARARGS, GetConeOrigin_doc},
204         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
205          METH_VARARGS, GetConeTarget_doc},
206         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
207          METH_VARARGS, GetConeHeight_doc},
208         {NULL,NULL,NULL,NULL} //Sentinel
209 };
210
211 PyObject* KX_RadarSensor::_getattr(const STR_String& attr) {
212         _getattr_up(KX_TouchSensor);
213 }
214
215 /* getConeOrigin */
216 char KX_RadarSensor::GetConeOrigin_doc[] = 
217 "getConeOrigin()\n"
218 "\tReturns the origin of the cone with which to test. The origin\n"
219 "\tis in the middle of the cone.";
220 PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self, 
221                                                                                   PyObject* args, 
222                                                                                   PyObject* kwds) {
223         PyObject *retVal = PyList_New(3);
224         
225         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
226         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
227         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
228         
229         return retVal;
230 }
231
232 /* getConeOrigin */
233 char KX_RadarSensor::GetConeTarget_doc[] = 
234 "getConeTarget()\n"
235 "\tReturns the center of the bottom face of the cone with which to test.\n";
236 PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self, 
237                                                                                   PyObject* args, 
238                                                                                   PyObject* kwds) {
239         PyObject *retVal = PyList_New(3);
240         
241         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
242         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
243         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
244         
245         return retVal;
246 }
247
248 /* getConeOrigin */
249 char KX_RadarSensor::GetConeHeight_doc[] = 
250 "getConeHeight()\n"
251 "\tReturns the height of the cone with which to test.\n";
252 PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self, 
253                                                                                   PyObject* args, 
254                                                                                   PyObject* kwds) {
255         return PyFloat_FromDouble(m_coneheight);
256 }
257
258