In my 2d platformer with libGdx, my maps are quite large (eg. 200×200). The player character is only 2 tiles high. I use Box2d for collision, so in my tiled map I have layers for terrain, collectables, enemies and triggers. I parse the map and create bodies for each and every object in the map.
All bodies are initially set to inactive and I use culling to activate box2d bodies, update their related objects and for drawing. The sprite list has to iterate through all map objects to enable culling. This works fine. However there are too many unnecessary objects in memory because the player still didn’t reach a certain area of the map.
So my idea was to partition the map (eg. 40×40). The map is parsed as above but each object is now assigned a partition. Objects with a partition will not added to the sprite list. So now instead of 100′s of objects there will be only 10′s. When the player moves in the map, he enters a new partition, and its objects will be moved to the sprite list.
This will solve the problem of having a smaller sprite loop that needs to be looped over every frame.
However, in the above, parsing the map is still creating map objects and their box2d bodies.
Would I achieve better performance if objects and box2d bodies are created when the player enters the partition? However, the GC will kick in to dispose of the shape instances used to create the box2d bodies. Correct?
Maybe using partitioning with pooling of disposable map objects (eg. collectables and enemies) might avoid that. But for objects like terrain and triggers, there will be no pooling because they will exist as long as the level.
Does this look like a sound solution?