I have a lot of bodies with same BodyDef in Box2D and when I call:
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
for(Body b : bodies){
if(b.getPosition().y < -20f) {
world.destroyBody(b);
}
}
it destroys all of the bodies with that BodyDef.
How can I fix that?
You're not referencing any BodyDef here.
world.destroyBody(b);
should only destroy body b. Just make sure that your if condition is ok. You could also debug step by step (or put a log inside the if), just to check if the number of bodies destroyed is the same as the times the if condition is true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With