3
\$\begingroup\$

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

\$\endgroup\$
  • 2
    \$\begingroup\$ 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? \$\endgroup\$ – Luis Estrada Mar 2 '12 at 15:01
  • \$\begingroup\$ 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? \$\endgroup\$ – Majd Mar 4 '12 at 12:16
  • 1
    \$\begingroup\$ Possible duplicate of What is the proper way to remove a box2d body from the world in libgdx? \$\endgroup\$ – Anko Nov 7 '15 at 19:13
4
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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().

\$\endgroup\$
0
\$\begingroup\$

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
            .
            .
            .

        }
}
\$\endgroup\$
0
\$\begingroup\$

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);                
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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