svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / Ketsji / KX_VehicleWrapper.cpp
1
2
3 #include <Python.h>
4 #include "PyObjectPlus.h"
5
6 #include "KX_VehicleWrapper.h"
7 #include "PHY_IPhysicsEnvironment.h"
8 #include "PHY_IVehicle.h"
9 #include "KX_PyMath.h"
10 #include "KX_GameObject.h"
11 #include "KX_MotionState.h"
12
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 KX_VehicleWrapper::KX_VehicleWrapper(
18                                                 PHY_IVehicle* vehicle,
19                                                 PHY_IPhysicsEnvironment* physenv) :
20                 PyObjectPlus(),
21                 m_vehicle(vehicle),
22                 m_physenv(physenv)
23 {
24 }
25
26 KX_VehicleWrapper::~KX_VehicleWrapper()
27 {
28         int numMotion = m_motionStates.size();
29         for (int i=0;i<numMotion;i++)
30         {
31                 PHY_IMotionState* motionState = m_motionStates[i];
32                 delete motionState;
33         }
34         m_motionStates.clear();
35 }
36
37
38 PyObject* KX_VehicleWrapper::PyAddWheel(PyObject* args)
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:addWheel",&wheelGameObject,&pylistPos,&pylistDir,&pylistAxleDir,&suspensionRestLength,&wheelRadius,&hasSteering))
48         {
49                 KX_GameObject *gameOb;
50                 if (!ConvertPythonToGameObject(wheelGameObject, &gameOb, false, "vehicle.addWheel(...): KX_VehicleWrapper (first argument)"))
51                         return NULL;
52                 
53
54                 if (gameOb->GetSGNode())
55                 {
56                         PHY_IMotionState* motionState = new KX_MotionState(gameOb->GetSGNode());
57                         
58                         /* TODO - no error checking here! - bad juju */
59                         MT_Vector3 attachPos,attachDir,attachAxle;
60                         PyVecTo(pylistPos,attachPos);
61                         PyVecTo(pylistDir,attachDir);
62                         PyVecTo(pylistAxleDir,attachAxle);
63                         PHY__Vector3 aPos,aDir,aAxle;
64                         aPos[0] = attachPos[0];
65                         aPos[1] = attachPos[1];
66                         aPos[2] = attachPos[2];
67                         aDir[0] = attachDir[0];
68                         aDir[1] = attachDir[1];
69                         aDir[2] = attachDir[2];
70                         aAxle[0] = -attachAxle[0];//someone reverse some conventions inside Bullet (axle winding)
71                         aAxle[1] = -attachAxle[1];
72                         aAxle[2] = -attachAxle[2];
73                         
74                         printf("attempt for addWheel: suspensionRestLength%f wheelRadius %f, hasSteering:%d\n",suspensionRestLength,wheelRadius,hasSteering);
75                         m_vehicle->AddWheel(motionState,aPos,aDir,aAxle,suspensionRestLength,wheelRadius,hasSteering);
76                 }
77                 
78         } else {
79                 return NULL;
80         }
81         Py_RETURN_NONE;
82 }
83
84
85
86
87 PyObject* KX_VehicleWrapper::PyGetWheelPosition(PyObject* args)
88 {
89         
90         int wheelIndex;
91
92         if (PyArg_ParseTuple(args,"i:getWheelPosition",&wheelIndex))
93         {
94                 float position[3];
95                 m_vehicle->GetWheelPosition(wheelIndex,position[0],position[1],position[2]);
96                 MT_Vector3 pos(position[0],position[1],position[2]);
97                 return PyObjectFrom(pos);
98         }
99         return NULL;
100 }
101
102 PyObject* KX_VehicleWrapper::PyGetWheelRotation(PyObject* args)
103 {
104         int wheelIndex;
105         if (PyArg_ParseTuple(args,"i:getWheelRotation",&wheelIndex))
106         {
107                 return PyFloat_FromDouble(m_vehicle->GetWheelRotation(wheelIndex));
108         }
109         return NULL;
110 }
111
112 PyObject* KX_VehicleWrapper::PyGetWheelOrientationQuaternion(PyObject* args)
113 {
114         int wheelIndex;
115         if (PyArg_ParseTuple(args,"i:getWheelOrientationQuaternion",&wheelIndex))
116         {
117                 float orn[4];
118                 m_vehicle->GetWheelOrientationQuaternion(wheelIndex,orn[0],orn[1],orn[2],orn[3]);
119                 MT_Quaternion   quatorn(orn[0],orn[1],orn[2],orn[3]);
120                 MT_Matrix3x3 ornmat(quatorn);
121                 return PyObjectFrom(ornmat);
122         }
123         return NULL;
124
125 }
126
127
128 PyObject* KX_VehicleWrapper::PyGetNumWheels(PyObject* args)
129 {
130         return PyLong_FromSsize_t(m_vehicle->GetNumWheels());
131 }
132
133
134 PyObject* KX_VehicleWrapper::PyGetConstraintId(PyObject* args)
135 {
136         return PyLong_FromSsize_t(m_vehicle->GetUserConstraintId());
137 }
138
139
140
141 PyObject* KX_VehicleWrapper::PyApplyEngineForce(PyObject* args)
142 {
143         float force;
144         int wheelIndex;
145
146         if (PyArg_ParseTuple(args,"fi:applyEngineForce",&force,&wheelIndex))
147         {
148                 force *= -1.f;//someone reverse some conventions inside Bullet (axle winding)
149                 m_vehicle->ApplyEngineForce(force,wheelIndex);
150         }
151         else {
152                 return NULL;
153         }
154         Py_RETURN_NONE;
155 }
156
157 PyObject* KX_VehicleWrapper::PySetTyreFriction(PyObject* args)
158 {
159         float wheelFriction;
160         int wheelIndex;
161
162         if (PyArg_ParseTuple(args,"fi:setTyreFriction",&wheelFriction,&wheelIndex))
163         {
164                 m_vehicle->SetWheelFriction(wheelFriction,wheelIndex);
165         }
166         else {
167                 return NULL;
168         }
169         Py_RETURN_NONE;
170 }
171
172 PyObject* KX_VehicleWrapper::PySetSuspensionStiffness(PyObject* args)
173 {
174         float suspensionStiffness;
175         int wheelIndex;
176
177         if (PyArg_ParseTuple(args,"fi:setSuspensionStiffness",&suspensionStiffness,&wheelIndex))
178         {
179                 m_vehicle->SetSuspensionStiffness(suspensionStiffness,wheelIndex);
180         }
181         else {
182                 return NULL;
183         }
184         Py_RETURN_NONE;
185 }
186
187 PyObject* KX_VehicleWrapper::PySetSuspensionDamping(PyObject* args)
188 {
189         float suspensionDamping;
190         int wheelIndex;
191
192         if (PyArg_ParseTuple(args,"fi:setSuspensionDamping",&suspensionDamping,&wheelIndex))
193         {
194                 m_vehicle->SetSuspensionDamping(suspensionDamping,wheelIndex);
195         } else {
196                 return NULL;
197         }
198         Py_RETURN_NONE;
199 }
200
201 PyObject* KX_VehicleWrapper::PySetSuspensionCompression(PyObject* args)
202 {
203         float suspensionCompression;
204         int wheelIndex;
205
206         if (PyArg_ParseTuple(args,"fi:setSuspensionCompression",&suspensionCompression,&wheelIndex))
207         {
208                 m_vehicle->SetSuspensionCompression(suspensionCompression,wheelIndex);
209         } else {
210                 return NULL;
211         }
212         Py_RETURN_NONE;
213 }
214
215 PyObject* KX_VehicleWrapper::PySetRollInfluence(PyObject* args)
216 {
217         float rollInfluence;
218         int wheelIndex;
219
220         if (PyArg_ParseTuple(args,"fi:setRollInfluence",&rollInfluence,&wheelIndex))
221         {
222                 m_vehicle->SetRollInfluence(rollInfluence,wheelIndex);
223         }
224         else {
225                 return NULL;
226         }
227         Py_RETURN_NONE;
228 }
229
230
231 PyObject* KX_VehicleWrapper::PyApplyBraking(PyObject* args)
232 {
233         float braking;
234         int wheelIndex;
235
236         if (PyArg_ParseTuple(args,"fi:applyBraking",&braking,&wheelIndex))
237         {
238                 m_vehicle->ApplyBraking(braking,wheelIndex);
239         }
240         else {
241                 return NULL;
242         }
243         Py_RETURN_NONE;
244 }
245
246
247
248
249 PyObject* KX_VehicleWrapper::PySetSteeringValue(PyObject* args)
250 {
251         float steeringValue;
252         int wheelIndex;
253
254         if (PyArg_ParseTuple(args,"fi:setSteeringValue",&steeringValue,&wheelIndex))
255         {
256                 m_vehicle->SetSteeringValue(steeringValue,wheelIndex);
257         }
258         else {
259                 return NULL;
260         }
261         Py_RETURN_NONE;
262 }
263
264
265 PyObject* KX_VehicleWrapper::PyGetConstraintType(PyObject* args)
266 {
267         return PyLong_FromSsize_t(m_vehicle->GetUserConstraintType());
268 }
269
270
271
272
273
274 //python specific stuff
275 PyTypeObject KX_VehicleWrapper::Type = {
276 #if (PY_VERSION_HEX >= 0x02060000)
277         PyVarObject_HEAD_INIT(NULL, 0)
278 #else
279         /* python 2.5 and below */
280         PyObject_HEAD_INIT( NULL )  /* required py macro */
281         0,                          /* ob_size */
282 #endif
283                 "KX_VehicleWrapper",
284                 sizeof(PyObjectPlus_Proxy),
285                 0,
286                 py_base_dealloc,
287                 0,
288                 0,
289                 0,
290                 0,
291                 py_base_repr,
292                 0,0,0,0,0,0,0,0,0,
293                 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
294                 0,0,0,0,0,0,0,
295                 Methods,
296                 0,
297                 0,
298                 &PyObjectPlus::Type,
299                 0,0,0,0,0,0,
300                 py_base_new
301 };
302
303 PyMethodDef KX_VehicleWrapper::Methods[] = {
304         {"addWheel",(PyCFunction) KX_VehicleWrapper::sPyAddWheel, METH_VARARGS},
305         {"getNumWheels",(PyCFunction) KX_VehicleWrapper::sPyGetNumWheels, METH_VARARGS},
306         {"getWheelOrientationQuaternion",(PyCFunction) KX_VehicleWrapper::sPyGetWheelOrientationQuaternion, METH_VARARGS},
307         {"getWheelRotation",(PyCFunction) KX_VehicleWrapper::sPyGetWheelRotation, METH_VARARGS},
308         {"getWheelPosition",(PyCFunction) KX_VehicleWrapper::sPyGetWheelPosition, METH_VARARGS},
309         {"getConstraintId",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintId, METH_VARARGS},
310         {"getConstraintType",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintType, METH_VARARGS},
311         {"setSteeringValue",(PyCFunction) KX_VehicleWrapper::sPySetSteeringValue, METH_VARARGS},
312         {"applyEngineForce",(PyCFunction) KX_VehicleWrapper::sPyApplyEngineForce, METH_VARARGS},
313         {"applyBraking",(PyCFunction) KX_VehicleWrapper::sPyApplyBraking, METH_VARARGS},
314
315         {"setTyreFriction",(PyCFunction) KX_VehicleWrapper::sPySetTyreFriction, METH_VARARGS},
316
317         {"setSuspensionStiffness",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionStiffness, METH_VARARGS},
318
319         {"setSuspensionDamping",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionDamping, METH_VARARGS},
320
321         {"setSuspensionCompression",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionCompression, METH_VARARGS},
322
323         {"setRollInfluence",(PyCFunction) KX_VehicleWrapper::sPySetRollInfluence, METH_VARARGS},
324
325         {NULL,NULL} //Sentinel
326 };
327
328 PyAttributeDef KX_VehicleWrapper::Attributes[] = {
329         { NULL }        //Sentinel
330 };