Good day, Habrovchane! In this article I want to talk about a simple and fast way to generate a landscape. Before we proceed to the analysis of the algorithm itself, I would like to note that I did not notice this algorithm in relation to the generation of the landscape, however, a similar algorithm for generating levels was described in the article, the link to which will be at the end.
In what situation is the algorithm convenient?
Recently faced with the task: to write a simple strategy with a three-dimensional landscape. Since I currently have little programming experience in C ++, my attempts to write "diamond-square" ended up with errors out of the blue (the link to the article on "diamond-square" will also be at the end). It required an easy-to-write algorithm that did not give a realistic landscape, so this method will help first of all beginners.
')
Algorithm and result
Before describing the algorithm itself, I will share its results:
The algorithm is that the program in random coordinates fills the map with random-sized rectangles. The map has the form of a two-dimensional array, representing the map of the heights of our landscape.
For simplicity, create a rectangle structure:
struct tRect { int x1, y1, x2, y2; }
The variables x1 and y1 are the lower left coordinate of the rectangle, x2 and y2 are the upper right.
Let be:
- Our map is presented as an array of HM [mapsizex] [mapsizey];
- mapsizey and mapsizex - variables that determine the size of your map;
- genStep is a variable responsible for the number of our rectangles;
- zscale - a certain coefficient of stretching the card in height. You can replace the number.
- recSizex and recSizey - limits the size of the rectangle.
Now you need to fill our map with rectangles:
for (int i=0; i<genStep; i++) { genRect.x1 = rand()%mapsizex; genRect.y1 = rand()%mapsizey; genRect.x2 = genRect.x1 + recSizex / 4 + rand()%recSizex; genRect.y2 = genRect.y1 + recSizey / 4 + rand()%recSizey; if (genRect.y2 > mapsizey) genRect.y2 = mapsizey; if (genRect.x2 > mapsizex) genRect.x2 = mapsizex; for (int i2 = genRect.x1; i2<genRect.x2; i2++) for (int j2 = genRect.y1; j2<genRect.y2; j2++) Map.HM[i2][j2]+= float(zscale) / float(genStep) + rand()%50 / 50.0; }
The relief from the screenshot was obtained by the values:
genStep = 1024
zscale = 512
mapsizex and mapsizey = 128
recSize = 10
Then you display the map in any way you can. In my case, openGl + glfw.
Advantages and disadvantages of the algorithm
Benefits:
- Simplicity and speed in writing the algorithm itself
- Algorithm execution speed
Disadvantages:
- Primitiveness
- With a small step of filling the map, the landscape becomes "square"
- There is no possibility to break the landscape into biomes in the course of the height map generation
This method, as mentioned above, is primarily suitable for beginners and people who are very limited in time.
I hope this article was helpful to you.
→
Article about the generation of game levels
→
Article about "diamond-square"