fixed some motionstate synchronization issues
[blender.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         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
107         replica->SynchronizeTransform();
108         
109         return replica;
110 }
111
112
113 /**
114  *      Transforms the collision object. A cone is not correctly centered
115  *      for usage.  */
116 void KX_RadarSensor::SynchronizeTransform()
117 {
118         // Getting the parent location was commented out. Why?
119         MT_Transform trans;
120         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
121         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
122         // What is the default orientation? pointing in the -y direction?
123         // is the geometry correctly converted?
124
125         // a collision cone is oriented
126         // center the cone correctly 
127         // depends on the radar 'axis'
128         switch (m_axis)
129         {
130         case 0: // X Axis
131                 {
132                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
133                         trans.rotate(rotquatje);
134                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
135                         break;
136                 };
137         case 1: // Y Axis
138                 {
139                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
140                         trans.rotate(rotquatje);
141                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
142                         break;
143                 };
144         case 2: // Z Axis
145                 {
146                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
147                         trans.rotate(rotquatje);
148                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
149                         break;
150                 };
151         default:
152                 {
153                 }
154         }
155         m_cone_origin = trans.getOrigin();
156         m_cone_target = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
157
158
159         if (m_physCtrl)
160         {
161                 m_physCtrl->setPosition(trans.getOrigin().x(),trans.getOrigin().y(),trans.getOrigin().z());
162                 m_physCtrl->setOrientation(trans.getRotation().x(),trans.getRotation().y(),trans.getRotation().z(),trans.getRotation().w());
163                 m_physCtrl->calcXform();
164         }
165
166 }
167
168 /* ------------------------------------------------------------------------- */
169 /* Python functions                                                          */
170 /* ------------------------------------------------------------------------- */
171
172 /* Integration hooks ------------------------------------------------------- */
173 PyTypeObject KX_RadarSensor::Type = {
174         PyObject_HEAD_INIT(&PyType_Type)
175         0,
176         "KX_RadarSensor",
177         sizeof(KX_RadarSensor),
178         0,
179         PyDestructor,
180         0,
181         __getattr,
182         __setattr,
183         0, //&MyPyCompare,
184         __repr,
185         0, //&cvalue_as_number,
186         0,
187         0,
188         0,
189         0
190 };
191
192 PyParentObject KX_RadarSensor::Parents[] = {
193         &KX_RadarSensor::Type,
194         &KX_NearSensor::Type,
195         &KX_TouchSensor::Type,
196         &SCA_ISensor::Type,
197         &SCA_ILogicBrick::Type,
198         &CValue::Type,
199         NULL
200 };
201
202 PyMethodDef KX_RadarSensor::Methods[] = {
203         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
204          METH_VARARGS, GetConeOrigin_doc},
205         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
206          METH_VARARGS, GetConeTarget_doc},
207         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
208          METH_VARARGS, GetConeHeight_doc},
209         {NULL,NULL,NULL,NULL} //Sentinel
210 };
211
212 PyObject* KX_RadarSensor::_getattr(const STR_String& attr) {
213         _getattr_up(KX_TouchSensor);
214 }
215
216 /* getConeOrigin */
217 char KX_RadarSensor::GetConeOrigin_doc[] = 
218 "getConeOrigin()\n"
219 "\tReturns the origin of the cone with which to test. The origin\n"
220 "\tis in the middle of the cone.";
221 PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self, 
222                                                                                   PyObject* args, 
223                                                                                   PyObject* kwds) {
224         PyObject *retVal = PyList_New(3);
225         
226         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
227         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
228         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
229         
230         return retVal;
231 }
232
233 /* getConeOrigin */
234 char KX_RadarSensor::GetConeTarget_doc[] = 
235 "getConeTarget()\n"
236 "\tReturns the center of the bottom face of the cone with which to test.\n";
237 PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self, 
238                                                                                   PyObject* args, 
239                                                                                   PyObject* kwds) {
240         PyObject *retVal = PyList_New(3);
241         
242         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
243         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
244         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
245         
246         return retVal;
247 }
248
249 /* getConeOrigin */
250 char KX_RadarSensor::GetConeHeight_doc[] = 
251 "getConeHeight()\n"
252 "\tReturns the height of the cone with which to test.\n";
253 PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self, 
254                                                                                   PyObject* args, 
255                                                                                   PyObject* kwds) {
256         return PyFloat_FromDouble(m_coneheight);
257 }
258
259