0
\$\begingroup\$

So I'm working on a LibGDX game, and when a bullet body collides with a block, I want to destroy the bullet. I'm calling:

Globals.gameMan.world.destroyBody(bullet.body);

and for whatever reason, the entire game crashes after this. I'm given a message with the option to debug twice, and when I click it, nothing happens. Thank you, and sorry for the noob question.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can't destroy a body inside the cycle of Box2d World. You need to mark that body to be destroyed and destroy it after the world.step() is finished executing. Get an array of bodies to be destroyed and destroy them afterwards.

\$\endgroup\$
1
\$\begingroup\$

Make sure you're not calling World.destroyBody passing a Body that has already been destroyed.

Also, from where are you calling World.destroyBody, if it's from a Box2D call-back (a collision handler, for example) you are not allowed to destroy bodies. If that is the case you need to make note of the Body to destroy and then destroy it outside of the call-back.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .