[ODE] Specific Collision Handling...

Roel van Dijk R.vanDijk at rad.umcn.nl
Thu Oct 9 09:05:01 MST 2003


This is where C++ could use some features from java :-)
This is how I would do it:
Create a virtual function "collided" in the CBaseObject class:

virtual void collided(CBaseObject *object);

CItem, CPlayer and CObstacle should implement this function. I'm assuming that 
CBaseObject is a wrapper for a dGeomID and possibly a dBodyID. So when you 
create an instance of CBaseObject you should pass the geom a pointer to that 
instance. Something like:

CBaseObject::CBaseObject()
{
	geom = dCreateSomeGeom();
	dGeomSetData(this);
}

Then in the collision callback routine do something like this:

static void nearCallback(void *data, dGeomID o1, dGeomID o2)
{
	CBaseObject 
		*baseObject1 = static_cast<CBaseObject*>(dGeomGetData(o1)),
		*baseObject2 = static_cast<CBaseObject*>(dGeomGetData(o2));
	baseObject1->collided(baseObject2);
	baseObject2->collided(baseObject1);
	...
}

Now the collided function of the appropriate objects is called, but those 
objects don't know the class of the object they collided with, except that it 
is also an CBaseObject. So here you will need to come up with a way to get 
the class of an object at runtime. Java has an 'instanceof' keyword, but c++ 
has no such thing (except when you use Qt). You could create a CMetaObject 
class that is the base of all your classes, including CBaseObject. Then you 
can put a member 'classID' in the CMetaObject. You probably want this classID 
to be a numerical type, that makes it easy to compare with other classID's. 
Make sure that every class is uniquely identified by it classID to avoid 
ambiguity. Then all you need is a quick check in the collided function.

You could also use a string as a classID. This would rule out any case of 
ambiguit, but it is slow to compare strings with string. Another solution is 
to use 2 members, a string className and a number classID. Write a 
setClassName function that set's the class name and creates a unique id for 
that string. You can use some hash function to accomplish this.

Anyway, I hope any of this helps with your problem :-)
Roel


More information about the ODE mailing list