Tobias
Tobias

Reputation: 2575

libGDX - endless loop in Box2D world step

I'm working on a little game with libGDX, that uses Box2D for collision detection. This was working fine, till I added an arrow to the game, so the player can shoot. Since I added this feature I'm facing the problem that the game gets stuck from time to time, and doesn't react anymore.

I think the reason for this behaviour is an endless loop somewhere in the Box2D world step. When I stop the execution using the debugger the stopping point is always in the World.step(float, int, int) method. Unfortunately this is a native method so I can't find where the problem is exactly:

// from com.badlogic.gdx.physics.box2d.World

public void step (float timeStep, int velocityIterations, int positionIterations) {
    jniStep(addr, timeStep, velocityIterations, positionIterations);
}

private native void jniStep (long addr, float timeStep, int velocityIterations, int positionIterations);

The problem:

Sometimes when using the new "shoot arrow" feature the execution seems to stop and the game just freezes. It is realy hard to reproduce, therefore I can't realy tell what the real root cause is. It only appears when adding a new arrow to the Box2D world, but the problem does not appear all the time.


What I've tried so far:


The code:

Since I couldn't seem to even reliably reproduce the problem, I also wasn't able to create a minimal reproducable example. I can only point to the GitHub repo of the game. Sorry for this :( The current code is placed in the branch projectile_bug.

To explain the code a bit:


Steps to reproduce:

Since I don't realy know the root cause of the problem, the bug can only be reproduced by shooting some arrows over the map:


The Question:

I'm not sure what causes this bug and I'm still quite new to Box2D. If anyone has an idea on how to fix this or knows a workarround for this problem, it would realy help me a lot. Also if you know about some related, reported bugs or anything like this, it could also help.

Thanks in advance.

Upvotes: 0

Views: 534

Answers (1)

constanze
constanze

Reputation: 136

Okay, I bite for it. In my experience box2d crashes or hangs most of the time when bodies or fixtures are not correctly destroyed, or when there are some dangling references to destroyed bodies. So I just tried to remove this statement form your code PhysicsWorld.getInstance().removeBodiesAndFixtures(); right after the step function, and voila, it works. Nice game btw. This is of course not the solution, but it might give you a hint where to search for the root cause.

Upvotes: 1

Related Questions