# Randomly generated map is rotated by 90 degrees.

Hi

My team and I are creating a rogue like game, and I’m now working on generating the random layout of rooms.
The rooms are stored in a 2d array of game objects. I instantiate a room in position [2,2], then search an adjacent room to it and instantiate another room there for about 6 rooms.

After the layout is complete, I add bridge objects (for room traversal) on top of each room.

I printed the layout to the console. but for some odd reason, the layout I get on the in the game view is rotated by 90 degrees compared to the layout I get in game.

The actual problem:
You can see that the bridges are not attached to the correct sides.
I’m pretty sure that the printed layout is the correct one, because it matches up with the bridge locations. if your pair the rotated position of each room with its’ current bridge positions, everything matches.
This is the script that generates both the printed and actual layout.

generateRandomMap() - generates a random layout of rooms and tells each rooms if it has adjacent rooms.

printMapLayout() - prints the generated layout as a matrix to the console. Only rooms with are instantiated ones.

getAdjacencyList(int i, int j) - gives each room a list of which rooms adjacent to it are actually instantiated or not.

This is the script:

``````using UnityEngine;

public class LevelSpawner : MonoBehaviour
{
[SerializeField] GameObject room;
Vector2Int coordinates;
GameObject[,] rooms= new GameObject[5,5];

public void generateRandomMap()
{
coordinates = new Vector2Int(2,2);

for (int i = 1; i < 6; i++)
{
float offset = 1000;
rooms[coordinates.x, coordinates.y] = Instantiate(room, new Vector3((coordinates.x*100)+offset,coordinates.y*100,0),Quaternion.identity);
//str += (\$"[{coordinates.x} , {coordinates.y} ] ,");
coordinates = chooseNonOccupiedNeighbor(coordinates);

}

for (int i = 0; i < rooms.GetLength(0); i++)
{
for (int j = 0; j < rooms.GetLength(1); j++)
{
if (rooms[i, j] != null)
rooms[i, j].GetComponent<Room>().init(getAdjecencyList(i, j));
}
}
}
private void printMapLayout()
{
for (int i = 0; i < rooms.GetLength(0); i++)
{
string str = "   ";
for (int j = 0; j < rooms.GetLength(1); j++)
{
if (rooms[i,j] != null)
{
str += "[X]\t";
}

else
str += "[ ]\t";
}
print(str);
}
}

Vector2Int chooseNonOccupiedNeighbor(Vector2Int vec)
{
bool val;

int[] neighboorI = { vec.x-1, vec.x+1 };

int[] neighboorJ = { vec.y - 1, vec.y + 1 };

int randI;
int randJ;
Vector2Int rand;
do
{
val = true;
int r = Random.Range(0, 2);
if (r == 0)
{
randI = neighboorI[Random.Range(0, 2)];
randJ = vec.y;
rand = new Vector2Int(randI, randJ);
}

else
{
randI = vec.x;
randJ = neighboorJ[Random.Range(0, 2)];
rand = new Vector2Int(randI, randJ);
}

if (randI >= 0 && randI < rooms.GetLength(0) && randJ >= 0 && randJ < rooms.GetLength(1))
{
if (rooms[randI, randJ] != null)
val = false;
}
else
{
val = false;
}
}
while (!val);
return rand;
}

bool[] getAdjecencyList(int i, int j)
{
bool[] list = new bool[4];
if (rooms[i, j] == null)
return null;

if (i > 0 && rooms[i - 1, j] != null)
list[0] = true;

if (j > 0 && rooms[i, j - 1] != null)
list[1] = true;

if (i < rooms.GetLength(0) - 1 && rooms[i + 1, j] != null)
list[2] = true;

if (j < rooms.GetLength(1) - 1 && rooms[i, j + 1] != null)
list[3] = true;

return list;
}
}
``````

I’d love some help, as I’ve been at it for a while.
If more information is necessary, please tell me and I’ll provide it.
Thank you!

Pretty sure you want to use the for() loops in a different order:

• outer loop should be the GetLength(1) iterator

• inner loop should be the GetLength(0) iterator

You have it the other way around.

The other problem you will have is that if you are above the world looking down, then +Z increasing will be “up” in your view.

But the log console paints “downwards” as you iterate through time, eg each higher number is “down” in your view

So you need to invert one or the other. I would invert the log console by building the string in reverse and outputting it all at once.

Thank you for the reply!
the downward vs. upward thing had occurred to me, but that would cause it to be flipped 180 degrees and not 90.

and regarding the reversed iterators, it should not matter because the matrix is a square matrix, so its’ height and width are always the same.

I’ve managed to fix it by left shifting the array values in getAdjacencyList(), all values were raised by one and that fixed the issue.

I don’t really like this solution because it doesn’t fix the core issue that was there, but I just couldn’t find the real culprit.
Anyway, thank you!

This is only true if a square object cannot be emplaced in any number of 8 orientations.

I’m tellin’ you, it’s the mismatch between the iterator nesting (y inside of x) when line 16 above clearly shows (x,y) style position generation.

When you have a grid indexed by (x,y) and want to output it, you must do so with:

``````for y ...
for x ...
output (x,y)
``````

Source: I have cartesianed before.