What's the right way to delete a Chipmunk body? Simply calling cpBodyFree
or cpBodyDestroy
doesn't seem to work, as the body still shows up in the cpSpaceEachBody
iteration.
if(body->p.y < -260 || fabsf(body->p.x) > 340) {
/* body is permanently off the screen */
/* so it needs to be permanently deleted */
cpBodyFree(body); ??
cpBodyDestroy(body); ??
}
Here's how to delete a body:
Here's how to make the Plink demo rain down a single shower of pentagons and clean them up when they go off screen.
Add this line to the "//Add lots of pentagons" loop. This is so we can free the shape attached to the body.
body->data=shape;
remove the shape and body from the space, then free the shape and body. It doesn't seem to matter if you remove/free the shape first or the body first, so long as you keep in mind that you lose the pointer to the shape when you free the body. Change the eachBody function to:
if (body->p.y < -260 ) {
cpSpaceRemoveShape(space, body->data);
cpSpaceRemoveBody(space, body);
cpShapeFree(body->data);
cpBodyFree(body);
}