I'm using a LevelManager to load Tiled maps in my libGDX game, which stores:


  • final Size of map in Tiles
  • final Size of map in Pixels
  • final Size of an individual tile
  • TiledMap ACTIVE_MAP
  • World WORLD

When the map is loaded, it creates a new World and stores it in the static WORLD, later populating it with static bodies based on objects found in the map's Collision layer.

My reasoning for using static is down to the fact that logically, only one map will be playable at any one time, however I've got it in my head that only constants and singletons should be static, so this feels strange for me. Is there a better, more efficient way of handling this?

Should I have one World per Level, or a global World independent of what TiledMap is active?

In other words, should I initialize WORLD once and leave it as is, making it final, or continue what I am doing and initialize it as a new World whenever a new TiledMap is loaded?

How do I correctly dispose of all bodies within the World, essentially clearing it?

I'm going to have to do this every time I load a new map, regardless of whether or not WORLD is global.

  • \$\begingroup\$ constants and singletons should be static, this is not true, constants are static final. Static has nothing to do with constant, it is just a shared variable all across your LevelManager class, its value can change. If you follow java naming conventions you should name it activeMap not ACTIVE_MAP, since its not final, just static. Also in java you don't have to dispose anything. It has an automated garbage collector. \$\endgroup\$
    – dimitris93
    Commented Jul 13, 2015 at 14:27
  • \$\begingroup\$ When I say dispose, I mean clear the world of all bodies, otherwise when I load a new level, bodies from the previous level will persist. Also, I meant static final, and while this question isn't about naming conventions, I thank you for pointing that one out, and will modify it. \$\endgroup\$
    – driima
    Commented Jul 13, 2015 at 14:45
  • \$\begingroup\$ There is a world.dispose() function for that. Does it not do what you require ? If not, you could iterate through every single body and call world.destroyBody(body), even though I am pretty sure that is what world.dispose() should be doing and perhaps even more. \$\endgroup\$
    – dimitris93
    Commented Jul 13, 2015 at 14:46
  • \$\begingroup\$ Unfortunately, no. The dispose method causes my game to crash. I have overcome this however, and will provide details in an answer. \$\endgroup\$
    – driima
    Commented Jul 13, 2015 at 14:54
  • \$\begingroup\$ Probably because you are disposing the Player's body itself, since it is in the same world. \$\endgroup\$
    – dimitris93
    Commented Jul 13, 2015 at 14:55

1 Answer 1


I took some time to think about the issue and realized that logically, there is no purpose for disposing the World or re-initializing it whenever a new Level is loaded it seems a lot better to dispose and reinitialize the world when a new Level is loaded - thanks to @Shiro for helping me see this. I won't have to manually clear all of the World's bodies, forces, etc.

I created a WorldManager class which contains the World and a method to "clear" it:

public final class WorldManager {

    public static World world;

    private WorldManager() {}

    public static void clearWorld() {
        if (world != null) {

        world = new World(new Vector2(0, 0), true);

In my LevelManager class, before loading a specified level, I make a call to WorldManager.clearWorld()

  • \$\begingroup\$ destroyBodies destroys joints as well. I am pretty sure what you are doing is exactly what world.dispose() does. Are you sure you are re-instantiating your world, after you dispose it ? world = new World(new Vector2(0, -10), true); \$\endgroup\$
    – dimitris93
    Commented Jul 13, 2015 at 15:19
  • \$\begingroup\$ As I said, I shouldn't need to reinitialize it. I don't see the purpose of doing so. World#dispose() doesn't do this. \$\endgroup\$
    – driima
    Commented Jul 13, 2015 at 15:22
  • 1
    \$\begingroup\$ This is a native function that calls code written in C language. You can clearly see that the world gets deleted. delete world;. So yes, you should need to re-instantiate your world object. world.dispose() destroys all bodies and the world instance itself. \$\endgroup\$
    – dimitris93
    Commented Jul 13, 2015 at 15:23
  • \$\begingroup\$ I understand where you're coming from now. Thanks! \$\endgroup\$
    – driima
    Commented Jul 13, 2015 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .