I thought I would make a shot at automatic planet tile generation for Space Miner. This would save a lot of design time and open interesting opportunities in the future for automatic generation of random planets.
Generating a tilemap in LibGDX turns out to be quite easy, here is a small code snippet to help you get started:
TiledMap map = new TiledMap();
MapLayers layers = map.getLayers();
TiledMapTileLayer layer1 = new TiledMapTileLayer(100, 100, 16, 16);
//Create cells and use them to fill in the layer
Cell cell = new Cell();
Texture text = new Texture(“data/PlanetSurface/EarthTiles.pix.png”);
TextureRegion tr = new TextureRegion(text, 0, 0, 16, 16);
layer1.setCell(0, 0, cell);
//Add more cells and setCell here
I have tested this code with one celltype pasted onto a 1000×1000 map on my cheapish android phone. The map was generated quite fast, and the fps was smooth.
Ok, so now we know how to generate a TiledMap without a pre-defined map file. How would we go about generating a whole planet?
What I will test is to divide the planet into x amount of areas based on the center of planet to surface angle.
Areas divided into degrees with 0 facing up:
A1 = 337.5 to 22.5
A2 = 22.5 to 67.5
A3 = 67.5 to 112.5
A4 = 112.5 to 157.5
A5 = 157.5 to 202.5
A6 = 202.5 to 247.5
A7 = 247.5 to 292.5
A8 = 292.5 to 337.5
Each Area (A1 – A8) will have its own flat surface tiles. To build stuff like mountains, tiles from neighboring Areas will be used.
Possible Areas to use tiles from when generating the surface inside a specific area:
A1 => A7, A8, A1, A2, A3
A2 => A8, A1, A2, A3, A4
A3 => A1, A2, A3, A4, A5
A4 => A2, A3, A4, A5, A6
A5 => A3, A4, A5, A6, A7
A6 => A4, A5, A6, A7, A8
A7 => A5, A6, A7, A8, A1
A8 => A6, A7, A8, A1, A2
Adding more Areas would make the planet more smooth, but 8 is probably good as a starting point.
The start of generation will be to select a certain height from center at 0 degrees (middle of A1) and select the flat tile from A1 there. From there the automatic generation would place surface tiles moving clockwise until a full revolution is done. Care must be taken that the last tile will fit into the first one.
From the first tile, a random selection of the next tile will occur from the list of possible tiles to select. Depending on the height from centrum of the planet, increasing height tiles will be chosen with a higher/lower ratio than decreasing height tiles. This should generate planet surfaces with mountains and valleys.