Skip to main content

Random Map Generator

A lot of games use Perlin Noise to generate maps, so I decided I would give it a try. One of the most popular games to do this is Minecraft which uses a similar noise function to create its map. 
Landscape generated in MineCraft
The basic premise is that you use the noise to create a height map on a 2d grid. Every point on a grid is assigned a random value and that value is its height. If you where to just use a normal random number function to assign the values they would be all over the place. It wouldn't make any natural looking shapes. That is why Perlin Noise is used. It creates smooth gradients between random points. This make the height changes less jarring and look natural.

Perlin noise vs white noise
 Unity already has a Perlin noise function so I did not have to implement my own. All I did at first was make a 2d array and assign each index a height value. Every height above a cutoff is land and everything below is water, this is the result.

It was a lot easier than I expected to start getting good looking shapes. After that I added more cutoffs just to make things interesting. I ended up having 6 different colors. They are suppose to represent deep water, water, sand, grass, rock, and snow. Here are some maps generated with those.



The next thing I decided to do was add some rivers. The first idea I had was to start generating rivers on high points and make them move down. The idea was that it would be like a river forming at a top of a mountain and then flowing down into a lake. It turned out that it looked more natural if I did the opposite and had rivers start at lakes and move upwards. The river function starts by looking for water in the array. Then when it finds it there is a chance to make a river. If it decides to make a river it moves to all adjacent spaces to determine if any of them have a height value higher than that of the current one. If it does it moves to the highest adjacent tile and keeps going recursively. It only stops if it hits the edge of the map or if there are no higher adjacent tiles. Before it makes the river it also chooses a random number in a small range to choose the width of the river. This way it can generate large rivers and little streams to make things look more natural.


If the river is high enough it will turn to an ice color
Next I decided to add some trees. It picks a random place to put a tree and then if it is on grass and there are no other trees in the area it will put one down. It keeps doing this until it has generated a predetermined max number of trees or until it fails to find space for a tree after some many iterations. Each tree is also assigned a random scale and rotation. For the trees I just made a quick drawing.
Tree Drawing



Right now I can only generate one chunk of the map but it should be possible to keep generating more and more chunks and they should flow together. When ever a river flows off the map it saves the location so the next chunk will know to start a river there. I think i'm going to use these maps for a top down exploring game so I will try to use this blog to track my progress.


Comments

  1. To generate rivers, read up about flow-accumulation. Like this: http://grass.osgeo.org/grass64/manuals/r.terraflow.html
    It's a reasonably fast method used to calculate where actual river are in real world data, and it will provide you with the most realistic river-paths possible. As a side-effect you'll get all possible lakes (nevermind their altitude).

    ReplyDelete
  2. Yeah, this community can help beginners who want to build houses, clans , flags and many more stuff's in minecraft.

    Previously I also face some difficulties while constructing houses in minecraft, Because minecraft is a square boxed game and if anyone wants to create circle in minecraft then it's not easy to make.

    But now some of online tools are also available to create minecraft circle and any beginner can also use that if he/she is facing difficulty while creating circle.

    ReplyDelete
  3. I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! Minecraft server list

    ReplyDelete
  4. I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! economy minecraft servers

    ReplyDelete

Post a Comment

Popular posts from this blog

Water Effect

This purpose of this post is to give some insight in to my 2d water effect. The effect  was made in unity and consist of one C# script and two shaders. Additionally this effect can be be broken down in to two parts. The reflection and the distortion. First we will look at the reflection Reflection A lot of the work of the reflection is done through the C# script. The script creates a new game object with the sprite renderer of the original object, mirrors it, and puts our water reflection shader on it. You can see it below. A couple things to note with this technique. First, everything done in this script could be done manually through the editor. It is only for convenience and does not have to be done at run time. You might want to add [ExecuteInEditMode] to this script if you plan on using it. Second, this is kind of a crude way of doing things. Animations, lighting, or anything else that effect your sprite will not get reflected. For animations you could use the script to

2d Book Distortion

The game I am working on allows the player to read and create magic books. Because of this, I decided it would be useful to create different effects that look liked they were happening on the book's pages. To do this I needed a normal map. I figured a book would not be too hard to model so I made this model in blender. Book Model The model didn't have to be perfect, because I only need it to render a normal map viewing it from the top. Normal Map That is all the work I needed to do in blender.  The art for the actual book is just for testing and I found it on open game art  here . I fudged with the normal map and the book art a little bit in gimp until they lined up nicely. The rest of the work I did in unity. Everything that  I wanted to appear on the pages I put on a separate layer, that I will be referring to as the page layer. This way I could render that layer with a separate camera. I ended up rendering that to a render texture that was applied to a

Chromatic Aberration

I made a chromatic aberration shader after not finding one I liked online. You can see the effect above on the sprite bottle to the right. My goal was to make a stylized chromatic aberration effect that I could apply to any sprite. The effect works by separating the rgb channels and displacing them in different directions. Below you can see the blue and red channels getting displaced in opposite directions on the x axis.  You can control the displacement easily with a script to change how extreme the effect is. I personally think the effect looks best when used subtlety but I could see some cool applications were it is controlled dynamically and combined with a screen shake or something to show a large impact. Here is the effect applied more subtlety on a cool rogue sprite Here is a link to the shader Here is an example C# script to control it