[GameEngine] Commit all Kester's changes made to the gameengine to restore 2.25 like...
[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
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                         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                                 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
75 KX_RadarSensor::~KX_RadarSensor()
76 {
77         
78 }
79
80 /**
81  *      Transforms the collision object. A cone is not correctly centered
82  *      for usage.  */
83 void KX_RadarSensor::SynchronizeTransform()
84 {
85         // Getting the parent location was commented out. Why?
86         MT_Transform trans;
87         trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
88         trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
89         // What is the default orientation? pointing in the -y direction?
90         // is the geometry correctly converted?
91
92         // a collision cone is oriented
93         // center the cone correctly 
94         // depends on the radar 'axis'
95         switch (m_axis)
96         {
97         case 0: // X Axis
98                 {
99                         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
100                         trans.rotate(rotquatje);
101                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
102                         break;
103                 };
104         case 1: // Y Axis
105                 {
106                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
107                         trans.rotate(rotquatje);
108                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
109                         break;
110                 };
111         case 2: // Z Axis
112                 {
113                         MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
114                         trans.rotate(rotquatje);
115                         trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
116                         break;
117                 };
118         default:
119                 {
120                 }
121         }
122         m_cone_origin = trans.getOrigin();
123         m_cone_target = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
124
125         m_sumoObj->setPosition(trans.getOrigin());
126         m_sumoObj->setOrientation(trans.getRotation());
127         m_sumoObj->calcXform();
128 }
129
130 /* ------------------------------------------------------------------------- */
131 /* Python functions                                                          */
132 /* ------------------------------------------------------------------------- */
133
134 /* Integration hooks ------------------------------------------------------- */
135 PyTypeObject KX_RadarSensor::Type = {
136         PyObject_HEAD_INIT(&PyType_Type)
137         0,
138         "KX_RadarSensor",
139         sizeof(KX_RadarSensor),
140         0,
141         PyDestructor,
142         0,
143         __getattr,
144         __setattr,
145         0, //&MyPyCompare,
146         __repr,
147         0, //&cvalue_as_number,
148         0,
149         0,
150         0,
151         0
152 };
153
154 PyParentObject KX_RadarSensor::Parents[] = {
155         &KX_RadarSensor::Type,
156         &KX_NearSensor::Type,
157         &KX_TouchSensor::Type,
158         &SCA_ISensor::Type,
159         &SCA_ILogicBrick::Type,
160         &CValue::Type,
161         NULL
162 };
163
164 PyMethodDef KX_RadarSensor::Methods[] = {
165         {"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin, 
166          METH_VARARGS, GetConeOrigin_doc},
167         {"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget, 
168          METH_VARARGS, GetConeTarget_doc},
169         {"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight, 
170          METH_VARARGS, GetConeHeight_doc},
171         {NULL,NULL,NULL,NULL} //Sentinel
172 };
173
174 PyObject* KX_RadarSensor::_getattr(char* attr) {
175         _getattr_up(KX_TouchSensor);
176 }
177
178 /* getConeOrigin */
179 char KX_RadarSensor::GetConeOrigin_doc[] = 
180 "getConeOrigin()\n"
181 "\tReturns the origin of the cone with which to test. The origin\n"
182 "\tis in the middle of the cone.";
183 PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self, 
184                                                                                   PyObject* args, 
185                                                                                   PyObject* kwds) {
186         PyObject *retVal = PyList_New(3);
187         
188         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
189         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
190         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
191         
192         return retVal;
193 }
194
195 /* getConeOrigin */
196 char KX_RadarSensor::GetConeTarget_doc[] = 
197 "getConeTarget()\n"
198 "\tReturns the center of the bottom face of the cone with which to test.\n";
199 PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self, 
200                                                                                   PyObject* args, 
201                                                                                   PyObject* kwds) {
202         PyObject *retVal = PyList_New(3);
203         
204         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
205         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
206         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
207         
208         return retVal;
209 }
210
211 /* getConeOrigin */
212 char KX_RadarSensor::GetConeHeight_doc[] = 
213 "getConeHeight()\n"
214 "\tReturns the height of the cone with which to test.\n";
215 PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self, 
216                                                                                   PyObject* args, 
217                                                                                   PyObject* kwds) {
218         return PyFloat_FromDouble(m_coneheight);
219 }
220
221