I would like to implement the generation of a three-dimensional maze, something like this
Nothing good was found on this topic. Does anyone have any ideas on this topic?
I would like to implement the generation of a three-dimensional maze, something like this
Nothing good was found on this topic. Does anyone have any ideas on this topic?
To begin with, let's decide what the maze is, i.e. We give the first definition of a labyrinth, which we subsequently modify slightly. I will consider the two-dimensional case. Three-dimensional in all its variations is constructed exactly the same.
Definition A maze on a plane will be called a connected undirected graph without loops, each vertex of which has no more than 4 edges.
It is quite easy to draw an analogy of the nxm grid-labyrinth and the labyrinth. How to do it? It is enough to look at the cell and notice that each cell has 4 neighbors (we don’t take boundary cases):
Now you can remove, for example, one neighbor. The usual situation in the maze:
Each cell can be associated with a vertex. The presence of a neighbor - to connect with the edge. In such a case, it is rather obvious that the definition is more or less correct. At first glance, it is not clear: for each labyrinth, it is possible to choose a labyrinth grid, exactly like vice versa. Here we need a rigorous proof, which does not make much sense.
Generally speaking, to generate the labyrinth itself, it is not necessary to build the graph explicitly. I just focused on this in order to give free rein to your imagination. Building a graph can be useful for more detailed study of the issue. It may be important to you that the maze has any properties. Maybe you want to introduce some kind of "density" or branching of the maze. In this case, graphs can come to the rescue.
Definition The boundary of the lattice or simply the border is the vertex whose coordinate on the lattice is: (x, m), or (n, x), or (1, x), or (n, x), where x is any number from the intervals [1, n] or [1, m].
Now let's move on to the labyrinth generation. Make it so. Define the matrix nxm . Choose one point on one of the borders. After this, we run from this point a detour into the depth within the lattice.
For each maze point we will take turns choosing each of the 4 directions and randomly, with probability p , decide whether there is a passage in this direction or not. We will carry it out until the total number of points on the border is at most k . As soon as this happens, we stop our actions.
By doing actions in this way, one can get a very uneven maze. Therefore, it is possible to slightly change the stop criterion. We will carry out the process until the total number of points on the border is no more than k and on each of the borders (of the whole border, of course, 4) there will be at least k_i points.
Now we need to choose to choose the beginning and the end. You can, of course, take any 2 points on the border, but in this case, it may happen that the path from the beginning to the end will be trivial (that is, too simple and short). In order to avoid this, you should choose the start and end points at a distance from each other not less than d .
Thus, we have built a maze generation algorithm with parameters: k , k_i (4 pieces), p , d .
I advise you to pay attention to such a thing as the theory of percolation . This theory implements the process I described above.
I have three implementations of the percolation process on python, hardware and pluses. You can look at them here: 1 , 2 , 3
The simplest thing that can come to mind is to generate a 3-dimensional array through Random (), if you write in Java.
int n = 10; int[][][] arr = new int[n][n][n]; for (int i=0; i<n; i++){ for (int j=0; j<n; j++){ for (int k=0; k<n; k++){ Random random = new Random(); Integer r = random.nextInt(2); if (r != 0) { arr[i][j][k] = 1; }else{ arr[i][j][k] = 0; } } } } If one, then there is a passage, otherwise there is no passage. At the expense of drawing it in 3d model depends on what you write, you can use jPCT in Java.
Source: https://ru.stackoverflow.com/questions/753305/
All Articles