Page 1 of 2

Picking

Posted: Thu Mar 27, 2008 4:47 pm
by jim
I'd imagine that there is a simple way of specifying a coordinate and then using the spatial hash to find if there is a shape there...i.e. picking. Does anyone know how to do this with chipmunk? Any pointers will be much appreciated.

thanks.

Re: Picking

Posted: Thu Mar 27, 2008 6:38 pm
by Kimau
There are two ways

For polys you can do it manually as all polys in Chipmunk must be concave so its simple to implment.

The alternative way is make a circle shape and add it to your space but dont add its body.
Then update your circle shape to match mouse position. And you can use collisions generated by circle shape.

Re: Picking

Posted: Fri Mar 28, 2008 2:05 am
by slembcke
If you use the trunk code, I had already added functions for doing point queries against shapes a while ago. I finally added the support to do point queries to the spatial hash just now.

The new function is cpSpaceHashPointQuery(). I didn't test it particularly thoroughly, but it's pretty simple. I added a call to the main demo application so that it highlights the shape under the mouse. Cheers.

One thing to note: cpSpace.activeShapes (or staticShapes) isn't really part of the public API. I intend to add a nice API for generic spatial indexes someday, but until then...

Re: Picking

Posted: Fri Mar 28, 2008 3:10 am
by jim
Thanks for your quick and helpful responses.
I will use cpSpaceHashPointQuery() and feedback any issues I might encounter.

Thanks,
Jim.

Re: Picking

Posted: Fri Mar 28, 2008 10:44 am
by Pipo
Is there any way to do this with the ruby binding?

Thanks.

Re: Picking

Posted: Fri Mar 28, 2008 2:46 pm
by slembcke
Not yet. Right now it's a pretty low level implementation. I'll at least want to make it a function of the space first.

Would probably only be like 30 lines of code if you wanted to try your hand at implementing it.

Re: Picking

Posted: Sun Mar 30, 2008 12:57 am
by slembcke
Ok, I added a space functions for querying the hashes.

C Side:

Code: Select all

typedef void (*cpSpacePointQueryFunc)(cpShape *shape, void *data);
void cpSpaceShapePointQuery(cpSpace *space, cpVect point, cpSpacePointQueryFunc func, void *data);
void cpSpaceStaticShapePointQuery(cpSpace *space, cpVect point, cpSpacePointQueryFunc func, void *data);
Ruby Side:

Code: Select all

CP::Space#shape_point_query(point){|shape| ... }
CP::Space#static_shape_point_query(point){|shape| ... }

Re: Picking

Posted: Mon Mar 31, 2008 1:16 am
by mcc
slembcke wrote:Ok, I added a space functions for querying the hashes.

C Side:

Code: Select all

typedef void (*cpSpacePointQueryFunc)(cpShape *shape, void *data);
void cpSpaceShapePointQuery(cpSpace *space, cpVect point, cpSpacePointQueryFunc func, void *data);
void cpSpaceStaticShapePointQuery(cpSpace *space, cpVect point, cpSpacePointQueryFunc func, void *data);
Ruby Side:

Code: Select all

CP::Space#shape_point_query(point){|shape| ... }
CP::Space#static_shape_point_query(point){|shape| ... }
Hi,

That is fantastic! I actually just signed up at the forum to ask if something like this were possible. However, I am a little bit confused. How do I get access to these functions? When I click at "source+demos" at the main site I get a "ChipmunkLatest.tgz", which unpacks to a Chipmunk 4.0.2. It does not appear to contain these functions you describe here. Am I missing something?

Thanks.

EDIT: Never mind, I figured out how to check out from svn head. Thanks!

Re: Picking

Posted: Mon Mar 31, 2008 7:17 am
by jim
Ok, I have some static items that I would like to be picked using this system. These items should not be collideable, so I intend so add them to the Space as static and set them to a sperate layer. Will this work (currently coding it), or is their a better solution?

Thanks,
Jim.

Re: Picking

Posted: Mon Mar 31, 2008 9:07 am
by slembcke
That will work fine. There is support for querying the static hash as well.