Blender V2.61 - r43446

KX_VehicleWrapper.cpp

Go to the documentation of this file.
00001 
00006 #include "PyObjectPlus.h"
00007 
00008 #include "KX_VehicleWrapper.h"
00009 #include "PHY_IPhysicsEnvironment.h"
00010 #include "PHY_IVehicle.h"
00011 #include "KX_PyMath.h"
00012 #include "KX_GameObject.h"
00013 #include "KX_MotionState.h"
00014 
00015 KX_VehicleWrapper::KX_VehicleWrapper(
00016                         PHY_IVehicle* vehicle,
00017                         PHY_IPhysicsEnvironment* physenv) :
00018         PyObjectPlus(),
00019         m_vehicle(vehicle),
00020         m_physenv(physenv)
00021 {
00022 }
00023 
00024 KX_VehicleWrapper::~KX_VehicleWrapper()
00025 {
00026     int numMotion = m_motionStates.size();
00027     for (int i=0;i<numMotion;i++)
00028     {
00029         PHY_IMotionState* motionState = m_motionStates[i];
00030         delete motionState;
00031     }
00032     m_motionStates.clear();
00033 }
00034 
00035 #ifdef WITH_PYTHON
00036 
00037 PyObject* KX_VehicleWrapper::PyAddWheel(PyObject* args)
00038 {
00039     
00040     PyObject* pylistPos,*pylistDir,*pylistAxleDir;
00041     PyObject* wheelGameObject;
00042     float suspensionRestLength,wheelRadius;
00043     int hasSteering;
00044 
00045     
00046     if (PyArg_ParseTuple(args,"OOOOffi:addWheel",&wheelGameObject,&pylistPos,&pylistDir,&pylistAxleDir,&suspensionRestLength,&wheelRadius,&hasSteering))
00047     {
00048         KX_GameObject *gameOb;
00049         if (!ConvertPythonToGameObject(wheelGameObject, &gameOb, false, "vehicle.addWheel(...): KX_VehicleWrapper (first argument)"))
00050             return NULL;
00051         
00052 
00053         if (gameOb->GetSGNode())
00054         {
00055             PHY_IMotionState* motionState = new KX_MotionState(gameOb->GetSGNode());
00056             
00057             /* TODO - no error checking here! - bad juju */
00058             MT_Vector3 attachPos,attachDir,attachAxle;
00059             PyVecTo(pylistPos,attachPos);
00060             PyVecTo(pylistDir,attachDir);
00061             PyVecTo(pylistAxleDir,attachAxle);
00062             PHY__Vector3 aPos,aDir,aAxle;
00063             aPos[0] = attachPos[0];
00064             aPos[1] = attachPos[1];
00065             aPos[2] = attachPos[2];
00066             aDir[0] = attachDir[0];
00067             aDir[1] = attachDir[1];
00068             aDir[2] = attachDir[2];
00069             aAxle[0] = -attachAxle[0];//someone reverse some conventions inside Bullet (axle winding)
00070             aAxle[1] = -attachAxle[1];
00071             aAxle[2] = -attachAxle[2];
00072             
00073             printf("attempt for addWheel: suspensionRestLength%f wheelRadius %f, hasSteering:%d\n",suspensionRestLength,wheelRadius,hasSteering);
00074             m_vehicle->AddWheel(motionState,aPos,aDir,aAxle,suspensionRestLength,wheelRadius,hasSteering);
00075         }
00076         
00077     } else {
00078         return NULL;
00079     }
00080     Py_RETURN_NONE;
00081 }
00082 
00083 
00084 
00085 
00086 PyObject* KX_VehicleWrapper::PyGetWheelPosition(PyObject* args)
00087 {
00088     
00089     int wheelIndex;
00090 
00091     if (PyArg_ParseTuple(args,"i:getWheelPosition",&wheelIndex))
00092     {
00093         float position[3];
00094         m_vehicle->GetWheelPosition(wheelIndex,position[0],position[1],position[2]);
00095         MT_Vector3 pos(position[0],position[1],position[2]);
00096         return PyObjectFrom(pos);
00097     }
00098     return NULL;
00099 }
00100 
00101 PyObject* KX_VehicleWrapper::PyGetWheelRotation(PyObject* args)
00102 {
00103     int wheelIndex;
00104     if (PyArg_ParseTuple(args,"i:getWheelRotation",&wheelIndex))
00105     {
00106         return PyFloat_FromDouble(m_vehicle->GetWheelRotation(wheelIndex));
00107     }
00108     return NULL;
00109 }
00110 
00111 PyObject* KX_VehicleWrapper::PyGetWheelOrientationQuaternion(PyObject* args)
00112 {
00113     int wheelIndex;
00114     if (PyArg_ParseTuple(args,"i:getWheelOrientationQuaternion",&wheelIndex))
00115     {
00116         float orn[4];
00117         m_vehicle->GetWheelOrientationQuaternion(wheelIndex,orn[0],orn[1],orn[2],orn[3]);
00118         MT_Quaternion   quatorn(orn[0],orn[1],orn[2],orn[3]);
00119         MT_Matrix3x3 ornmat(quatorn);
00120         return PyObjectFrom(ornmat);
00121     }
00122     return NULL;
00123 
00124 }
00125 
00126 
00127 PyObject* KX_VehicleWrapper::PyGetNumWheels(PyObject* args)
00128 {
00129     return PyLong_FromSsize_t(m_vehicle->GetNumWheels());
00130 }
00131 
00132 
00133 PyObject* KX_VehicleWrapper::PyGetConstraintId(PyObject* args)
00134 {
00135     return PyLong_FromSsize_t(m_vehicle->GetUserConstraintId());
00136 }
00137 
00138 
00139 
00140 PyObject* KX_VehicleWrapper::PyApplyEngineForce(PyObject* args)
00141 {
00142     float force;
00143     int wheelIndex;
00144 
00145     if (PyArg_ParseTuple(args,"fi:applyEngineForce",&force,&wheelIndex))
00146     {
00147         force *= -1.f;//someone reverse some conventions inside Bullet (axle winding)
00148         m_vehicle->ApplyEngineForce(force,wheelIndex);
00149     }
00150     else {
00151         return NULL;
00152     }
00153     Py_RETURN_NONE;
00154 }
00155 
00156 PyObject* KX_VehicleWrapper::PySetTyreFriction(PyObject* args)
00157 {
00158     float wheelFriction;
00159     int wheelIndex;
00160 
00161     if (PyArg_ParseTuple(args,"fi:setTyreFriction",&wheelFriction,&wheelIndex))
00162     {
00163         m_vehicle->SetWheelFriction(wheelFriction,wheelIndex);
00164     }
00165     else {
00166         return NULL;
00167     }
00168     Py_RETURN_NONE;
00169 }
00170 
00171 PyObject* KX_VehicleWrapper::PySetSuspensionStiffness(PyObject* args)
00172 {
00173     float suspensionStiffness;
00174     int wheelIndex;
00175 
00176     if (PyArg_ParseTuple(args,"fi:setSuspensionStiffness",&suspensionStiffness,&wheelIndex))
00177     {
00178         m_vehicle->SetSuspensionStiffness(suspensionStiffness,wheelIndex);
00179     }
00180     else {
00181         return NULL;
00182     }
00183     Py_RETURN_NONE;
00184 }
00185 
00186 PyObject* KX_VehicleWrapper::PySetSuspensionDamping(PyObject* args)
00187 {
00188     float suspensionDamping;
00189     int wheelIndex;
00190 
00191     if (PyArg_ParseTuple(args,"fi:setSuspensionDamping",&suspensionDamping,&wheelIndex))
00192     {
00193         m_vehicle->SetSuspensionDamping(suspensionDamping,wheelIndex);
00194     } else {
00195         return NULL;
00196     }
00197     Py_RETURN_NONE;
00198 }
00199 
00200 PyObject* KX_VehicleWrapper::PySetSuspensionCompression(PyObject* args)
00201 {
00202     float suspensionCompression;
00203     int wheelIndex;
00204 
00205     if (PyArg_ParseTuple(args,"fi:setSuspensionCompression",&suspensionCompression,&wheelIndex))
00206     {
00207         m_vehicle->SetSuspensionCompression(suspensionCompression,wheelIndex);
00208     } else {
00209         return NULL;
00210     }
00211     Py_RETURN_NONE;
00212 }
00213 
00214 PyObject* KX_VehicleWrapper::PySetRollInfluence(PyObject* args)
00215 {
00216     float rollInfluence;
00217     int wheelIndex;
00218 
00219     if (PyArg_ParseTuple(args,"fi:setRollInfluence",&rollInfluence,&wheelIndex))
00220     {
00221         m_vehicle->SetRollInfluence(rollInfluence,wheelIndex);
00222     }
00223     else {
00224         return NULL;
00225     }
00226     Py_RETURN_NONE;
00227 }
00228 
00229 
00230 PyObject* KX_VehicleWrapper::PyApplyBraking(PyObject* args)
00231 {
00232     float braking;
00233     int wheelIndex;
00234 
00235     if (PyArg_ParseTuple(args,"fi:applyBraking",&braking,&wheelIndex))
00236     {
00237         m_vehicle->ApplyBraking(braking,wheelIndex);
00238     }
00239     else {
00240         return NULL;
00241     }
00242     Py_RETURN_NONE;
00243 }
00244 
00245 
00246 
00247 
00248 PyObject* KX_VehicleWrapper::PySetSteeringValue(PyObject* args)
00249 {
00250     float steeringValue;
00251     int wheelIndex;
00252 
00253     if (PyArg_ParseTuple(args,"fi:setSteeringValue",&steeringValue,&wheelIndex))
00254     {
00255         m_vehicle->SetSteeringValue(steeringValue,wheelIndex);
00256     }
00257     else {
00258         return NULL;
00259     }
00260     Py_RETURN_NONE;
00261 }
00262 
00263 
00264 PyObject* KX_VehicleWrapper::PyGetConstraintType(PyObject* args)
00265 {
00266     return PyLong_FromSsize_t(m_vehicle->GetUserConstraintType());
00267 }
00268 
00269 
00270 
00271 
00272 
00273 //python specific stuff
00274 PyTypeObject KX_VehicleWrapper::Type = {
00275     PyVarObject_HEAD_INIT(NULL, 0)
00276     "KX_VehicleWrapper",
00277     sizeof(PyObjectPlus_Proxy),
00278     0,
00279     py_base_dealloc,
00280     0,
00281     0,
00282     0,
00283     0,
00284     py_base_repr,
00285     0,0,0,0,0,0,0,0,0,
00286     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00287     0,0,0,0,0,0,0,
00288     Methods,
00289     0,
00290     0,
00291     &PyObjectPlus::Type,
00292     0,0,0,0,0,0,
00293     py_base_new
00294 };
00295 
00296 PyMethodDef KX_VehicleWrapper::Methods[] = {
00297     {"addWheel",(PyCFunction) KX_VehicleWrapper::sPyAddWheel, METH_VARARGS},
00298     {"getNumWheels",(PyCFunction) KX_VehicleWrapper::sPyGetNumWheels, METH_VARARGS},
00299     {"getWheelOrientationQuaternion",(PyCFunction) KX_VehicleWrapper::sPyGetWheelOrientationQuaternion, METH_VARARGS},
00300     {"getWheelRotation",(PyCFunction) KX_VehicleWrapper::sPyGetWheelRotation, METH_VARARGS},
00301     {"getWheelPosition",(PyCFunction) KX_VehicleWrapper::sPyGetWheelPosition, METH_VARARGS},
00302     {"getConstraintId",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintId, METH_VARARGS},
00303     {"getConstraintType",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintType, METH_VARARGS},
00304     {"setSteeringValue",(PyCFunction) KX_VehicleWrapper::sPySetSteeringValue, METH_VARARGS},
00305     {"applyEngineForce",(PyCFunction) KX_VehicleWrapper::sPyApplyEngineForce, METH_VARARGS},
00306     {"applyBraking",(PyCFunction) KX_VehicleWrapper::sPyApplyBraking, METH_VARARGS},
00307 
00308     {"setTyreFriction",(PyCFunction) KX_VehicleWrapper::sPySetTyreFriction, METH_VARARGS},
00309 
00310     {"setSuspensionStiffness",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionStiffness, METH_VARARGS},
00311 
00312     {"setSuspensionDamping",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionDamping, METH_VARARGS},
00313 
00314     {"setSuspensionCompression",(PyCFunction) KX_VehicleWrapper::sPySetSuspensionCompression, METH_VARARGS},
00315 
00316     {"setRollInfluence",(PyCFunction) KX_VehicleWrapper::sPySetRollInfluence, METH_VARARGS},
00317 
00318     {NULL,NULL} //Sentinel
00319 };
00320 
00321 PyAttributeDef KX_VehicleWrapper::Attributes[] = {
00322     { NULL }    //Sentinel
00323 };
00324 
00325 #endif // WITH_PYTHON