- Home /
Fill in the small empty gaps (Maze Generator)
So I created a generator that is able to generate different mazes using random seeds. Now the problem I am running into is how to fill in the tiny gaps that it creates when doing different maps. I want to leave the bigger area for a player to move around.
Code:
public float cellSize = 1;
public int XSize = 20;
public int ZSize = 20;
[Range(0,100)]
public int randomFillPercent;
public GameObject Cell;
public string seed;
public bool useRandomSeed;
public Transform MazeHolder;
private GameObject[,] Maze;
private void Start()
{
GenerateMaze();
}
private void GenerateMaze()
{
Maze = new GameObject[XSize, ZSize];
CreateMaze();
}
private void CreateMaze()
{
if (useRandomSeed)
{
seed = Random.Range(0,999999999).ToString("0");
}
System.Random pseudoRandom = new System.Random(seed.GetHashCode());
for (int x = 0; x < XSize; x++)
{
for (int z = 0; z < ZSize; z++)
{
if (x == 0 || x == XSize - 1 || z == 0 || z == ZSize - 1)
{
Maze[x, z] = (GameObject)SpawnCell(x, z);
}
else if (pseudoRandom.Next(0, 100) < randomFillPercent)
{
Maze[x, z] = (GameObject)SpawnCell(x, z);
}
}
}
}
private GameObject SpawnCell(int X, int Z)
{
float newX = cellSize * X;
float newZ = cellSize * Z;
GameObject newCell = Instantiate(Cell, new Vector3(newX, 0, newZ), Quaternion.identity);
newCell.transform.SetParent(MazeHolder);
return newCell;
}
Answer by Glurth · May 11, 2018 at 02:56 PM
An image of the "tiny spaces" would help confirm, but I suspect this issue is due to the fact that the Mesh you use for each cell is NOT precisely cellSize, in world space. From the description I'd guess it is slightly smaller that cellSize, or maybe slightly offset from model-space center.
You could use the [bounds]
dox link member of MeshRenderer
(assuming the cell has one) e.g. cell.GetComponent<MeshRenderer>().bounds
to get the actual, rendered size of the cell, in worldspace. You can then use this value, instead of the hard-coded '20'.
Its these areas that I highlighted with red. As for the cellSize, it should be distance between each cell, which I am just using the standard cube from Unity. As for the mesh, its multiply cubes, not 1 huge mesh.
[1]: /storage/temp/116700-highlighted.png
Ah, so you want an algorithm to fill an area. $$anonymous$$y answer above does NOT apply.
Here is a link to "flood-fill", which is a way to fill in those tiles. The trick is going to be knowing if a given area SHOULD be filled or not. You may want to make it TWO passes, where the first one only COUNTS the empty tiles to see if they are above or below your "fillSizeThreshold", then on the second pass, do the actual fill only on areas below the fillSizeThreshold.
I get what your saying, but would you know how to integrated it into the code I created?
Your answer
![](https://koobas.hobune.stream/wayback/20220612162447im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Generating 2D collisions for maze 0 Answers
[Help]How can I make my maze makes steps as it generate ? 2 Answers
how one would go about using Triangulator in javascript? 0 Answers
Procedural terrain generation ? 0 Answers
Dyanmic Platforms and Erasing Geometry? 0 Answers