Page 1 of 1

How many shapes are surrounding another?

Posted: Mon Apr 24, 2017 5:57 pm
by lautarov
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!

Re: How many shapes are surrounding another?

Posted: Sat May 20, 2017 5:36 am
by viblo
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 :)

Re: How many shapes are surrounding another?

Posted: Sun Jun 04, 2017 5:35 pm
by lautarov
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 :)
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.

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);
}

:D

Re: How many shapes are surrounding another?

Posted: Mon Jun 05, 2017 6:11 am
by viblo
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

Re: How many shapes are surrounding another?

Posted: Mon Jun 12, 2017 12:48 pm
by lautarov
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
Hi again Viblo.. Sorry, I think I copied some nonsenses the other day. It was so long ago that I forgot some things.
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!

Re: How many shapes are surrounding another?

Posted: Tue Jun 13, 2017 3:12 pm
by viblo
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.

Re: How many shapes are surrounding another?

Posted: Thu Jun 22, 2017 4:41 pm
by lautarov
viblo 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.
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 help :)