Page 1 of 3

body:destroy()

Posted: Mon Feb 28, 2011 2:29 pm
by tentus
If my reading of Box2D is right, destroy()ing a body also, by necessity, destroys all shapes associated with it, right? I recently discovered that niling/refilling a table a body was in does not delete the body, so I want to try and find out if there are any other oddities I should know about.

Re: body:destroy()

Posted: Mon Feb 28, 2011 3:59 pm
by Robin
I don't think you ever need to call body:destroy() yourself. You have to have zero references to it though.

Re: body:destroy()

Posted: Mon Feb 28, 2011 4:04 pm
by kikito
Robin wrote:I don't think you ever need to call body:destroy() yourself. You have to have zero references to it though.
I'm not so sure about that. If you have zero references and you manually invoke the garbage collector, then you are probably safe. Otherwise, I think there could be some "phantom bodies" laying around until the GC is launched. I don't have time to make the test right now.

Re: body:destroy()

Posted: Mon Feb 28, 2011 5:15 pm
by tentus
I guarantee the GC won't get it, unless the GC takes more than 30 seconds to work (in a test scenario that runs at 400-600 fps on my computer). I think that because bodies are inside the world, the GC thinks they're not garbage, which makes sense. Regardless, I can't have phantom player bodies sitting around after every death, or worse, breakable blocks that don't actually break, enemies that won't die, or explosions that never stop being dangerous.

Remember, I can nil out the table the bodies are in completely, but they stick around.

Re: body:destroy()

Posted: Mon Feb 28, 2011 6:33 pm
by Robin
That is odd.

Re: body:destroy()

Posted: Mon Feb 28, 2011 7:54 pm
by BlackBulletIV
I've tried in vain to destroy bodies after creation, but I can't. If I use body:destroy(), usually the result is a big crash a few seconds later with a weird Box2D error. Other times, there will be ghost bodies lying around for a few seconds afterwards, and then after a little bit the ghost bodies will go away... but usually not long after another crash occurs.

So in short, I can't destroy bodies.

Re: body:destroy()

Posted: Tue Mar 01, 2011 1:49 am
by tentus
I'm having some success by simply moving bodies to above the world, where the player can't get to, and then calling destroy. I dunno if destroy is working (I suspect not, since if I don't move the objects I can expect crashes like BlackBullet described), and this method worries me because I'm shooting in the dark.

Could we go ahead and request the feature that destroy() return a boolean, true if it was a success, and false if not?

Re: body:destroy()

Posted: Tue Mar 01, 2011 6:23 am
by BlackBulletIV
Well that's a way of pretending they're not there at least, either moving them outside the world or putting them on a blank collision layer. But yeah, I don't think destroy() works properly (not for me anyway).

Re: body:destroy()

Posted: Tue Mar 01, 2011 9:58 am
by bartbes
Alright, let's just end all this:
For both destroy() and the GC to function, all shapes, contacts and other associated things have to be released as well.
Then, the difference between destroy() and the GC: destroy() is there to force removal at that specific time, the GC may take some longer.
As for the crashes: If you destroy an object that is still colliding Box2D won't like it, which is why moving it outside of the world, or setting it to no longer collide solves this problem.

Re: body:destroy()

Posted: Tue Mar 01, 2011 10:03 am
by BlackBulletIV
Ah, that helps, thanks. Also explains the unpredictability of the crashes.