[SOLVED][LibGDX] Problem with removing and creating Bodies

I really didn’t find any suitable solution on the web for my particular problem.

I’m creating a game with LibGDX and run into problems when i try to create bodies. The Problem occurs quite randomly and i can’t track it down properly, but it seems it is only occuring when i create an Item from my Item class.

This is the error message:

http://pastebin.com/Qy0qaKBr

and this is the logfile created:

http://pastebin.com/jz6Tr6u5

As i read on some resources it seems like this error is caused when destroying a body while iterating over the bodylist. As i do not destroy bodies when this error happens its seems rather odd. Also i never figured out why the OpenAL library is involved here, as i don’t use any sounds yet…

I just found the solution to this problem. As allways it was pretty obvious but the error was not :P.

Previously i just flagged my entities for destruction and then iterated over them to destroy the bodies. This worked fine but the Problem was with the creation of the bodies (obviously doing that when the world was stepping).

The solution was, that i now add my entities to a list when they need to be created/destroyed. I create them before the step happens, and destroy them after the step finished.


createEntities();
world.step();
destroyEntities();

It was kinda hard to find the solution, as it’s only mentioned with 1 sentence at the end here: http://www.iforce2d.net/b2dtut/removing-bodies

“By the way, although this topic has focused on removing bodies since that is the most common case to cause problems, the creation of new bodies, and the creation and removal of fixtures must be dealt with in the same way.”