Space Miner Automatic Planet Generation P8


Well, rendering half a million tiles with the LibGDX OrthogonalTiledMapRenderer is apparently not a good idea. FPS when drawing the whole planet was less than 1 on target, not nice.

After some research I found out that people who know what they are doing are mapping their static map layers to SpriteCache which they send to the GPU one time only. After some trial and error I had my half million tile large planet on screen with a very decent FPS.

You can use a SpriteCache by doing something similar to this:

 //Create your TiledMap m_map here.
 TiledMapTileLayer planetLayer = (TiledMapTileLayer) m_map.getLayers().get(0);
 SpriteCache spriteCache;
 spriteCache = new SpriteCache(1000000, false);
 for (int y = 0; y < planetLayer.getHeight(); y++)
   for (int x = 0; x < planetLayer.getWidth(); x++)
     Cell cell = planetLayer.getCell(x, y);
     if (cell != null)
       float tileX = (x - (planetLayer.getWidth() / 2)) * planetLayer.getTileWidth() * planetScale;
       float tileY = (y - (planetLayer.getHeight() / 2)) * planetLayer.getTileHeight() * planetScale;
       spriteCache.add(cell.getTile().getTextureRegion(), tileX, tileY, 0, 0, 16, 16, planetScale, planetScale, 0);
 normalCacheID = spriteCache.endCache();

 //spriteCache is done. It is now possible to use it when rendering
 //You need to define your OrthographicCamera cameraPlanet

Generally speaking I think rendering half a million tiles might be a bad idea anyway for low end phones. But this is a step in the right direction. If needed, smarter optimization can be performed.

Some possible optimization ideas:

  • Create an image of the rendered planet, show that image instead of the actual tiles when zoomed out. Show the tiles when zoomed in and only showing a part of the planet.
  • Create mega-tiles to fill the inside of the planet with a small amount of tiles. Keep the normal 16×16 resolution on surface tiles and tiles close to the surface.
  • Do not allow zoom out to show the whole planet. Not a good idea, as it would take away from player experience.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s