Reputation: 7753
I need to destroy one of the body's after collision in JBox2D android game. I found that JBox2D world become locked when body's become in contact. I wanted to destroy one of the body after collision. Can i get any call back after world released lock ?. I found an option that adding the body's into an array for destroying it. But when to destroy the body ?. I am not using andengine/libgdx in this game. Find my collision listener class below,
private class CollisionListener implements ContactListener {
@Override
public void beginContact(Contact contact) {
Object fixtureA = contact.getFixtureA().getUserData();
Object fixtureB = contact.getFixtureB().getUserData();
Body mBodyA = contact.getFixtureA().getBody();
Body mBodyB = contact.getFixtureB().getBody();
if (fixtureA instanceof Bullet) {
destroyBalloonBody(mBodyB);
}
if (fixtureB instanceof Bullet) {
destroyBalloonBody(mBodyA);
}
}
@Override
public void endContact(Contact contact) {
}
@Override
public void postSolve(Contact contact, ContactImpulse contactImpulse) {
}
@Override
public void preSolve(Contact contact, Manifold manifold) {
}
}
public void destroyBalloonBody(Body balloon){
//Can i start a new thread which is having a loop until the world become release for destroying the body
//Or
//Do i need to add the body to a deletionArrayList to destroy it.
}
Upvotes: 2
Views: 908
Reputation: 3397
I believe the problem is that you are trying to remove the body during a collision callback.
From the Box2D Manual:
It is tempting to implement game logic that alters the physics world inside a contact callback. For example, you may have a collision that applies damage and try to destroy the associated actor and its rigid body. However, Box2D does not allow you to alter the physics world inside a callback because you might destroy objects that Box2D is currently processing, leading to orphaned pointers.
That is to say, you should not destroy bodies inside of a collision callback.
You should store off the references to the bodies you wish to destroy and destroy them using the World reference after the update cycle.
Was this what you needed?
Upvotes: 2