Page 2 of 3

Re: Collision detection advice

Posted: Fri Jan 20, 2012 4:59 pm
by kandkstuart
I have 18 dynamic bodies with one shape attached to each. I am storing 8 of those dynamic bodies offscreen but at non-colliding locations..... If I comment out the code above for all the static objects I get 60 fps, which makes sense but I don't understand if they are static and none of the dynamic bodies are touching them why the it takes up to 9 seconds in the cpSpaceStep routine iterating through collision pairs... does that make any sense to you?

Re: Collision detection advice

Posted: Fri Jan 20, 2012 5:20 pm
by slembcke
Only if the shapes really aren't being attached to static bodies. Try stepping through the code of cpSpaceAddShape() to see if it's following the static path. I'm not sure what else to tell you at this point.

Re: Collision detection advice

Posted: Fri Jan 20, 2012 5:26 pm
by kandkstuart
I've stepped through and it seems like they are static. I'll add some debug to make sure all my static body/shapes are getting setup correctly. The code is taking a long time in here;
// Find colliding pairs.
cpSpacePushFreshContactBuffer(space);
cpSpatialIndexEach(space->activeShapes, (cpSpatialIndexIteratorFunc)cpShapeUpdateFunc, NULL);
cpSpatialIndexReindexQuery(space->activeShapes, (cpSpatialIndexQueryFunc)cpSpaceCollideShapes, space);

This is where all the time is being spent. Does that give you any ideas on how I'm messing things up?

Re: Collision detection advice

Posted: Sat Jan 21, 2012 8:42 am
by kandkstuart
Hey Scott, so I was completely wrong. After debugging last night the issue wasn't in cpSpaceStep (I don't know how I missed this but I did) it is in cpSpaceEachShape. When I stepped into the code I realized that routine calls the specified function for Active and Static shapes. I'm sure it calls it for static shapes because in instances they can be rogue and need updating. For me that's not the case at all, all my static shapes never move. So I created a new function that deals with only active shapes and viola I'm getting 60 fps now. Thanks for taking the time to help me out. Now it's time to finish up!!!!!! Have a great weekend!

Re: Collision detection advice

Posted: Sat Jan 21, 2012 11:03 am
by slembcke
If you are using the private active shapes index in the space, keep in mind that when objects fall asleep, they get moved into the static index. I wouldn't really recommend doing that. My general advice to people is not to treat the space as an iterable container at all. You should be keeping your own list of sprites or objects or whatever external to Chipmunk.

Re: Collision detection advice

Posted: Sat Jan 21, 2012 11:08 am
by kandkstuart
I am doing that. So what I don't understand then is if chipmunk is keeping track of sleeping items, why does it call the function passed into cpSpaceEachShape even if the item is sleeping? My callback is getting called for static shapes that aren't moving, and its doing it every frame? Not sure why that would be. Should it be doing that? Or is it my responsibility to exit out of the callback if the shape is static?

Re: Collision detection advice

Posted: Sat Jan 21, 2012 12:43 pm
by slembcke
cpSpaceEachShape() calls the callback for every shape whether static, dynamic or sleeping. The problem is that there isn't really an efficient way to only iterate the dynamic or sleeping shapes separate from the static ones. Which again is why I would recommend that you store a list of sprites externally and not use cpSpaceEachShape() in your update loop.

Re: Collision detection advice

Posted: Sat Jan 21, 2012 4:01 pm
by kandkstuart
That makes complete sense! I didn't realize that is how it worked but knowing is half the battle. I'll just do all that stuff in my sprite step func and things will work much better. I appreciate the insight and help!!!

So I gotta ask, what is the whole chipmunk thing? Are you from the North East? I've got tons of those little lawn killing creatures in my yard :-)

Re: Collision detection advice

Posted: Sat Jan 21, 2012 4:14 pm
by slembcke
I'm from Minnesota, but they are pretty much everywhere.

My first physics library was called Squirrel Physics Kit. It wasn't very good. There were lots of Squirrels outside of my dorm window when I wrote it.

Chipmunk is actually my third physic library. (I called my second one Chipmunk as well, but the Chipmunk that exists today has been almost completely rewritten since then.) I figured I'd stick with the whole rodent thing, and we have a ton of chipmunks near the family lake cabin.

Re: Collision detection advice

Posted: Sat Jan 21, 2012 4:28 pm
by kandkstuart
Pretty cold winters I imagine. I'm from NH so I know what that's like. It's my first stab at an iOS game (I write software for a big SBC (Session Border Controllers) company in Mass) and it's really fun. You've done a great job with the API. When the game hits the app store I'll let you know if your interested.