4 #include "PyObjectPlus.h"
6 #include "KX_VehicleWrapper.h"
7 #include "PHY_IPhysicsEnvironment.h"
8 #include "PHY_IVehicle.h"
10 #include "KX_GameObject.h"
11 #include "KX_MotionState.h"
17 KX_VehicleWrapper::KX_VehicleWrapper(
18 PHY_IVehicle* vehicle,
19 PHY_IPhysicsEnvironment* physenv,PyTypeObject *T) :
26 KX_VehicleWrapper::~KX_VehicleWrapper()
28 int numMotion = m_motionStates.size();
29 for (int i=0;i<numMotion;i++)
31 PHY_IMotionState* motionState = m_motionStates[i];
34 m_motionStates.clear();
38 PyObject* KX_VehicleWrapper::PyAddWheel(PyObject* self,
43 PyObject* pylistPos,*pylistDir,*pylistAxleDir;
44 PyObject* wheelGameObject;
45 float suspensionRestLength,wheelRadius;
49 if (PyArg_ParseTuple(args,"OOOOffi:addWheel",&wheelGameObject,&pylistPos,&pylistDir,&pylistAxleDir,&suspensionRestLength,&wheelRadius,&hasSteering))
51 KX_GameObject* gameOb = (KX_GameObject*) wheelGameObject;
53 if (gameOb->GetSGNode())
55 PHY_IMotionState* motionState = new KX_MotionState(gameOb->GetSGNode());
57 MT_Vector3 attachPos,attachDir,attachAxle;
58 PyVecTo(pylistPos,attachPos);
59 PyVecTo(pylistDir,attachDir);
60 PyVecTo(pylistAxleDir,attachAxle);
61 PHY__Vector3 aPos,aDir,aAxle;
62 aPos[0] = attachPos[0];
63 aPos[1] = attachPos[1];
64 aPos[2] = attachPos[2];
65 aDir[0] = attachDir[0];
66 aDir[1] = attachDir[1];
67 aDir[2] = attachDir[2];
68 aAxle[0] = -attachAxle[0];//someone reverse some conventions inside Bullet (axle winding)
69 aAxle[1] = -attachAxle[1];
70 aAxle[2] = -attachAxle[2];
72 printf("attempt for addWheel: suspensionRestLength%f wheelRadius %f, hasSteering:%d\n",suspensionRestLength,wheelRadius,hasSteering);
73 m_vehicle->AddWheel(motionState,aPos,aDir,aAxle,suspensionRestLength,wheelRadius,hasSteering);
85 PyObject* KX_VehicleWrapper::PyGetWheelPosition(PyObject* self,
92 if (PyArg_ParseTuple(args,"i:getWheelPosition",&wheelIndex))
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);
102 PyObject* KX_VehicleWrapper::PyGetWheelRotation(PyObject* self,
107 if (PyArg_ParseTuple(args,"i:getWheelRotation",&wheelIndex))
109 return PyFloat_FromDouble(m_vehicle->GetWheelRotation(wheelIndex));
114 PyObject* KX_VehicleWrapper::PyGetWheelOrientationQuaternion(PyObject* self,
119 if (PyArg_ParseTuple(args,"i:getWheelOrientationQuaternion",&wheelIndex))
122 m_vehicle->GetWheelOrientationQuaternion(wheelIndex,orn[0],orn[1],orn[2],orn[3]);
123 MT_Quaternion quatorn(orn[0],orn[1],orn[2],orn[3]);
124 MT_Matrix3x3 ornmat(quatorn);
125 return PyObjectFrom(ornmat);
132 PyObject* KX_VehicleWrapper::PyGetNumWheels(PyObject* self,
136 return PyInt_FromLong(m_vehicle->GetNumWheels());
140 PyObject* KX_VehicleWrapper::PyGetConstraintId(PyObject* self,
144 return PyInt_FromLong(m_vehicle->GetUserConstraintId());
149 PyObject* KX_VehicleWrapper::PyApplyEngineForce(PyObject* self,
156 if (PyArg_ParseTuple(args,"fi:applyEngineForce",&force,&wheelIndex))
158 force *= -1.f;//someone reverse some conventions inside Bullet (axle winding)
159 m_vehicle->ApplyEngineForce(force,wheelIndex);
167 PyObject* KX_VehicleWrapper::PySetTyreFriction(PyObject* self,
174 if (PyArg_ParseTuple(args,"fi:setTyreFriction",&wheelFriction,&wheelIndex))
176 m_vehicle->SetWheelFriction(wheelFriction,wheelIndex);
184 PyObject* KX_VehicleWrapper::PySetSuspensionStiffness(PyObject* self,
188 float suspensionStiffness;
191 if (PyArg_ParseTuple(args,"fi:setSuspensionStiffness",&suspensionStiffness,&wheelIndex))
193 m_vehicle->SetSuspensionStiffness(suspensionStiffness,wheelIndex);
201 PyObject* KX_VehicleWrapper::PySetSuspensionDamping(PyObject* self,
205 float suspensionDamping;
208 if (PyArg_ParseTuple(args,"fi:setSuspensionDamping",&suspensionDamping,&wheelIndex))
210 m_vehicle->SetSuspensionDamping(suspensionDamping,wheelIndex);
217 PyObject* KX_VehicleWrapper::PySetSuspensionCompression(PyObject* self,
221 float suspensionCompression;
224 if (PyArg_ParseTuple(args,"fi:setSuspensionCompression",&suspensionCompression,&wheelIndex))
226 m_vehicle->SetSuspensionCompression(suspensionCompression,wheelIndex);
233 PyObject* KX_VehicleWrapper::PySetRollInfluence(PyObject* self,
240 if (PyArg_ParseTuple(args,"fi:setRollInfluence",&rollInfluence,&wheelIndex))
242 m_vehicle->SetRollInfluence(rollInfluence,wheelIndex);
251 PyObject* KX_VehicleWrapper::PyApplyBraking(PyObject* self,
258 if (PyArg_ParseTuple(args,"fi:applyBraking",&braking,&wheelIndex))
260 m_vehicle->ApplyBraking(braking,wheelIndex);
271 PyObject* KX_VehicleWrapper::PySetSteeringValue(PyObject* self,
278 if (PyArg_ParseTuple(args,"fi:setSteeringValue",&steeringValue,&wheelIndex))
280 m_vehicle->SetSteeringValue(steeringValue,wheelIndex);
289 PyObject* KX_VehicleWrapper::PyGetConstraintType(PyObject* self,
293 return PyInt_FromLong(m_vehicle->GetUserConstraintType());
300 //python specific stuff
301 PyTypeObject KX_VehicleWrapper::Type = {
302 PyObject_HEAD_INIT(NULL)
305 sizeof(KX_VehicleWrapper),
320 PyParentObject KX_VehicleWrapper::Parents[] = {
321 &KX_VehicleWrapper::Type,
326 PyObject* KX_VehicleWrapper::py_getattro(PyObject *attr)
328 //here you can search for existing data members (like mass,friction etc.)
329 py_getattro_up(PyObjectPlus);
332 int KX_VehicleWrapper::py_setattro(PyObject *attr,PyObject* pyobj)
334 /* TODO - strange setattr, needs updating */
335 PyTypeObject* type = pyobj->ob_type;
338 if (type == &PyList_Type)
342 if (type == &PyFloat_Type)
347 if (type == &PyInt_Type)
351 if (type == &PyString_Type)
356 result = PyObjectPlus::py_setattro(attr,pyobj);
361 PyMethodDef KX_VehicleWrapper::Methods[] = {
362 {"addWheel",(PyCFunction) KX_VehicleWrapper::sPyAddWheel, METH_VARARGS},
363 {"getNumWheels",(PyCFunction) KX_VehicleWrapper::sPyGetNumWheels, METH_VARARGS},
364 {"getWheelOrientationQuaternion",(PyCFunction) KX_VehicleWrapper::sPyGetWheelOrientationQuaternion, METH_VARARGS},
365 {"getWheelRotation",(PyCFunction) KX_VehicleWrapper::sPyGetWheelRotation, METH_VARARGS},
366 {"getWheelPosition",(PyCFunction) KX_VehicleWrapper::sPyGetWheelPosition, METH_VARARGS},
367 {"getConstraintId",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintId, METH_VARARGS},
368 {"getConstraintType",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintType, METH_VARARGS},
369 {"setSteeringValue",(PyCFunction) KX_VehicleWrapper::sPySetSteeringValue, METH_VARARGS},
370 {"applyEngineForce",(PyCFunction) KX_VehicleWrapper::sPyApplyEngineForce, METH_VARARGS},
371 {"applyBraking",(PyCFunction) KX_VehicleWrapper::sPyApplyBraking, METH_VARARGS},
373 {"setTyreFriction",(PyCFunction) KX_VehicleWrapper::sPySetTyreFriction, METH_VARARGS},
375 {"setSuspensionStiffness",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionStiffness, METH_VARARGS},
377 {"setSuspensionDamping",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionDamping, METH_VARARGS},
379 {"setSuspensionCompression",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionCompression, METH_VARARGS},
381 {"setRollInfluence",(PyCFunction) KX_VehicleWrapper::sPySetRollInfluence, METH_VARARGS},
383 {NULL,NULL} //Sentinel
386 PyAttributeDef KX_VehicleWrapper::Attributes[] = {