- Home /
Question by
jsams_unity · Mar 23, 2020 at 11:15 PM ·
procedural generationoverlappingdungeon
Dungeon rooms overlapping
I've been trying to figure this out but can't see where I've gone wrong.
I am creating a dungeon generator that uses a grid made up of cells. I want to create rooms that are comprised of cells and that don't overlap rooms that have already been generated. The issue is that rooms do overlap.
Here is my overlap function:
public bool IsOverlapping(Room other)
{
return !((this.right < other.left)
|| (this.left > other.right)
|| (this.top < other.bottom)
|| (this.bottom > other.top));
}
When I'm generating the rooms, I use the below code to check for overlaps:
for (int i = 0; i < maxRoomPlaceAttempts; i++)
{
if (rooms.Count >= maxRoomCount)
break;
Vector2Int roomSize = new Vector2Int(Random.Range(minRoomSize.x, maxRoomSize.x + 1),
Random.Range(minRoomSize.y, maxRoomSize.y + 1));
int startRow = Random.Range(0, rowCount - roomSize.y);
int startCol = Random.Range(0, colCount - roomSize.x);
int endRow = startRow + roomSize.y;
int endCol = startCol + roomSize.x;
Room newRoom = new Room(this, startCol, startRow, endCol, endRow);
foreach (Room other in rooms)
{
if (newRoom.IsOverlapping(other))
{
Debug.Log("New room is overlapping, skipping...");
goto NEXT_ROOM;
}
}
rooms.Add(newRoom);
NEXT_ROOM:;
}
As far as I can tell it should be avoiding all overlaps but it always gives me several rooms that overlap. Where am I going wrong?
Comment