[ODE] Re: body geometry dualism

don don_reid at attbi.com
Sun Aug 18 16:28:02 2002


--ikeVEW9yuYc//A+q
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Nate W wrote:
> 
> On Thu, 8 Aug 2002, don wrote:
> > That said, the first thing I did was to start a library of objects
> > where each object manages a body and a geometry...

> Could this be done by deriving classes from the existing C++ wrappers?

Yes, although it can become awkward dealing with the auto construction of
things.


> 		dCCylinder::setBody (dCCylinder::_id);

yes, that is how you refer to the _id member ofthe embedded dCCylinder.

> ...
> 
> Personally I think this fits best at the C++ level.  Seems to me that a
> plain-C layer that does the same thing wouldn't provide the user with much
> benefit as compared to the usual approach of just associating the body and
> geometry objects at the application level.  That might just be a
> reflection of my own pro-C++ prejudices though. :-)

I'm pro C++ too when it helps, (or when the overhead doesn't hurt).

I find that just the number of commands needed to create a body and geom
are enough to justify some sort of wrapper.  Here is what I have at the
moment, still in progress obviously.  This allows a box to be created
with:

	seg[i] = dObjBox(world, space, length, width, height, x, y, z, R, &m);

The wrappers around dBodySetRotation, etc. are just shorthand, not
really needed.  They allow

	seg[i].getPos()

instead of

	dBodyGetPosition(seg[i].body);

Which is a little more object like.

Don Reid

--ikeVEW9yuYc//A+q
Content-Type: text/x-chdr; charset=us-ascii
Content-Disposition: attachment; filename="dObj.h"

///////////////////////////////////////////////////////////////////////////////
//   dObj.h
//
///////////////////////////////////////////////////////////////////////////////

#ifndef _DOBJ_H_
#define _DOBJ_H_

#include <ode/ode.h>

#define CP_V3(v3) (*((dVector3 *) v3 ))
#define CP_M3(m3) (*((dMatrix3 *) m3 ))
#define CP_Q(q)   (*((dQuaternion *) q ))


class dObj {
	public:
		dBodyID body;

	protected:
		// These provide for a global transform for creation
		static dVector3 xfPos;
		static dQuaternion xfQ;

	public:
		dObj();
		dObj(dWorldID world);
		dObj(dWorldID world, dReal x, dReal y, dReal z, dMatrix3 R, dMass *m);

		void setPos(dReal x, dReal y, dReal z);
		void setRot(dMatrix3 R);
		void setMass(dMass *m);

		const dReal * getPos();
		const dReal * getRot();
		const dReal * getQ();
		void getMass(dMass * m);

		void setTransform(dReal x, dReal y, dReal z, dMatrix3 rot);

	protected:
		void transform(dVector3 newPos, dMatrix3 newRot, 
				dReal x, dReal y, dReal z, dMatrix3 rot);
	};

class dObjBox : public dObj {
	public:
		dGeomID geom;

		dObjBox();
		dObjBox(dWorldID world, dSpaceID space, dReal lx, dReal ly, dReal lz,
				 dReal x, dReal y, dReal z, dMatrix3 R, dMass *m);

		void setSize(dReal lx, dReal ly, dReal lz);

		void getSize(dVector3 result);
	};

#endif
/* vi:set ts=4: */

--ikeVEW9yuYc//A+q
Content-Type: text/x-c++src; charset=us-ascii
Content-Disposition: attachment; filename="dObj.cpp"

//////////////////////////////////////////////////////////////////////////////
//  dObj.cpp
//////////////////////////////////////////////////////////////////////////////

#include "dObj.h"


dObj::dObj() : body(NULL) {}		// dummy for simplicity

dObj::dObj(dWorldID world) {
	if (body) dBodyDestroy(body);
	body = dBodyCreate (world);
	}

dObj::dObj(dWorldID world, dReal x, dReal y, dReal z, 
		   dMatrix3 R, dMass *m) {
	if (body) dBodyDestroy(body);
	body = dBodyCreate (world);
	setPos(x, y, z);
	setRot(R);
	setMass(m);
	}

void dObj::setPos(dReal x, dReal y, dReal z) {
	dBodySetPosition(body, x, y, z);
	}

void dObj::setRot(dMatrix3 R) { dBodySetRotation (body, R); }
void dObj::setMass(dMass *m) { dBodySetMass(body, m); }

const dReal * dObj::getPos() { return(dBodyGetPosition(body)); }
const dReal * dObj::getRot() { return(dBodyGetRotation(body)); }
const dReal * dObj::getQ() { return(dBodyGetQuaternion(body)); }

void dObj::getMass(dMass * m) { dBodyGetMass(body, m); }


////////////////////////////////
// Box
////////////////////////////////

dObjBox::dObjBox() : geom(NULL) {};		// dummy for simplicity

dObjBox::dObjBox(dWorldID world, dSpaceID space, dReal lx, dReal ly, dReal lz,
				 dReal x, dReal y, dReal z, dMatrix3 R, dMass *m)
				: dObj(world, x, y, z, R, m) {
	if (geom) dGeomDestroy(geom);
	geom = dCreateBox(space, lx, ly, lz);
	dGeomSetBody(geom, body);
	}

void dObjBox::setSize(dReal lx, dReal ly, dReal lz) {
 	dGeomBoxSetLengths(geom, lx, ly, lz);
	}

void dObjBox::getSize(dVector3 result) {
	dGeomBoxGetLengths(geom, result);
	}

/* vi:set ts=4: */

--ikeVEW9yuYc//A+q--