prepared physics/game engine infrastructure for vehicle support.
authorErwin Coumans <blender@erwincoumans.com>
Mon, 30 Jan 2006 20:33:59 +0000 (20:33 +0000)
committerErwin Coumans <blender@erwincoumans.com>
Mon, 30 Jan 2006 20:33:59 +0000 (20:33 +0000)
fixed a python related bug with physics contraints
fixed some line-ending problem with blenderbuttons.c

makefile/scons/projectfiles need to add source/gameengine/Ketsji/KX_VehicleWrapper.cpp

source/gameengine/Expressions/PyObjectPlus.cpp
source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
source/gameengine/Ketsji/KX_VehicleWrapper.cpp [new file with mode: 0644]
source/gameengine/Ketsji/KX_VehicleWrapper.h [new file with mode: 0644]
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h
source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h
source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h
source/gameengine/Physics/common/PHY_DynamicTypes.h
source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h

index 55cd76824c95d6c4bba9e96bfc9794c1bfc6a9e4..1b343088446ad8c5d0150f404a6b989228985892 100644 (file)
@@ -79,7 +79,10 @@ PyTypeObject PyObjectPlus::Type = {
 
 PyObjectPlus::~PyObjectPlus()
 {
-       _Py_ForgetReference(this);
+       if (ob_refcnt)
+       {
+               _Py_ForgetReference(this);
+       }
 //     assert(ob_refcnt==0);
 }
 
index 0e7ebc6442e4e8b91e39fb89640b0f4dfc367b32..e9c71fe02c96684e62c467a5f375778e23d58cbe 100644 (file)
 #include "KX_PyConstraintBinding.h"
 #include "PHY_IPhysicsEnvironment.h"
 #include "KX_ConstraintWrapper.h"
+#include "KX_VehicleWrapper.h"
 #include "KX_PhysicsObjectWrapper.h"
 #include "PHY_IPhysicsController.h"
+#include "PHY_IVehicle.h"
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -66,7 +68,8 @@ static char gPySetSolverType__doc__[] = "setSolverType(int solverType) Very expe
 
 
 static char gPyCreateConstraint__doc__[] = "createConstraint(ob1,ob2,float restLength,float restitution,float damping)";
-static char gPyRemoveConstraint__doc__[] = "removeConstraint(constraint id)";
+static char gPyGetVehicleConstraint__doc__[] = "getVehicleConstraint(int constraintId)";
+static char gPyRemoveConstraint__doc__[] = "removeConstraint(int constraintId)";
 
 
 
@@ -291,6 +294,35 @@ static PyObject* gPySetSolverType(PyObject* self,
 
 
 
+static PyObject* gPyGetVehicleConstraint(PyObject* self,
+                                                                                PyObject* args, 
+                                                                                PyObject* kwds)
+{
+#if defined(_WIN64)
+       __int64 constraintid;
+       if (PyArg_ParseTuple(args,"L",&constraintid))
+#else
+       long constraintid;
+       if (PyArg_ParseTuple(args,"l",&constraintid))
+#endif
+       {
+               if (PHY_GetActiveEnvironment())
+               {
+                       
+                       PHY_IVehicle* vehicle = PHY_GetActiveEnvironment()->getVehicleConstraint(constraintid);
+                       if (vehicle)
+                       {
+                               KX_VehicleWrapper* pyWrapper = new KX_VehicleWrapper(vehicle,PHY_GetActiveEnvironment());
+                               return pyWrapper;
+                       }
+
+               }
+       }
+
+       Py_INCREF(Py_None); return Py_None;
+}
+
+
 
 
 
@@ -407,9 +439,11 @@ static struct PyMethodDef physicsconstraints_methods[] = {
    METH_VARARGS, gPySetSolverType__doc__},
 
 
-
   {"createConstraint",(PyCFunction) gPyCreateConstraint,
    METH_VARARGS, gPyCreateConstraint__doc__},
+     {"getVehicleConstraint",(PyCFunction) gPyGetVehicleConstraint,
+   METH_VARARGS, gPyGetVehicleConstraint__doc__},
+
   {"removeConstraint",(PyCFunction) gPyRemoveConstraint,
    METH_VARARGS, gPyRemoveConstraint__doc__},
 
diff --git a/source/gameengine/Ketsji/KX_VehicleWrapper.cpp b/source/gameengine/Ketsji/KX_VehicleWrapper.cpp
new file mode 100644 (file)
index 0000000..299a2d1
--- /dev/null
@@ -0,0 +1,140 @@
+
+
+#include <Python.h>
+#include "KX_VehicleWrapper.h"
+#include "PHY_IPhysicsEnvironment.h"
+#include "PHY_IVehicle.h"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+KX_VehicleWrapper::KX_VehicleWrapper(
+                                               PHY_IVehicle* vehicle,
+                                               PHY_IPhysicsEnvironment* physenv,PyTypeObject *T) :
+               PyObjectPlus(T),
+               m_vehicle(vehicle),
+               m_physenv(physenv)
+{
+}
+
+KX_VehicleWrapper::~KX_VehicleWrapper()
+{
+}
+
+
+PyObject* KX_VehicleWrapper::PyAddWheel(PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds)
+{
+       Py_INCREF(Py_None);
+       return Py_None;
+}
+
+
+
+PyObject* KX_VehicleWrapper::PyGetWheelsTransform(PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds)
+{
+       assert(0);
+       return PyInt_FromLong(m_vehicle->GetNumWheels());
+}
+
+
+PyObject* KX_VehicleWrapper::PyGetNumWheels(PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds)
+{
+       return PyInt_FromLong(m_vehicle->GetNumWheels());
+}
+
+
+PyObject* KX_VehicleWrapper::PyGetConstraintId(PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds)
+{
+       return PyInt_FromLong(m_vehicle->GetUserConstraintId());
+}
+
+PyObject* KX_VehicleWrapper::PyGetConstraintType(PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds)
+{
+       return PyInt_FromLong(m_vehicle->GetUserConstraintType());
+}
+
+
+
+
+//python specific stuff
+PyTypeObject KX_VehicleWrapper::Type = {
+       PyObject_HEAD_INIT(&PyType_Type)
+               0,
+               "KX_VehicleWrapper",
+               sizeof(KX_VehicleWrapper),
+               0,
+               PyDestructor,
+               0,
+               __getattr,
+               __setattr,
+               0, //&MyPyCompare,
+               __repr,
+               0, //&cvalue_as_number,
+               0,
+               0,
+               0,
+               0
+};
+
+PyParentObject KX_VehicleWrapper::Parents[] = {
+       &KX_VehicleWrapper::Type,
+       NULL
+};
+
+PyObject*      KX_VehicleWrapper::_getattr(const STR_String& attr)
+{
+       //here you can search for existing data members (like mass,friction etc.)
+       _getattr_up(PyObjectPlus);
+}
+
+int    KX_VehicleWrapper::_setattr(const STR_String& attr,PyObject* pyobj)
+{
+       
+       PyTypeObject* type = pyobj->ob_type;
+       int result = 1;
+
+       if (type == &PyList_Type)
+       {
+               result = 0;
+       }
+       if (type == &PyFloat_Type)
+       {
+               result = 0;
+
+       }
+       if (type == &PyInt_Type)
+       {
+               result = 0;
+       }
+       if (type == &PyString_Type)
+       {
+               result = 0;
+       }
+       if (result)
+               result = PyObjectPlus::_setattr(attr,pyobj);
+       return result;
+};
+
+
+PyMethodDef KX_VehicleWrapper::Methods[] = {
+       {"addWheel",(PyCFunction) KX_VehicleWrapper::sPyAddWheel, METH_VARARGS},
+       {"getNumWheels",(PyCFunction) KX_VehicleWrapper::sPyGetNumWheels, METH_VARARGS},
+       {"getWheelsTransform",(PyCFunction) KX_VehicleWrapper::sPyGetWheelsTransform, METH_VARARGS},
+       {"getConstraintId",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintId, METH_VARARGS},
+       {"getConstraintType",(PyCFunction) KX_VehicleWrapper::sPyGetConstraintType, METH_VARARGS},
+       {NULL,NULL} //Sentinel
+};
+
+
+
diff --git a/source/gameengine/Ketsji/KX_VehicleWrapper.h b/source/gameengine/Ketsji/KX_VehicleWrapper.h
new file mode 100644 (file)
index 0000000..ebdf1b0
--- /dev/null
@@ -0,0 +1,33 @@
+#ifndef KX_VEHICLE_WRAPPER
+#define KX_VEHICLE_WRAPPER
+
+#include "Value.h"
+#include "PHY_DynamicTypes.h"
+class PHY_IVehicle;
+
+///Python interface to physics vehicles (primarily 4-wheel cars and 2wheel bikes)
+class  KX_VehicleWrapper : public PyObjectPlus
+{
+       Py_Header;
+       virtual PyObject*               _getattr(const STR_String& attr);
+       virtual int                     _setattr(const STR_String& attr, PyObject *value);
+public:
+       KX_VehicleWrapper(PHY_IVehicle* vehicle,class PHY_IPhysicsEnvironment* physenv,PyTypeObject *T = &Type);
+       virtual ~KX_VehicleWrapper ();
+       int                     getConstraintId();
+       
+       
+       KX_PYMETHOD(KX_VehicleWrapper,AddWheel);
+       KX_PYMETHOD(KX_VehicleWrapper,GetNumWheels);
+       KX_PYMETHOD(KX_VehicleWrapper,GetWheelsTransform);
+       
+       KX_PYMETHOD(KX_VehicleWrapper,GetConstraintId);
+       KX_PYMETHOD(KX_VehicleWrapper,GetConstraintType);
+
+
+private:
+       PHY_IVehicle*                    m_vehicle;
+       PHY_IPhysicsEnvironment* m_physenv;
+};
+
+#endif //KX_VEHICLE_WRAPPER
index 1b2527aa5bbc3cd414e6af73cf73a8dbbe29a744..a599968a3057de0e831186b9f8f464f28beb42db 100644 (file)
@@ -79,6 +79,11 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
                        float axisX,float axisY,float axisZ);
            virtual void                removeConstraint(int    constraintid);
 
