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