Skip to content

Commit

Permalink
UPBGE: Refactor physics constraints initialization and python access.
Browse files Browse the repository at this point in the history
Previously two issues were noticed in the sources, first the constraint were
were not represented by an object and so the attribute were modified by the
physics environment functions with as first argument the constraint id, e.g:
SetConstraintParam/GetConstraintParam.
Second, the vehicle constraint were created in the physics environment by
the same function than regular constraitn even if they behave differntly.

To fix these both issue the constraint are not represented by a new class
named PHY_IConstraint inherited in CcdConstraint for bullet constraint, this
class contains function to set and get paramater, get id and type without
any dependencies to the physics environment.
The physics environment is now returing an instance of PHY_IConstraint in
CreateConstraint and a PHY_IVehicle in the new function CreateVehicle.
This last function is called in python thanks to the new function:
bge.constraint.createVehicle(chassisPhysicsId)

The two constraint python proxy classes KX_ConstraintWrapper and KX_VehicleWrapper
now take as constructor respectively PHY_IConstraint and PHY_IVehicle and so
remove the dependency to PHY_IPhsicsEnvironment.

To keep the compatitliby the vehicle wrapper define the attribute constraint_id
and constrant_type (sorry sdfgeoff :)) to look as a property and be still useable
with contraint.getVehicleConstraint. In the same time constraints.createConstraint
return a KX_VehicleWrapper when the type specified is PHY_VEHICLE_CONSTRAINT.

Note that the file in Physics/Common are now targeted in a separate cmake in
the same directory isntead of in the cmake of Ketsji/
  • Loading branch information
panzergame committed Jul 18, 2017
1 parent 676f604 commit 3341047
Show file tree
Hide file tree
Showing 19 changed files with 459 additions and 367 deletions.
10 changes: 10 additions & 0 deletions doc/python_api/rst/bge.constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ Functions
:return: A constraint wrapper.
:rtype: :class:`~bge.types.KX_ConstraintWrapper`

.. function:: createVehicle(physicsid)

Creates a vehicle constraint.

:arg physicsid: The physics id of the chassis object in constraint.
:type physicsid: int

:return: A vehicle constraint wrapper.
:rtype: :class:`~bge.types.KX_VehicleWrapper`

.. function:: exportBulletFile(filename)

Exports a file representing the dynamics world (usually using ``.bullet`` extension).
Expand Down
12 changes: 0 additions & 12 deletions source/gameengine/Ketsji/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,6 @@ set(SRC
KX_WorldInfo.h
KX_WorldIpoController.h
KX_CollisionContactPoints.h

# orphan headers (not apart of a library)
../Physics/Common/PHY_DynamicTypes.h
../Physics/Common/PHY_ICharacter.h
../Physics/Common/PHY_IController.h
../Physics/Common/PHY_IGraphicController.h
../Physics/Common/PHY_IMotionState.h
../Physics/Common/PHY_IPhysicsController.h
../Physics/Common/PHY_IPhysicsEnvironment.h
../Physics/Common/PHY_IVehicle.h
../Physics/Common/PHY_Pro.h

)

add_definitions(${GL_DEFINITIONS})
Expand Down
24 changes: 9 additions & 15 deletions source/gameengine/Ketsji/KX_ConstraintWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,11 @@
*/


#include "EXP_PyObjectPlus.h"
#include "KX_ConstraintWrapper.h"
#include "PHY_IPhysicsEnvironment.h"

KX_ConstraintWrapper::KX_ConstraintWrapper(
PHY_ConstraintType ctype,
int constraintId,
PHY_IPhysicsEnvironment* physenv) :
m_constraintId(constraintId),
m_constraintType(ctype),
m_physenv(physenv)
#include "PHY_IConstraint.h"

KX_ConstraintWrapper::KX_ConstraintWrapper(PHY_IConstraint *constraint)
:m_constraint(constraint)
{
}
KX_ConstraintWrapper::~KX_ConstraintWrapper()
Expand All @@ -56,7 +50,7 @@ std::string KX_ConstraintWrapper::GetName()

