genaray
genaray

Reputation: 1180

2D Array - wrong "Wall " placement?

Good evening and merry christmas !

I wanted to make my own Dungeon-Generator. So i decided to first spawn in some empty rooms with placing the walls in the same step.

public void generateRoom(byte[][] dungeon){

    if(roomCornerDownLeft.x + roomWidth < dungeon[0].length && roomCornerDownLeft.y + roomHeigth  < dungeon.length ){


        setRoomFloor(dungeon);

        setWalls(dungeon);


    }

}


public void setRoomFloor(byte[][] dungeon){


    origin = new Vector2(roomCornerDownLeft.x + roomWidth / 2, roomCornerDownLeft.y + roomHeigth / 2);  


    roomHeigth += 1;   // +1 because of the WallBorders. So the Interior of the room is totally 5 tiles height

    roomWidth += 1;    // +1 because of the WallBorders. So the Interior of the room is totally 5 tiles width


    roomCornerUpRight = new Vector2(roomCornerDownLeft.x + roomWidth, roomCornerDownLeft.y + roomHeigth);


    for(int yPos = (int) roomCornerDownLeft.y; yPos <= roomCornerUpRight.y; yPos++){

        for(int xPos = (int) roomCornerDownLeft.x; xPos <= roomCornerUpRight.x ; xPos++){

            dungeon[yPos][xPos] = 1;

        }

    }



}


public void setWalls(byte[][] dungeon){

  // Vertical walls
  for (int i = (int) roomCornerDownLeft.x; i <= roomCornerDownLeft.x + roomHeigth; i++) {

        dungeon[i][(int) roomCornerDownLeft.y] = 2; // North wall

        dungeon[i][(int) roomCornerDownLeft.y + (roomWidth )] = 2; // South wall

  }

  // horizontal walls
    for (int y = (int) roomCornerDownLeft.y; y <= roomCornerDownLeft.y + roomWidth; y++) {

        dungeon[(int) roomCornerDownLeft.x][y] = 2; // North wall

        dungeon[(int) (roomCornerDownLeft.x + ( roomHeigth ))][y] = 2; // South wall

    }

}

But i approached some strange problems with it. Placing the "room floor" works without any problems. The position and size is just correct. But when i try to build a wall around it, it only works sometimes. Heres an example :

    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000002222222000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002111112000000000000000000
    00000000000000000000000002222222000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000

1 = Floor 2 = Wall

Xposition = 25; Yposition = 25;

width = 5; height = 5;

It worked fine right ? So now lets try this with some other values :

        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000000001111111111110000000000000
        00000000000000000000222222222222111110000000000000
        00000000000000000000200001111112111110000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000200000000002000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000

1 = Floor 2 = Wall

Xposition = 20; Yposition = 25;

width = 10; height = 5;

As you can see, the align ist right anymore. The wall is on a completly other position than the room itself.... The strange part is, that this only happens when the values (xpos,ypos,width and height ) Are different from each other.

---UPDATE---

When i invert the x and y axis i get this :

        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000211111100002000000000000000000
        00000000000000000000222222222222000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000111111100000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000
        00000000000000000000000000000000000000000000000000

The coords and sizes are the same as the example above.

Wheres the problem in my code ? Normally the Wall should be around the room...

Upvotes: 0

Views: 127

Answers (2)

genaray
genaray

Reputation: 1180

I finally got the solution, my Wall Algorythm was broken, i wrote a new one :

  // Vertical Walls 
  for(int y = (int) roomCornerDownLeft.y; y <= roomCornerDownLeft.y + roomHeigth; y++){

      dungeon[y][ (int) roomCornerDownLeft.x ] = 2;

      dungeon[y][ (int) roomCornerDownLeft.x + roomWidth ] = 2;

  } 

  // Horizontal Walls
  for(int x = (int) roomCornerDownLeft.x; x <= roomCornerDownLeft.x + roomWidth; x++){

      dungeon[ (int) roomCornerDownLeft.y ][ x ] = 2;

      dungeon[ (int) roomCornerDownLeft.y + roomHeigth][ x ] = 2;

  } 

Upvotes: 1

J.Baoby
J.Baoby

Reputation: 2231

In the inner for-loop of your setRoomFloor method, you're inverting the position on the x-axis and the position on the y-axis.

dungeon[yPos][xPos] = 1;

should be

dungeon[xPos][yPos] = 1;

Upvotes: 2

Related Questions