Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

2D Procedural Generation Questions

Discussion in 'Scripting' started by NovusAnimos, Jan 1, 2017.

  1. NovusAnimos

    NovusAnimos

    Joined:
    Jan 1, 2017
    Posts:
    9
    So I am trying to make a level the contains 5 rooms, of varying width and height, that are all accessible. My issue comes from trying to space the rooms, I have no idea how to even attempt it. My code stores the rooms position, size, minimum point and maximum point.

    DungeonCreator Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DungeonCreator : MonoBehaviour
    6. {
    7.     [Header("Dungeon Atributes")]
    8.     public int numberOfRooms = 5;
    9.     public int numberOfBosses = 1;
    10.     public Vector2 widthRange = new Vector2(6, 23);
    11.     public Vector2 heightRange = new Vector2(3, 12);
    12.  
    13.     public void SpaceRooms(List<Room> roomList)
    14.     {
    15.         //Help Please
    16.     }
    17.  
    18.     public List<Room> CreateRooms(int numberOfRooms)
    19.     {
    20.         List<Room> roomList = new List<Room>();
    21.  
    22.         for(int r = 0; r < numberOfRooms; r++)
    23.         {
    24.             Room newRoom = CreateRoom(PickRoomPos(), PickRoomSize(widthRange, heightRange));
    25.             roomList.Add(newRoom);
    26.         }
    27.  
    28.         return roomList;
    29.     }
    30.  
    31.     public Room CreateRoom(Vector2 roomPos, Vector2 roomSize)
    32.     {
    33.         Room newRoom = new Room();
    34.  
    35.         newRoom.newRoom(roomPos, roomSize);
    36.  
    37.         return newRoom;
    38.     }
    39.  
    40.     public Vector2 PickRoomPos()
    41.     {
    42.         Vector2 roomPos;
    43.  
    44.         roomPos.x = Mathf.RoundToInt(Random.Range(-10, 10));
    45.         roomPos.y = Mathf.RoundToInt(Random.Range(-10, 10));
    46.  
    47.         return roomPos;
    48.     }
    49.  
    50.     public Vector2 PickRoomSize(Vector2 mWidthRange, Vector2 lengthRange)
    51.     {
    52.         Vector2 roomSize;
    53.  
    54.         roomSize.x = Mathf.RoundToInt(Random.Range(mWidthRange.x, mWidthRange.y));
    55.         roomSize.y = Mathf.RoundToInt(Random.Range(lengthRange.x, lengthRange.y));
    56.  
    57.         if(roomSize.x == roomSize.y)
    58.         {
    59.             PickRoomSize(widthRange, heightRange);
    60.         } else if(roomSize.x == mWidthRange.x && roomSize.y == lengthRange.y)
    61.         {
    62.             PickRoomSize(widthRange, heightRange);
    63.         } else if(roomSize.x == mWidthRange.y && roomSize.y == lengthRange.x)
    64.         {
    65.             PickRoomSize(widthRange, heightRange);
    66.         }
    67.  
    68.         return roomSize;
    69.     }
    70. }
    71.  
    72. public class Room
    73. {
    74.     public List<Room> rooms = new List<Room>();
    75.  
    76.     public Vector2 roomPosition;
    77.     public Vector2 roomScale;
    78.  
    79.     public Vector2 minPoint; // lower left of room
    80.     public Vector2 maxPoint; // uper right of room
    81.  
    82.     public Room newRoom(Vector2 roomPos, Vector2 roomSize)
    83.     {
    84.         Room room = new Room();
    85.  
    86.         room.roomPosition = roomPos;
    87.         room.roomScale = roomSize;
    88.  
    89.         room.minPoint = new Vector2(roomPos.x - (.5f * roomSize.x), roomPos.y - (.5f * roomSize.y));
    90.         room.maxPoint = new Vector2(roomPos.x + (.5f * roomSize.x), roomPos.y + (.5f * roomSize.y));
    91.  
    92.         rooms.Add(room);
    93.  
    94.         return room;
    95.     }
    96. }
    Thank you so much for your time :D
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Placing rooms of a dungeon is basically:
    you try to place one
    it doesn't overlap -> great for you
    it overlaps -> you retry
     
  3. NovusAnimos

    NovusAnimos

    Joined:
    Jan 1, 2017
    Posts:
    9
    How can I tell if they overlap. That is my issue.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Just google AABB intersection algorithm
     
  5. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,610
    I'd do sort of a random number part to decide width and height, test for collisions (maybe raycasting against colliders, maybe check dimensions and compare) and if there is, then redo the random roll until it does not collide.

    The more rooms, the more times you must retry and compare against several areas of collision. I won't code this for you because it'll be hard haha, but basically either compare corners of stuff to see if they cross, or raycasting in fancy ways to see if there intersecting....

    And yeah AABB might just do this trick like said above.
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Another way would be to randomly throw down every single room, checking them for collisions, and slowly "shaking" them out of collision, like a 2d collision script would do it
     
  7. NovusAnimos

    NovusAnimos

    Joined:
    Jan 1, 2017
    Posts:
    9
    I believe I figured it out, if it works I will post the full script.(I am using a foreach loop and manually checking if it overlaps and if it does it moves to room to the edge of the current room)
    Thank you so much gorbit99 :)
     
unityunity