PyObject *KX_ConstraintWrapper::PyGetConstraintId()
{
return PyLong_FromLong(m_constraintId);
return PyLong_FromLong(m_constraint->GetIdentifier());
}


Expand All @@ -68,7 +62,7 @@ PyObject *KX_ConstraintWrapper::PyGetParam(PyObject *args, PyObject *kwds)
if (!PyArg_ParseTuple(args,"i:getParam",&dof))
return nullptr;

value = m_physenv->GetConstraintParam(m_constraintId,dof);
value = m_constraint->GetParam(dof);
return PyFloat_FromDouble(value);

}
Expand All @@ -81,7 +75,7 @@ PyObject *KX_ConstraintWrapper::PySetParam(PyObject *args, PyObject *kwds)
if (!PyArg_ParseTuple(args,"iff:setParam",&dof,&minLimit,&maxLimit))
return nullptr;

m_physenv->SetConstraintParam(m_constraintId,dof,minLimit,maxLimit);
m_constraint->SetParam(dof,minLimit,maxLimit);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -125,13 +119,13 @@ PyAttributeDef KX_ConstraintWrapper::Attributes[] = {
PyObject *KX_ConstraintWrapper::pyattr_get_constraintId(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
KX_ConstraintWrapper* self = static_cast<KX_ConstraintWrapper*>(self_v);
return self->PyGetConstraintId();
return PyLong_FromLong(self->m_constraint->GetIdentifier());
}

PyObject *KX_ConstraintWrapper::pyattr_get_constraintType(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
KX_ConstraintWrapper* self = static_cast<KX_ConstraintWrapper*>(self_v);
return PyLong_FromLong(self->m_constraintType);
return PyLong_FromLong(self->m_constraint->GetType());
}

#endif // WITH_PYTHON
11 changes: 4 additions & 7 deletions source/gameengine/Ketsji/KX_ConstraintWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@
#define __KX_CONSTRAINTWRAPPER_H__

#include "EXP_Value.h"
#include "PHY_DynamicTypes.h"

class PHY_IConstraint;

class KX_ConstraintWrapper : public CValue
{
Py_Header
public:
KX_ConstraintWrapper(PHY_ConstraintType ctype,int constraintId,class PHY_IPhysicsEnvironment* physenv);
KX_ConstraintWrapper(PHY_IConstraint *constraint);
virtual ~KX_ConstraintWrapper ();

virtual std::string GetName();

int getConstraintId() { return m_constraintId; }

#ifdef WITH_PYTHON
KX_PYMETHOD_NOARGS(KX_ConstraintWrapper,GetConstraintId);
Expand All @@ -56,9 +55,7 @@ class KX_ConstraintWrapper : public CValue
#endif

private:
int m_constraintId;
PHY_ConstraintType m_constraintType;
PHY_IPhysicsEnvironment* m_physenv;
PHY_IConstraint *m_constraint;
};

#endif /* __KX_CONSTRAINTWRAPPER_H__ */
50 changes: 46 additions & 4 deletions source/gameengine/Ketsji/KX_PyConstraintBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ PyDoc_STRVAR(gPyCreateConstraint__doc__,
"createConstraint(ob1,ob2,float restLength,float restitution,float damping)\n"
""
);
PyDoc_STRVAR(gPyCreateVehicle__doc__,
"createVehicle(chassis)\n"
""
);
PyDoc_STRVAR(gPyGetVehicleConstraint__doc__,
"getVehicleConstraint(int constraintId)\n"
""
Expand Down Expand Up @@ -450,7 +454,7 @@ static PyObject *gPyGetVehicleConstraint(PyObject *self,
PHY_IVehicle* vehicle = PHY_GetActiveEnvironment()->GetVehicleConstraint(constraintid);
if (vehicle)
{
KX_VehicleWrapper* pyWrapper = new KX_VehicleWrapper(vehicle,PHY_GetActiveEnvironment());
KX_VehicleWrapper* pyWrapper = new KX_VehicleWrapper(vehicle);
return pyWrapper->NewProxy(true);
}

Expand Down Expand Up @@ -515,6 +519,14 @@ static PyObject *gPyCreateConstraint(PyObject *self,
PHY_IPhysicsController *physctrl = (PHY_IPhysicsController*)physicsid;
PHY_IPhysicsController *physctrl2 = (PHY_IPhysicsController*)physicsid2;
if (physctrl) { //TODO:check for existence of this pointer!
if (constrainttype == PHY_VEHICLE_CONSTRAINT) {
ShowDeprecationWarning("bge.constraints.createConstraint(...)", "bge.constraints.createVehicle(chassis)");
PHY_IVehicle *vehicle = PHY_GetActiveEnvironment()->CreateVehicle(physctrl);

KX_VehicleWrapper *wrap = new KX_VehicleWrapper(vehicle);

return wrap->NewProxy(true);
}
//convert from euler angle into axis
const float deg2rad = 0.017453292f;

Expand All @@ -525,23 +537,51 @@ static PyObject *gPyCreateConstraint(PyObject *self,
MT_Vector3 axis1 = localCFrame.getColumn(1);
MT_Vector3 axis2 = localCFrame.getColumn(2);

int constraintid = PHY_GetActiveEnvironment()->CreateConstraint(
PHY_IConstraint *constraint = PHY_GetActiveEnvironment()->CreateConstraint(
physctrl, physctrl2, (enum PHY_ConstraintType)constrainttype, pivotX, pivotY, pivotZ,
(float)axis0.x(), (float)axis0.y(), (float)axis0.z(),
(float)axis1.x(), (float)axis1.y(), (float)axis1.z(),
(float)axis2.x(), (float)axis2.y(), (float)axis2.z(), flag);

KX_ConstraintWrapper *wrap = new KX_ConstraintWrapper(
(enum PHY_ConstraintType)constrainttype, constraintid, PHY_GetActiveEnvironment());
if (!constraint) {
return nullptr;
}

KX_ConstraintWrapper *wrap = new KX_ConstraintWrapper(constraint);

return wrap->NewProxy(true);
}
}
Py_RETURN_NONE;
}

static PyObject *gPyCreateVehicle(PyObject *self, PyObject *args)
{
/* FIXME - physicsid is a long being cast to a pointer, should at least use PyCapsule */
unsigned long long physicsid = 0;

if (!PyArg_ParseTuple(args, "K:createVehicle", &physicsid)) {
return nullptr;
}

if (!PHY_GetActiveEnvironment()) {
Py_RETURN_NONE;
}

PHY_IPhysicsController *physctrl = (PHY_IPhysicsController*)physicsid;
if (!physctrl) { //TODO:check for existence of this pointer!
return nullptr;
}

PHY_IVehicle *vehicle = PHY_GetActiveEnvironment()->CreateVehicle(physctrl);
if (!vehicle) {
return nullptr;
}

KX_VehicleWrapper *wrap = new KX_VehicleWrapper(vehicle);

return wrap->NewProxy(true);
}

static PyObject *gPyGetAppliedImpulse(PyObject *self,
PyObject *args,
Expand Down Expand Up @@ -650,6 +690,8 @@ static struct PyMethodDef physicsconstraints_methods[] = {

{"createConstraint",(PyCFunction) gPyCreateConstraint,
METH_VARARGS|METH_KEYWORDS, (const char *)gPyCreateConstraint__doc__},
{"createVehicle",(PyCFunction) gPyCreateVehicle,
METH_VARARGS, (const char *)gPyCreateVehicle__doc__},
{"getVehicleConstraint",(PyCFunction) gPyGetVehicleConstraint,
METH_VARARGS, (const char *)gPyGetVehicleConstraint__doc__},

Expand Down
31 changes: 16 additions & 15 deletions source/gameengine/Ketsji/KX_VehicleWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
* \ingroup ketsji
*/

#include "EXP_PyObjectPlus.h"

#include "KX_VehicleWrapper.h"
#include "PHY_IPhysicsEnvironment.h"
#include "PHY_IVehicle.h"
#include "KX_PyMath.h"
#include "KX_GameObject.h"
Expand All @@ -34,23 +31,13 @@

#include "DNA_object_types.h" // for OB_MAX_COL_MASKS

KX_VehicleWrapper::KX_VehicleWrapper(
PHY_IVehicle* vehicle,
PHY_IPhysicsEnvironment* physenv) :
m_vehicle(vehicle),
m_physenv(physenv)
KX_VehicleWrapper::KX_VehicleWrapper(PHY_IVehicle *vehicle)
:m_vehicle(vehicle)
{
}

KX_VehicleWrapper::~KX_VehicleWrapper()
{
int numMotion = m_motionStates.size();
for (int i=0;i<numMotion;i++)
{
PHY_IMotionState* motionState = m_motionStates[i];
delete motionState;
}
m_motionStates.clear();
}

std::string KX_VehicleWrapper::GetName()
Expand Down Expand Up @@ -376,9 +363,23 @@ PyMethodDef KX_VehicleWrapper::Methods[] = {

PyAttributeDef KX_VehicleWrapper::Attributes[] = {
KX_PYATTRIBUTE_RW_FUNCTION("rayMask", KX_VehicleWrapper, pyattr_get_ray_mask, pyattr_set_ray_mask),
KX_PYATTRIBUTE_RO_FUNCTION("constraint_id", KX_VehicleWrapper, pyattr_get_constraintId),
KX_PYATTRIBUTE_RO_FUNCTION("constraint_type", KX_VehicleWrapper, pyattr_get_constraintType),
KX_PYATTRIBUTE_NULL //Sentinel
};

PyObject *KX_VehicleWrapper::pyattr_get_constraintId(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
KX_VehicleWrapper* self = static_cast<KX_VehicleWrapper*>(self_v);
return PyLong_FromLong(self->m_vehicle->GetUserConstraintId());
}

PyObject *KX_VehicleWrapper::pyattr_get_constraintType(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
KX_VehicleWrapper* self = static_cast<KX_VehicleWrapper*>(self_v);
return PyLong_FromLong(PHY_VEHICLE_CONSTRAINT);
}

PyObject *KX_VehicleWrapper::pyattr_get_ray_mask(PyObjectPlus *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
{
KX_VehicleWrapper *wrapper = static_cast<KX_VehicleWrapper*>(self);
Expand Down
12 changes: 3 additions & 9 deletions source/gameengine/Ketsji/KX_VehicleWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,18 @@

#include "EXP_Value.h"
class PHY_IVehicle;
class PHY_IMotionState;

#include <vector>

///Python interface to physics vehicles (primarily 4-wheel cars and 2wheel bikes)
class KX_VehicleWrapper : public CValue
{
Py_Header

std::vector<PHY_IMotionState*> m_motionStates;

public:
KX_VehicleWrapper(PHY_IVehicle* vehicle,class PHY_IPhysicsEnvironment* physenv);
KX_VehicleWrapper(PHY_IVehicle* vehicle);
virtual ~KX_VehicleWrapper ();

virtual std::string GetName();

int getConstraintId();

#ifdef WITH_PYTHON

KX_PYMETHOD_VARARGS(KX_VehicleWrapper,AddWheel);
Expand Down Expand Up @@ -57,12 +50,13 @@ class KX_VehicleWrapper : public CValue

static PyObject *pyattr_get_ray_mask(PyObjectPlus *self, const struct KX_PYATTRIBUTE_DEF *attrdef);
static int pyattr_set_ray_mask(PyObjectPlus *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
static PyObject *pyattr_get_constraintId(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static PyObject *pyattr_get_constraintType(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef);

#endif /* WITH_PYTHON */

private:
PHY_IVehicle* m_vehicle;
PHY_IPhysicsEnvironment* m_physenv;
};

#endif /* __KX_VEHICLEWRAPPER_H__ */
2 changes: 2 additions & 0 deletions source/gameengine/Physics/Bullet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ set(INC_SYS
)

set(SRC
CcdConstraint.cpp
CcdPhysicsEnvironment.cpp
CcdPhysicsController.cpp
CcdGraphicController.cpp

CcdConstraint.h
CcdMathUtils.h
CcdGraphicController.h
CcdPhysicsController.h
Expand Down
Loading

0 comments on commit 3341047

Please sign in to comment.