Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

on Android application using libgdx, when I use World.destroyBody( Body body ) method, once in a while the application suddenly shuts down.

Is there some setting I need to do with body collision or Box2DDebugRenderer before I destroy bodies?

Below is the source I use for destroying bodies.

private void deleteUnusedObject( ) {
    for( Iterator<Body> iter = mWorld.getBodies() ; iter.hasNext() ; ){
        Body body = iter.next( ) ;
        if( body.getUserData( ) != null ) {
            Box2DUserData data = (Box2DUserData) body.getUserData( ) ;
            if( ! data.getActFlag() ) {
                if( body != null ) {
                    mWorld.destroyBody( body ) ;
                }
            }
        }
    }
}

Thanks

share|improve this question
2  
Do you have any error messages? Why would you check if the body != null in the inner if statement when it is first used in your first if statement body.getUserData() != null. Second, what does mWorld.destroyBody( body ); do? Do you have any other threads running? –  Luis Estrada Mar 2 '12 at 15:01
    
Do you do any contact filtering? When do you call deleteUnusedObjects? Are you sure youre not storing and using any references to destroyed bodies somewhere? –  Majd Mar 4 '12 at 12:16
add comment

5 Answers

I also face the same problem in libgdx. But I found an alternate solution about this is. The following is the way to work same as destroyBody

world.step(0,0,0);
body.setActive(false);

this will made body inactive in the physics world and in the dispose method we destroy the body.

share|improve this answer
add comment

Make sure you are not trying to destroy the body inside of world.step(). If you are destroying it from a ContactListener or something similar, you'll have to mark the body to be destroyed later and not during world.setp().

share|improve this answer
add comment

If you are trying to destroy the body in a CollisonEvent, this happens mid-step so you can't do that. Your best bet is to build a list of objects you would like to destroy and have them removed next frame.

share|improve this answer
    
This is most likely the problem. @minami I did post a solution here that you would need to call outside the world.step() gamedev.stackexchange.com/questions/27113/… –  Rubber Mallet Aug 3 '12 at 20:57
add comment

In your render method check if the world is locked, with world.islocked(), if it isn´t locked, and call immediatly the destroy body.

I suppose that you error is an assertion from Box2D

render(float arg0)
{
       if(!world.isLocked())
       {
            destroy your bodies here
            .
            .
            .

        }
}
share|improve this answer
add comment

This is the best way to do it, i read it in the Box2D manual. You have to call this just after CURRENT_WORLD.step(..,...,...);

Iterator<Body> i = mWorld.getBodies();
Body node=i.next();
while (i.hasNext()) {
    Body oBj=node;
    node=i.next();
    Box2DUserData data = (Box2DUserData ) oBj.getUserData();
    if(data!=null  &&  !data.getActFlag()){
        mWorld.destroyBody(oBj);                
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.