Reputation: 2575
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:
0x0000
, so it doesn't collide with any other object. This actually fixed the problem (or at least I couldn't reproduce it anymore). But this doesn't help very much, because an arrow that doesn't hit anything isn't very usefull in the game...CATEGORY_OBSTACLE
) also seemed to fix the problem, but I have no idea why, and still this isn't realy a solution...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:
GameScreen
has a render
method, from which the World.step
method is called (the one that causes the endless loop).Dwarf
has an executeSpecialAction
method, which starts the creation of the arrow through some factory methods.ProjectileFactory
has a createProjectile
method, that creates the arrow and adds it to the world.PhysicsBodyCategories
class. The arrow's fixture uses the mask MASK_PLAYER_ATTACK
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:
DesktopLauncher
in the desktop
-subproject.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
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