[ODE] Callback function for Colision

Nate W coding at natew.com
Sat Jan 18 11:49:02 2003


On Fri, 17 Jan 2003, Christian Gilman wrote:

> i have got an own class for ODE, but now i want to make the colision,
> but i don't know how i can code the nearCallback function in this
> class, becaus it must be static und the functions needs a memver var
> from the class.
> 
> This is not realy a question about ODE, but maybe someone got the same
> problem and found a solution.

Routing a static callback to a regular member function is a common issue
in C++ programming.  If you can pass a user-defined value (that is, a
value that the library won't mess with) into the static callback function,
you're set.  Fortunately, ODE allows this, probably for just this reason.
Basically, you pass in your "this" pointer so it will get passed into a
static callback function.  Then, inside the static callback, you grab the
"this" pointer and use it to call a regular class method.

class OdeUniverse
{
	void vStep ();
	
	void RegularMethodCallback (dGeomID o1, dGeomID o2)
	{
		// collision code goes here
	}

	static void StaticCollisionCallback (void *pData, 
		dGeomID o1, dGeomID o2)
	{
		// grab the "this" pointer
		OdeClass *pThis = (OdeClas*) pData;
	
		// call the regular class method version of the callback
		pThis->RegularMethodCallback (o1, o2);		
	}
}

void OdeUniverse::vStep ()
{
	// ...

	// the "void*" parameter is where you pass in the "this" pointer
	dSpaceCollide (MySpaceID, this, StaticCollisionCallback);

	// ...
}

-- 

Nate Waddoups
Redmond WA USA
http://www.natew.com