Merge from trunk 16122-16307
[blender.git] / source / gameengine / Ketsji / KX_VehicleWrapper.cpp
1
2
3 #include <Python.h>
4 #include "KX_VehicleWrapper.h"
5 #include "PHY_IPhysicsEnvironment.h"
6 #include "PHY_IVehicle.h"
7 #include "KX_PyMath.h"
8 #include "KX_GameObject.h"
9 #include "KX_MotionState.h"
10
11 #ifdef HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 KX_VehicleWrapper::KX_VehicleWrapper(
16                                                 PHY_IVehicle* vehicle,
17                                                 PHY_IPhysicsEnvironment* physenv,PyTypeObject *T) :
18                 PyObjectPlus(T),
19                 m_vehicle(vehicle),
20                 m_physenv(physenv)
21 {
22 }
23
24 KX_VehicleWrapper::~KX_VehicleWrapper()
25 {
26         int numMotion = m_motionStates.size();
27         for (int i=0;i<numMotion;i++)
28         {
29                 PHY_IMotionState* motionState = m_motionStates[i];
30                 delete motionState;
31         }
32         m_motionStates.clear();
33 }
34
35
36 PyObject* KX_VehicleWrapper::PyAddWheel(PyObject* self, 
37                                                                                         PyObject* args, 
38                                                                                         PyObject* kwds)
39 {
40         
41         PyObject* pylistPos,*pylistDir,*pylistAxleDir;
42         PyObject* wheelGameObject;
43         float suspensionRestLength,wheelRadius;
44         int hasSteering;
45
46         
47         if (PyArg_ParseTuple(args,"OOOOffi",&wheelGameObject,&pylistPos,&pylistDir,&pylistAxleDir,&suspensionRestLength,&wheelRadius,&hasSteering))
48         {
49                 KX_GameObject* gameOb = (KX_GameObject*) wheelGameObject;
50                 
51                 PHY_IMotionState* motionState = new KX_MotionState(gameOb->GetSGNode());
52
53                 MT_Vector3 attachPos,attachDir,attachAxle;
54                 PyVecTo(pylistPos,attachPos);
55                 PyVecTo(pylistDir,attachDir);
56                 PyVecTo(pylistAxleDir,attachAxle);
57                 PHY__Vector3 aPos,aDir,aAxle;
58                 aPos[0] = attachPos[0];
59                 aPos[1] = attachPos[1];
60                 aPos[2] = attachPos[2];
61                 aDir[0] = attachDir[0];
62                 aDir[1] = attachDir[1];
63                 aDir[2] = attachDir[2];
64                 aAxle[0] = -attachAxle[0];//someone reverse some conventions inside Bullet (axle winding)
65                 aAxle[1] = -attachAxle[1];
66                 aAxle[2] = -attachAxle[2];
67                 
68                 printf("attempt for addWheel: suspensionRestLength%f wheelRadius %f, hasSteering:%d\n",suspensionRestLength,wheelRadius,hasSteering);
69                 m_vehicle->AddWheel(motionState,aPos,aDir,aAxle,suspensionRestLength,wheelRadius,hasSteering);
70                 
71         } else {
72                 return NULL;
73         }
74         Py_RETURN_NONE;
75 }
76
77
78
79
80 PyObject* KX_VehicleWrapper::PyGetWheelPosition(PyObject* self, 
81                                                                                         PyObject* args, 
82                                                                                         PyObject* kwds)
83 {
84         
85         int wheelIndex;
86
87         if (PyArg_ParseTuple(args,"i",&wheelIndex))
88         {
89                 float position[3];
90                 m_vehicle->GetWheelPosition(wheelIndex,position[0],position[1],position[2]);
91                 MT_Vector3 pos(position[0],position[1],position[2]);
92                 return PyObjectFrom(pos);
93         }
94         return NULL;
95 }
96
97 PyObject* KX_VehicleWrapper::PyGetWheelRotation(PyObject* self, 
98                                                                                         PyObject* args, 
99                                                                                         PyObject* kwds)
100 {
101         int wheelIndex;
102         if (PyArg_ParseTuple(args,"i",&wheelIndex))
103         {
104                 return PyFloat_FromDouble(m_vehicle->GetWheelRotation(wheelIndex));
105         }
106         return NULL;
107 }
108
109 PyObject* KX_VehicleWrapper::PyGetWheelOrientationQuaternion(PyObject* self, 
110                                                                                         PyObject* args, 
111                                                                                         PyObject* kwds)
112 {
113         int wheelIndex;
114         if (PyArg_ParseTuple(args,"i",&wheelIndex))
115         {
116                 float orn[4];
117                 m_vehicle->GetWheelOrientationQuaternion(wheelIndex,orn[0],orn[1],orn[2],orn[3]);
118                 MT_Quaternion   quatorn(orn[0],orn[1],orn[2],orn[3]);
119                 MT_Matrix3x3 ornmat(quatorn);
120                 return PyObjectFrom(ornmat);
121         }
122         return NULL;
123
124 }
125
126
127 PyObject* KX_VehicleWrapper::PyGetNumWheels(PyObject* self, 
128                                                                                         PyObject* args, 
129                                                                                         PyObject* kwds)
130 {
131         return PyInt_FromLong(m_vehicle->GetNumWheels());
132 }
133
134
135 PyObject* KX_VehicleWrapper::PyGetConstraintId(PyObject* self, 
136                                                                                         PyObject* args, 
137                                                                                         PyObject* kwds)
138 {
139         return PyInt_FromLong(m_vehicle->GetUserConstraintId());
140 }
141
142
143
144 PyObject* KX_VehicleWrapper::PyApplyEngineForce(PyObject* self, 
145                                                                                         PyObject* args, 
146                                                                                         PyObject* kwds)
147 {
148         float force;
149         int wheelIndex;
150
151         if (PyArg_ParseTuple(args,"fi",&force,&wheelIndex))
152         {
153                 force *= -1.f;//someone reverse some conventions inside Bullet (axle winding)
154                 m_vehicle->ApplyEngineForce(force,wheelIndex);
155         }
156         else {
157                 return NULL;
158         }
159         Py_RETURN_NONE;
160 }
161
162 PyObject* KX_VehicleWrapper::PySetTyreFriction(PyObject* self, 
163                                                                                         PyObject* args, 
164                                                                                         PyObject* kwds)
165 {
166         float wheelFriction;
167         int wheelIndex;
168
169         if (PyArg_ParseTuple(args,"fi",&wheelFriction,&wheelIndex))
170         {
171                 m_vehicle->SetWheelFriction(wheelFriction,wheelIndex);
172         }
173         else {
174                 return NULL;
175         }
176         Py_RETURN_NONE;
177 }
178
179 PyObject* KX_VehicleWrapper::PySetSuspensionStiffness(PyObject* self, 
180                                                                                         PyObject* args, 
181                                                                                         PyObject* kwds)
182 {
183         float suspensionStiffness;
184         int wheelIndex;
185
186         if (PyArg_ParseTuple(args,"fi",&suspensionStiffness,&wheelIndex))
187         {
188                 m_vehicle->SetSuspensionStiffness(suspensionStiffness,wheelIndex);
189         }
190         else {
191                 return NULL;
192         }
193         Py_RETURN_NONE;
194 }
195
196 PyObject* KX_VehicleWrapper::PySetSuspensionDamping(PyObject* self, 
197                                                                                         PyObject* args, 
198                                                                                         PyObject* kwds)
199 {
200         float suspensionDamping;
201         int wheelIndex;
202
203         if (PyArg_ParseTuple(args,"fi",&suspensionDamping,&wheelIndex))
204         {
205                 m_vehicle->SetSuspensionDamping(suspensionDamping,wheelIndex);
206         } else {
207                 return NULL;
208         }
209         Py_RETURN_NONE;
210 }
211
212 PyObject* KX_VehicleWrapper::PySetSuspensionCompression(PyObject* self, 
213                                                                                         PyObject* args, 
214                                                                                         PyObject* kwds)
215 {
216         float suspensionCompression;
217         int wheelIndex;
218
219         if (PyArg_ParseTuple(args,"fi",&suspensionCompression,&wheelIndex))
220         {
221                 m_vehicle->SetSuspensionCompression(suspensionCompression,wheelIndex);
222         } else {
223                 return NULL;
224         }
225         Py_RETURN_NONE;
226 }
227
228 PyObject* KX_VehicleWrapper::PySetRollInfluence(PyObject* self, 
229                                                                                         PyObject* args, 
230                                                                                         PyObject* kwds)
231 {
232         float rollInfluence;
233         int wheelIndex;
234
235         if (PyArg_ParseTuple(args,"fi",&rollInfluence,&wheelIndex))
236         {
237                 m_vehicle->SetRollInfluence(rollInfluence,wheelIndex);
238         }
239         else {
240                 return NULL;
241         }
242         Py_RETURN_NONE;
243 }
244
245
246 PyObject* KX_VehicleWrapper::PyApplyBraking(PyObject* self, 
247                                                                                         PyObject* args, 
248                                                                                         PyObject* kwds)
249 {
250         float braking;
251         int wheelIndex;
252
253         if (PyArg_ParseTuple(args,"fi",&braking,&wheelIndex))
254         {
255                 m_vehicle->ApplyBraking(braking,wheelIndex);
256         }
257         else {
258                 return NULL;
259         }
260         Py_RETURN_NONE;
261 }
262
263
264
265
266 PyObject* KX_VehicleWrapper::PySetSteeringValue(PyObject* self, 
267                                                                                         PyObject* args, 
268                                                                                         PyObject* kwds)
269 {
270         float steeringValue;
271         int wheelIndex;
272
273         if (PyArg_ParseTuple(args,"fi",&steeringValue,&wheelIndex))
274         {
275                 m_vehicle->SetSteeringValue(steeringValue,wheelIndex);
276         }
277         else {
278                 return NULL;
279         }
280         Py_RETURN_NONE;
281 }
282
283
284 PyObject* KX_VehicleWrapper::PyGetConstraintType(PyObject* self, 
285                                                                                         PyObject* args, 
286                                                                                         PyObject* kwds)
287 {
288         return PyInt_FromLong(m_vehicle->GetUserConstraintType());
289 }
290
291
292
293
294
295 //python specific stuff
296 PyTypeObject KX_VehicleWrapper::Type = {
297         PyObject_HEAD_INIT(&PyType_Type)
298                 0,
299                 "KX_VehicleWrapper",
300                 sizeof(KX_VehicleWrapper),
301                 0,
302                 PyDestructor,
303                 0,
304                 __getattr,
305                 __setattr,
306                 0, //&MyPyCompare,
307                 __repr,
308                 0, //&cvalue_as_number,
309                 0,
310                 0,
311                 0,
312                 0
313 };
314
315 PyParentObject KX_VehicleWrapper::Parents[] = {
316         &KX_VehicleWrapper::Type,
317         NULL
318 };
319
320 PyObject*       KX_VehicleWrapper::_getattr(const STR_String& attr)
321 {
322         //here you can search for existing data members (like mass,friction etc.)
323         _getattr_up(PyObjectPlus);
324 }
325
326 int     KX_VehicleWrapper::_setattr(const STR_String& attr,PyObject* pyobj)
327 {
328         
329         PyTypeObject* type = pyobj->ob_type;
330         int result = 1;
331
332         if (type == &PyList_Type)
333         {
334                 result = 0;
335         }
336         if (type == &PyFloat_Type)
337         {
338                 result = 0;
339
340         }
341         if (type == &PyInt_Type)
342         {
343                 result = 0;
344         }
345         if (type == &PyString_Type)
346         {
347                 result = 0;
348         }
349         if (result)
350                 result = PyObjectPlus::_setattr(attr,pyobj);
351         return result;
352 };
353
354
355 PyMethodDef KX_VehicleWrapper::Methods[] = {
356         {"addWheel",(PyCFunction) KX_VehicleWrapper::sPyAddWheel, METH_VARARGS},
357         {"getNumWheels",(PyCFunction) KX_VehicleWrapper::sPyGetNumWheels, METH_VARARGS},
358         {"getWheelOrientationQuaternion",(PyCFunction) KX_VehicleWrapper::sPyGetWheelOrientationQuaternion, METH_VARARGS},
359         {"getWheelRotation",(PyCFunction) KX_VehicleWrapper::sPyGetWheelRotation, METH_VARARGS},
360         {"getWheelPosition",(PyCFunction) KX_VehicleWrapper::sPyGetWheelPosition, METH_VARARGS},
361         {"getConstraintId",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintId, METH_VARARGS},
362         {"getConstraintType",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintType, METH_VARARGS},
363         {"setSteeringValue",(PyCFunction) KX_VehicleWrapper::sPySetSteeringValue, METH_VARARGS},
364         {"applyEngineForce",(PyCFunction) KX_VehicleWrapper::sPyApplyEngineForce, METH_VARARGS},
365         {"applyBraking",(PyCFunction) KX_VehicleWrapper::sPyApplyBraking, METH_VARARGS},
366
367         {"setTyreFriction",(PyCFunction) KX_VehicleWrapper::sPySetTyreFriction, METH_VARARGS},
368
369         {"setSuspensionStiffness",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionStiffness, METH_VARARGS},
370
371         {"setSuspensionDamping",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionDamping, METH_VARARGS},
372
373         {"setSuspensionCompression",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionCompression, METH_VARARGS},
374
375         {"setRollInfluence",(PyCFunction) KX_VehicleWrapper::sPySetRollInfluence, METH_VARARGS},
376
377         {NULL,NULL} //Sentinel
378 };
379