Most games take place on some sort of map, and the choice of map type has a fundamental impact on gameplay. But how to choose? For this terraforming game we know the map needs to span an entire planet. The most common method of doing this is to make the world flat and wrap around the east-west direction. But this is actually a cylinder. Now I don't know about you but the planet I live on is a sphere. You could wrap around north-south like we did with east-west but this turns the world into a torus (also not a sphere). As it turns out this is the same problem cartographers have been dealing with ever since they started making maps. In fact they even proved that it's impossible to do perfectly. At this point you need to ask does this matter? Most games decided that the advantages of a flat map outweighed these downsides. But for this game I think we need a spherical map. Remember that the other map decision was to use a hexagonal grid. So we just need to cover the surface of a sphere with hexagons. So map projections, there’s a lot of them, and they all do the same thing, and none are perfect… But why do I care? Well, this is a game about terraforming planets so there needs to be some way to show that planet to the player. In my first post you can see the initial version of this. The planet is displayed as a flat map so I need to use some projection. But this has problems, it distorts the amount of area at each latitude, you can’t go over the poles, and I think it looks bad. The answer to all of this is just make the planet spherical instead of flat, essentially use a globe. So how to do that? Well, it turns out that if I want to keep the hex grid on the surface it’s As it turns out that's impossible. Thankfully there is a simple way around this problem, just add 12 pentagons to our hexagons. This is called a Goldberg polyhedron. Now making it! The first step is to construct an icosahedron. We do this by first defining the locations of all the vertices. Code Editor
Then connect the vertices into faces. Each line defines a face using the indices of the vertices in the vertex list. If we tell Unity to draw this we get a nice icosahedron. Still needs work to become a sphere. So we subdivide each edge and make new faces: Now there are more vertices but it’s still flat. To fix this we take the new vertices and push them out a bit: That’s better, let’s subdivide it more: 6 times! So now we have a nice sphere, but as you've probably noticed there still aren't any hexagons. Well we’re not quite there yet. First we need to break down the sphere into smaller chunks. But why? Unity has a limit to the number of vertices in a mesh. We’re not hitting that yet but as we make the terrain more complicated we might. The easiest way is to use each of the original 20 faces of our icosahedron as a chunk. Giving each chunk a color gives us a nice a beach ball. Now it's finally time for hexagons. Each vertex of the sphere gets replaced by a hexagon except the 12 original vertices. Those are our pentagons. Now we finally have the basis for our map. We'll make it look more interesting next time.
4 Comments
Max
6/18/2021 03:41:06 pm
How does the last part work?
Reply
Tim
6/18/2021 07:14:07 pm
For each vertex we already know the location of it's neighbors. Each set of two neighbors forms a triangle. This gives you 6 (or 5 for the 12 pentagons) triangles surrounding each vertex. You can then easily find the center of each triangle and that will give you the corner points of the new hexagon. You can then find the center vertex of the hexagon by once again averaging all the corner points. Here I also subdivided each edge of the polygon to have more mesh to work with later but it's not necessary.
Reply
Adrian
8/13/2021 12:25:11 pm
Is this Code public? so i can see how the chunks and hexagons made
Reply
Matt
9/10/2021 08:36:56 am
Nice work! I have a very similar code adventure I'm working through. Any chance I could invite you to a discord channel to discuss some things with this?
Reply
## Leave a Reply. |
## Archives## Categories |