Page 1 of 1

disable calculations on offscreen cpbody?

Posted: Mon Mar 22, 2010 5:46 pm
by moby
Hi,
I have a game that i'm optimizing performance on. I'm using the latest chipmunk (5.x) and cocos2d on the iphone. the question has to do with the chipmunk side.

I recycle my sprites. So I have a spriteFactory that makes - let's say - 6 sprites. Upon collision (with a bullet), i've chosen not to destroy the sprites (and associated cpShape/cpBody) so I can use them again without having to remanufacture them.
The technique I use in the collision callback is to simply place the collided sprite offscreen at some point far, far, away. Immediately after that is done, the sprite is reset and gets back onscreen.
So... all's well...
BUT...
I am worried that when i banish a sprite temporarily to that far away point, that chipmunk is still calculating stuff on it. And that is compounded if 2 sprites are banished to overlapping points.

My question is: is there a way I can temporarily disable calaculations on a cpBody/shape and resume the claculation once it's position is reset? that might gain me some frames per second.

thanks
m0by

Re: disable calculations on offscreen cpbody?

Posted: Mon Mar 22, 2010 5:54 pm
by slembcke
If you remove the body and collision shapes from the space, then no CPU time will be spent on them.

Re: disable calculations on offscreen cpbody?

Posted: Mon Mar 22, 2010 6:33 pm
by moby
thanks. so do i then re-add body and shape in the standard way (e.g. re-initialize them?)
cheers

Re: disable calculations on offscreen cpbody?

Posted: Tue Mar 23, 2010 8:19 am
by slembcke
I wouldn't really recommend reusing your objects like that as it can create a lot of headaches. If you really want, you can reinitialize any Chipmunk object by using the 'init' instead of 'new' function.

Example:

Code: Select all

// To create it the first time
cpBody *body = cpBodyNew(mass, moment);

// re-initialize it later
cpBodyInit(body, mass, moment);
Every type that has a 'new' function should also have an 'init' function that takes a pointer to the struct followed by the same parameters that the 'new' function takes.