I have come across a strange problem when destroying bodies in JBox2D. I have no idea if this is a problem in the C++ version. When destroying a single body, there is no problem. But when I destroy all the bodies, I get a NegativeArraySizeException thrown in the Island.java constructor where bodyCapacity has dropped below zero. I noticed that, the first time I do this, everything is fine (my algorithm does not delete the m_groundBody created in the World.java constructor), but the second time, the world.getBodyCount() returns 0 rather than 1. The third time, the body count drops to -2, then -5. Here is the code I am using to destroy bodies.
I kind of used a temporary solution of including this after the m_bodyCount decrement in world.destroyBody()
I do this because my code always leaves the ground body intact.
Code:
private void selectAll()
{
for( Body body = world.getBodyList(); body != null; body = body.getNext() )
{
if( !body.isSelected() )
{
body.select();
bodies.add( body );
}
}
}
{
for( Body body = world.getBodyList(); body != null; body = body.getNext() )
{
if( !body.isSelected() )
{
body.select();
bodies.add( body );
}
}
}
Code:
private void deleteSelected()
{
for( int i = 0; i < bodies.size(); i++ )
if( !bodies.get(i).isGround() )
world.destroyBody( bodies.get(i) );
System.out.println( "bodies: " + world.getBodyCount() );
}
{
for( int i = 0; i < bodies.size(); i++ )
if( !bodies.get(i).isGround() )
world.destroyBody( bodies.get(i) );
System.out.println( "bodies: " + world.getBodyCount() );
}
I kind of used a temporary solution of including this after the m_bodyCount decrement in world.destroyBody()
Code:
if( m_bodyCount < 1 )
m_bodyCount = 1;
m_bodyCount = 1;
I do this because my code always leaves the ground body intact.