2D Map Generator

Hello! My first post in the forum :smiley:

I believe I’m doing lots of stuff in a not optimum way, but I’m learning. My idea is create a game with RPG characteristics, but to be sincere, I have not planned how everything is going to work yet. I know that planning is very important when starting a new project, but I’m still learning most of the stuff I’m trying to do.

So, about what I’ve done so far. I have a basic 2D engine with window, input, renderer, etc. What I’m working most of my time is the 2D map generator.

I’m using the SimplexNoise class from JOML to create a height map. My map has dimensions “width x height”, but my height map has dimension (width + 1) x (height + 1) for the following reason: each pixel of the final map is generated by 4 pixels of the height map. Why? Well, because I’m simulating a lighting shader. It works like this: for the pixel in the position (x,y), I’ll take the height map pixels of position (x,y), (x+1,y), (x,y+1), (x+1,y+1). From this I calculate the gradient vector of the map pixel and then the normal vector. I use a “Sun vector” as reference for the light direction for computing the lighting of each pixel.

Later, I use the average of the height map values of the pixels (x,y), (x+1,y), (x,y+1), (x+1,y+1) to define the height of the pixel in the final map. I give a base color to that pixel based on its height and then apply the lighting.

Screenshots of the developing (in hyperlink for saving space in the board):

That darker green area is where a forest should be

Same, with random generated fog

After that, I wanted to add random generated trees to the darker green spots. I used a method that I found for generating islands height maps. Basically, each tree is a very small island.

Screenshots of the developing (in hyperlink for saving space in the board):

Trying to create a random generated tree

Using height map to generate the leaves

More shading in the leaves

Now the edges blend into the background

Working the up scaling…

…and the down scaling. Testing the variation.

Testing the placement (size/distribution): bigger trees

Testing the placement (size/distribution): smaller trees

I can say that the final result was satisfactory.

Next (small) steps:

  • Created island as terrain: using similar method used to create the trees;
  • Simulate a daytime lighting: more orange at dawn, cold blue at night, brighter at midday, etc

Opinions & suggestions are very welcome!

Looks good so far, I wouldnt worry too much about optimizing your code while you are learning and experimenting, there is always time to refactor later.

Looks very pretty :slight_smile: Can’t wait to see more.

Thanks for the feedback!

Update:

I was able to accomplish the next steps set in the original post:

Screenshots (in hyperlink)

First, I did the island generation thing.

Then I realized that map in particular kind of looked like Europe/Asia, don’t you think? xD

After that, I went for the dynamic lighting stuff. I created a Sun class, which will hold the “time” of the world and from it, generate a vector from where the light is coming. I now use that to make the lighting shader. The sun also holds a “color scale” which gives the shader a specific color to each time. I simply overlay that color in every pixel.

Screenshots (in hyperlink)

Creating the island stuff messed with the normal maps. Notice how, for the normal map, the map is not a island.

Fixed it!

I also made a video show how the lights are working in that map:

YbxgCuxW2Pg

For me the results were great, but I lost some of the performance. Curiously, the FPS at night time is higher than a day time, I don’t know why.

Next steps:

  • Dynamic shadows: I don’t know how hard it will be, but looks not that easy.
  • Redo the trees: and also try to make them drop shadow in the map.