Using Chipmunk's spatial hashing

Official forum for the Chipmunk2D Physics Library.
Post Reply
JNewt
Posts: 3
Joined: Tue Jan 27, 2009 11:55 am
Contact:

Using Chipmunk's spatial hashing

Post by JNewt »

Hello all,
After giving up on writing my physics system for a 2D platformer, I decided to integrate Chipmunk. Because I'm working in D, this involved the creation of bindings, but it all seems to be working now. My question regards the use of Chipmunk's hashing for things like drawing, hittesting, etc. My project has been using a quadtree class I wrote for object storage, but it seems like an awfully bad idea to maintain two seperate lists of objects, with the quadtree updating constantly to reflect the status of the chipmunk objects. Is there a quick way to return lists of objects within a world region? IE., for my rendering code, I'd like to know what bodies are at least partly contained within a certain rectangle. If such a function doesn't already exist, how can I get access to the bits necessary to write one?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk's spatial hashing

Post by slembcke »

Sort of, the spatial has is fairly generic, but I haven't gotten around to making functions specific for accessing them from Chipmunk spaces directly. So you'll have to poke around at a slightly lower level. The two relevant things you need to know are the following:

Code: Select all

// Query callback.
typedef int (*cpSpaceHashQueryFunc)(void *obj1, void *obj2, void *data);

// Query the hash for a given BBox.
void cpSpaceHashQuery(cpSpaceHash *hash, void *obj, cpBB bb, cpSpaceHashQueryFunc func, void *data);
Chipmunk spaces maintain two separate spatial hashes, space.staticShapes and space.activeShapes. The staticShapes hash contains references to shapes added with cpSpaceAddStaticShape() and activeShapes from cpSpaceAddShape().

If you need an example of how cpSpaceHashQuery() works, take a look in cpSpace.c where it's used to collide the active shapes against the static hash.

P.S. Something that might not be so obvious is that the obj parameter passed to cpSpaceHashQuery() will always be sent to the callback function as obj1. So if you don't actually care to send an object along with the query, just pass NULL as obj, and ignore obj1 in the callback.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
JNewt
Posts: 3
Joined: Tue Jan 27, 2009 11:55 am
Contact:

Re: Using Chipmunk's spatial hashing

Post by JNewt »

Ok, so if I understand you correctly, I can pass a null for the 2nd parameter, and a bounding box which represents the area I'm interested in for the 3rd. When my callback is called, what will the data argument contain? Some sort of list of the shapes which were either inside or intersecting with my bounding box? Then, presumably, I just need to loop through all the shapes in the list and draw the cpBody associated with them.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk's spatial hashing

Post by slembcke »

No, the data argument passed to the callback is the same as the parameter passed to the cpSpaceHashQuery() call. Really, both obj, and data are just context values.

The callback is called once for each object in the hash that is identified as probably overlapping. It does not perform a midphase bounding box check before calling the callback.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
JNewt
Posts: 3
Joined: Tue Jan 27, 2009 11:55 am
Contact:

Re: Using Chipmunk's spatial hashing

Post by JNewt »

Ok, that actually makes more sense than trying to cast a pointer back to a list, particularly because I'm going cross-language. That should get me going, thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests