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