[ODE] dSpaceQuery

Nate W coding at natew.com
Fri May 10 16:41:02 2002


It happens that my app would like to know whether certain geometry objects
are 'in' the current space.  I could keep a set of flags to remember which
objects are in, and tweak them ever time I call dSpaceAdd or dSpaceRemove,
but that strikes me as less than elegant.  So, I wrote a dSpaceQuery
method, which returns true if the given object is in the given space,
false if not.  Ah, the beauty of open source.  (Thanks so much, Russ!)

Added to space.h:

	bool dSpaceQuery (dSpaceID, dGoemID);

Added to space.cpp, in the dxSpace class:

	virtual void query (dGeomID)=0;

Non-pure-virtual members were added to dxSimpleSpace and dxHashSpace as
well.  Since the actual functions are identical in both classes, that
isn't strictly necessary, but since all of the other functions in dxSpace
are pure virtual (even though the 'add' methods are implemented
identically) I figured it was the way to go. 

Added to space.cpp, near bottom of file, below dSpaceRemove:

	bool dSpaceQuery (dxSpace * space, dxGeom *g)
	{
		return space->query (g);
	}


Added to space.cpp, for both dxSimpleSpace and dxHashSpace:

	bool dxSimpleSpace::query (dGeomID obj)
	{
		dAASSERT (this && obj);

		if (obj->spaceid != this)
			return false;

		dGeomID compare = first;
		while (compare)
		{
			if (compare == obj)
				return true;

			compare = compare->space.next;
		}
	
		dUASSERT (false, "object thinks it's in this space, but it ain't");
		return false;
	}


Added to msvcdefs.def:

	dCreateGeom
	dSpaceAdd
	dSpaceRemove
	dSpaceQuery

I was surprised that the first three weren't there already... Is
msvscdefs.def obsolete now?  

I've only tested this a little bit so far, but I feel pretty good about
it.

-- 

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