I am making a clone for the BreakOut game. I'm using Box2D and Libgdx for it. In the game, one scenario is that there could be 2-3 balls at one time. Now the problem occurs when 2 or more balls collide with the same object at the same instant. I'm simulating this by creating 2 balls overlapping each other and having velocities in both directions same, so they move like one body. When they collide I get this error : AL lib: alc_cleanup: 1 device not closed
. I searched a little and modified my code by putting the condition if (!world.isLocked())
. Now it just gets stuck and I have to end the process using task manager. Here is my code inside collision listener :
if (contact.getFixtureA().getUserData() instanceof Ball) {
Ball ballTemp = (Ball) contact.getFixtureA().getUserData();
if (contact.getFixtureB().getUserData() instanceof Brick) {
brickTemp = (Brick) contact.getFixtureB().getUserData();
if (brickTemp.getPower() != null)
brickTemp.getPower().update();
if (ballTemp.getX() > brickTemp.getPosition().x
+ brickTemp.getWidth()) { // collision on the right
ballTemp.goRight();
}
if (ballTemp.getX() < brickTemp.getPosition().x
- brickTemp.getWidth()) { // collision on the left
ballTemp.goLeft();
}
if (ballTemp.getY() > brickTemp.getPosition().y
+ brickTemp.getHeight()) { // collision on the top
ballTemp.goUp();
}
if (ballTemp.getY() < brickTemp.getPosition().y
+ brickTemp.getHeight()) { // collision on the
// bottom
ballTemp.goDown();
}
bodiesToDestroy.add(contact.getFixtureB());
}
}
I have another if statement where fixtureB is Ball and fixtureA is Brick with similar code. And the part in MainScreen.java where I'm doing the cleaning up job is :
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
if (!world.isLocked()) {
destroy(MyContactListener.bodiesToDestroy);
if (MyContactListener.removeBall != null) {
balls.removeValue(MyContactListener.removeBall, true);
}
if (MyContactListener.power) {
MyContactListener.powerBrick.applyPower(delta);
powerStartTime = System.nanoTime();
}
}
public void destroy(Array<Fixture> bodiesToDestroy) {
Array<Fixture> temp = new Array<Fixture>();
for (Fixture bodyToDestroy : bodiesToDestroy) {
if (bodyToDestroy.getBody() != null) {
world.destroyBody(bodyToDestroy.getBody());
temp.add(bodyToDestroy);
}
}
MyContactListener.bodiesToDestroy.removeAll(temp, true);
temp = null;
}
applyPower
method just creates two new instances to Ball class with the same co-ordinates and velocities.
bodiesToDestroy
is defined as public static Array<Fixture> bodiesToDestroy = new Array<Fixture>();
It's unlikely that two balls collide with the same object just at the same instant, but nonetheless I have to take care of such an occurrence. Please suggest how this should be handled.
AL lib: alc_cleanup: 1 device not closed
is something LibGDX seems to spit out nearly every time it exits. Does it kill your application when you run it without theisLocked
guard? \$\endgroup\$