Building my own planet
Published a year ago
The challenges of a spherical world
When your world is flat, Unity provides you with an out of box solution for almost anything you may need, however making a planet is not that simple, a planet implies challenges, some are obvious, others are not.
The planet itself was my first challenge; of course you can solve such problem with a sphere, maybe with a texture to fake terrain. That will work if you look at the planet form certain distance, but if you are on the planet’s surface, you cannot fake hills and valleys with a texture, actual hills and valleys are required in order to make them feel real. I could’ve made it on a modeling tool, but since I’m not an artist and have no experience in such tools, that felt like my last resource option. At the time I started remaking Tanksmash, I couldn’t find any tutorials to create a procedural planet, at least not anyone that yielded the results I wanted. I learned how to make procedural terrains with this excellent tutorial by Sebastian Lague, but as most of the solutions you can find out there, it was thought to work on flat worlds. I had to figure out a way to make it work on spherical surfaces. The first step was to make my own procedural sphere, the first options I explored generated too many triangles, until I recalled the way the earth is divided in parallels and meridians, I used this approach which proved to be the best solution for me, my sphere is built on a series of parallels divided into meridians. Starting at the North Pole, each parallel contains 4 meridians more than the previous, all the way to the equator, then it reduces 4 meridians every parallel all the way to the South Pole. With that solved, I just created a height map based on Sebastian Lague's tutorial and applied to the mesh’s vertices, but that didn’t work as I expected, there was a very notorious seam at the Greenwich meridian, I tried severl approaches to make the seam disappear, but it was quite persistent. But when I finally came up with a solution, another problem aroused: Mirrored hemispheres, seen from above or below the planet looked great, but from a lateral position it was obvious that the south hemisphere was a mirror of the north hemisphere. It took me several weeks  and a lot of trial an error to make the planet look convincing enough, without seams, without mirrored hemispheres, and without sudden changes of terrain roughness.
Once I had my planet’s mesh, the second challenge was gravity, maybe not hard enough to call it: “a challenge”. There’s also a tutorial by Sebastian Lague which explains you how to do it, but in order to make it work the way I wanted required some extra effort.
With the planet done, I could spawn my tank on it and drive around, but the enemies didn’t move… oh, I know what’s wrong, I forgot to bake the NavMesh… but wait… Unity’s NavMeshes are not designed to work on non flat worlds, so navigation on a bumpy spherical surface became a problem. And again I had to building my own solution. In a real tank fight, tanks driven by humans, won’t always follow the optimal path, a human operator will look where the enemy is, then point their cannon in that direction and fire, if the enemy is away from the fire range they will go straight to the target avoiding obstacles as they find them, simple and easy. So my current algorithm is based on obstacle avoidance only, it works by casting spheres (almost) all around the tank, if the sphere in the direction of the target hits nothing, then the tank goes on that direction, otherwise it chooses the direction indicated by the nearest sphere which didn’t hit anything within the AI’s view distance.
There were also things that didn’t were obvious to me at first glance, one of them was the sky; Skyboxes work great on flat worlds, but they are horrible on any kind of world that requires to look down the horizont. I solved this problem with a SkySphere, which is basically a sphere with inverted normals, my first approach was to simply apply a texture to it, but soon I realized I need to write my own shader in order to make it look good.
Another thing that you can easily overlook is the fact that placing objects on the surface of a sphere is not as simple as placing them of a flat horizontal surface, drag and drop just doesn’t work here, you need to apply certain rotation to the object according to its position on the sphere. To do it manually is not easy and time consuming if you want the object look naturally standing on the planet, and the bumpiness of the surface adds an extra level of difficulty, so in order to properly locate the objects in the surface of the planet I had to a add another option to my tools menu.
I’m still working on the planet and the game play itself, there are still problems I need to figure out how to solve, but all the biggest challenges had been solved, so I hope to have a playable demo of the newer version of the game by mid November 2016, the old version is still available at:, GameJolt and the Windows Store if you want a reference to compare.
Jorge Reyna Tamez
Game Developer - Programmer