+               //complex constraint for vehicles
+               virtual PHY_IVehicle*   getVehicleConstraint(int constraintId)
+               {
+                       return 0;
+               }
 
                virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
                                                                                float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
index a400459afe68e49d7f245c226b8d9d1d7776ac5d..278f4eea9cab32b1b91b17a8ab008534f0f181db 100644 (file)
@@ -62,6 +62,13 @@ public:
                        float axisX,float axisY,float axisZ);
 
        virtual void            removeConstraint(int    constraintid);
+
+               //complex constraint for vehicles
+       virtual PHY_IVehicle*   getVehicleConstraint(int constraintId)
+       {
+               return 0;
+       }
+
        virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
                                                                        float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
 
index e164140716639161145d93456a20ccfe95801694..886bc8bb5fd7f5c4d5583563489978236feb414c 100644 (file)
@@ -67,6 +67,13 @@ public:
                        float axisX,float axisY,float axisZ);
 
        virtual void            removeConstraint(int    constraintid);
+
+                       //complex constraint for vehicles
+       virtual PHY_IVehicle*   getVehicleConstraint(int constraintId)
+       {
+               return 0;
+       }
+
        virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient,float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
                                                                        float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
 
index 340e4b214674be1bb8b547c8ab971bbe1ed96f7f..e247a8c5f2ac1f2bdf2be933817a3758a13cc2ec 100644 (file)
@@ -98,7 +98,8 @@ typedef enum PHY_PhysicsType {
 /// PHY_ConstraintType enumerates all supported Constraint Types
 typedef enum PHY_ConstraintType {
        PHY_POINT2POINT_CONSTRAINT=1,
-       PHY_LINEHINGE_CONSTRAINT
+       PHY_LINEHINGE_CONSTRAINT=2,
+       PHY_VEHICLE_CONSTRAINT=11,//complex 'constraint' that turns a rigidbody into a vehicle
 
 } PHY_ConstraintType;
 
index 078f84d4de27cce0fb092706c84e2e5b98f4c23a..6b40b51eba70de1b33d54c6491f6519e23771951 100644 (file)
  *
  * ***** END GPL/BL DUAL LICENSE BLOCK *****
  */
+
 #ifndef _IPHYSICSENVIRONMENT
 #define _IPHYSICSENVIRONMENT
 
 #include <vector>
 #include "PHY_DynamicTypes.h"
+class PHY_IVehicle;
 
 /**
 *      Physics Environment takes care of stepping the simulation and is a container for physics entities (rigidbodies,constraints, materials etc.)
@@ -85,6 +87,9 @@ class PHY_IPhysicsEnvironment
                        float axisX,float axisY,float axisZ)=0;
                virtual void            removeConstraint(int    constraintid)=0;
 
+               //complex constraint for vehicles
+               virtual PHY_IVehicle*   getVehicleConstraint(int constraintId) =0;
+
                virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
                                                                                float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ)=0;