Hi there! I started using chipmunk some weeks ago and I'm stuck with something that may be simple, but I can't solve it.
I want to know how many shapes are sorrounding another one (let's call it shapeA) within a certain distance.
My idea was to get the position of the body associated with shapeA (no problem with that) and then use cpSpacePointQuery, because it should call a func for each shape found along within a given distance. Then I can count how many times this func was called by cpSpacePointQuery.
But it didn't work, the program collapse when running with a segmentation fault (core dumped). When I build I don't receive errors nor warnings, and when I debug it says "#0 0x43421b cpSpatialIndexQuery(index=0x65b000, obj=0x7fffffffddb0, bb=..., func=0x434350 <NearestPointQuery>, data=0x0) (../include/chipmunk/cpSpatialIndex.h:210)
I haven't found an example of cpSpacePointQuery and I must be misunderstanding the headers. I've really checked the pointers declarations and initializations. Is it too much to ask a little example code for this? may be it's faster if I check the example and compare with my program.
Thank you!
How many shapes are surrounding another?
-
- Posts: 8
- Joined: Fri Apr 21, 2017 12:52 pm
- Contact:
-
- Posts: 206
- Joined: Tue Aug 21, 2007 3:12 pm
- Contact:
Re: How many shapes are surrounding another?
Maybe you can try to make a simple example and paste here? Then its easier for us to see if/what is wrong. Unfortunately I mostly write python and use Chipmunk through the pymunk python library, so I dont have a ready example in C to show you.. But I know that its possible to make it work ![Smile :)](./images/smilies/icon_e_smile.gif)
![Smile :)](./images/smilies/icon_e_smile.gif)
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
-
- Posts: 8
- Joined: Fri Apr 21, 2017 12:52 pm
- Contact:
Re: How many shapes are surrounding another?
Hi viblo, thank you for the reply. I managed to solve my problem without using queries, but it would be interesting to learn how to use them. I think that the lines I used were something like this.viblo wrote:Maybe you can try to make a simple example and paste here? Then its easier for us to see if/what is wrong. Unfortunately I mostly write python and use Chipmunk through the pymunk python library, so I dont have a ready example in C to show you.. But I know that its possible to make it work
Somewhere in the code I call an iterator function:
cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc) functionA, NULL);
The function called is defined below, which then calls a counter.
static void
counter(cpShape *shape)
{
count=count+1;
}
static void
functionA(cpBody *body)
{
cpSpace *space;
cpVect pos;
void *data = NULL;
count = 0;
pos = cpBodyGetPosition(body);
cpSpacePointQuery(space, pos, 2*radius, CP_SHAPE_FILTER_NONE, (cpSpacePointQueryFunc) counter, data);
printf("%d: ( %g , %g ) \n", count, pos.x, pos.y);
}
![Very Happy :D](./images/smilies/icon_e_biggrin.gif)
-
- Posts: 206
- Joined: Tue Aug 21, 2007 3:12 pm
- Contact:
Re: How many shapes are surrounding another?
In general I would suggest to make a minimal example first (so just try with functionA that does nothing). Then you add one thing at a time until it breaks.
In this particular case I can see some strange things:
1. What is count in the counter function? A global variable?
2. Doesnt counter have the wrong type? Looks like it should have more parameters: https://chipmunk-physics.net/release/Ch ... eca9dfc257
3. What is space? You dont assign the space pointer to anything before calling cpSpacePointQuery.
4. Does functionA work? cpSpaceBodyIteratorFunc should also get a data argument: https://chipmunk-physics.net/release/Ch ... b8a742570e
In this particular case I can see some strange things:
1. What is count in the counter function? A global variable?
2. Doesnt counter have the wrong type? Looks like it should have more parameters: https://chipmunk-physics.net/release/Ch ... eca9dfc257
3. What is space? You dont assign the space pointer to anything before calling cpSpacePointQuery.
4. Does functionA work? cpSpaceBodyIteratorFunc should also get a data argument: https://chipmunk-physics.net/release/Ch ... b8a742570e
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
-
- Posts: 8
- Joined: Fri Apr 21, 2017 12:52 pm
- Contact:
Re: How many shapes are surrounding another?
Hi again Viblo.. Sorry, I think I copied some nonsenses the other day. It was so long ago that I forgot some things.viblo wrote:In general I would suggest to make a minimal example first (so just try with functionA that does nothing). Then you add one thing at a time until it breaks.
In this particular case I can see some strange things:
1. What is count in the counter function? A global variable?
2. Doesnt counter have the wrong type? Looks like it should have more parameters: https://chipmunk-physics.net/release/Ch ... eca9dfc257
3. What is space? You dont assign the space pointer to anything before calling cpSpacePointQuery.
4. Does functionA work? cpSpaceBodyIteratorFunc should also get a data argument: https://chipmunk-physics.net/release/Ch ... b8a742570e
The count variable was global and the missing arguments you mention in 2 and 4 were in my program, but I didn't copy them in the reply. But you are right about 3, I think that's the key.
In cpSpacePointQuery, the first argument must be cpSpace type. But I'm inside functionA and I can't pass a cpSpace argument to functionA (as it says in the link, the arguments must be a cpBody and a data). So, how can I pass the cpSpace argument to cpSpacePointQuery if I didn't passed it to functionA before? I think that's why I defined that pointer but I didn't know how to assign it.
Another thing, can I pass NULL as a data argument?
Thank you!
-
- Posts: 206
- Joined: Tue Aug 21, 2007 3:12 pm
- Contact:
Re: How many shapes are surrounding another?
Yes, its fine to pass NULL in case you dont have anything special to send in. However, in your case maybe you could just send the space there? Then you would have it easily available. Another option is to get the space from the body, and you can get the body from the shape. Check the docs for cpBodyGetSpace and cpShapeGetBody.
http://www.pymunk.org - A python library built on top of Chipmunk to let you easily get cool 2d physics in your python game/app
-
- Posts: 8
- Joined: Fri Apr 21, 2017 12:52 pm
- Contact:
Re: How many shapes are surrounding another?
Perfect, thank you very much for your help, this reply is very useful. I didn't know how to manage the data argument and those functions are also handy. I'm programming another things these days, but I'll return to this problem soon. I really appreciate your helpviblo wrote:Yes, its fine to pass NULL in case you dont have anything special to send in. However, in your case maybe you could just send the space there? Then you would have it easily available. Another option is to get the space from the body, and you can get the body from the shape. Check the docs for cpBodyGetSpace and cpShapeGetBody.
![Smile :)](./images/smilies/icon_e_smile.gif)
Who is online
Users browsing this forum: No registered users and 4 guests