Picking

Official forum for the Chipmunk2D Physics Library.

Picking

Postby jim » Thu Mar 27, 2008 4:47 pm

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.
jim
 
Posts: 6
Joined: Fri Mar 14, 2008 6:44 am

Re: Picking

Postby Kimau » Thu Mar 27, 2008 6:38 pm

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.
Kimau
 
Posts: 1
Joined: Mon Mar 24, 2008 4:08 am

Re: Picking

Postby slembcke » Fri Mar 28, 2008 2:05 am

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...
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Picking

Postby jim » Fri Mar 28, 2008 3:10 am

Thanks for your quick and helpful responses.
I will use cpSpaceHashPointQuery() and feedback any issues I might encounter.

Thanks,
Jim.
jim
 
Posts: 6
Joined: Fri Mar 14, 2008 6:44 am

Re: Picking

Postby Pipo » Fri Mar 28, 2008 10:44 am

Is there any way to do this with the ruby binding?

Thanks.
Pipo
 
Posts: 13
Joined: Thu Jan 03, 2008 11:27 am

Re: Picking

Postby slembcke » Fri Mar 28, 2008 2:46 pm

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.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Picking

Postby slembcke » Sun Mar 30, 2008 12:57 am

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| ... }
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: Picking

Postby mcc » Mon Mar 31, 2008 1:16 am

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!
mcc
 
Posts: 25
Joined: Sun Mar 30, 2008 9:00 pm

Re: Picking

Postby jim » Mon Mar 31, 2008 7:17 am

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.
jim
 
Posts: 6
Joined: Fri Mar 14, 2008 6:44 am

Re: Picking

Postby slembcke » Mon Mar 31, 2008 9:07 am

That will work fine. There is support for querying the static hash as well.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Next

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 5 guests

cron