Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to make pathways/corridors in a randomly generated dungeon

Discussion in '2D' started by Pennywise881, Apr 6, 2019.

  1. Pennywise881

    Pennywise881

    Joined:
    Nov 5, 2015
    Posts:
    26
    Hi there,

    I have been working on a 2D project where I am trying to make a randomly generated dungeon. So far, I have been able to spawn the rooms at different positions and connect them using a minimum spanning tree algorithm. You can see what I have so far in the attached image. But now I am stuck at what would be the last part and that is to connect the rooms through pathways or corridors. I would like the corridors to be straight or form an 'L' shape when connecting to rooms but I am having difficulty understanding how I should go about it all. So, if anyone can help me out then that would be great :)
     

    Attached Files:

  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Well first I would find the MIN/ MAX of both rooms x and y and compare them. If they overlap (a minimum of the width of your corridor) then you don't have to make a path with a bend(and the following works regardless of direction).
    overlap.PNG
    So assuming they do "overlap" in this case now we choose a random position on the overlap that is a random distance between Overlap MIN and Overlap Max - corridor width.
    Pos1.PNG height.PNG
     
  3. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    Of course this doesn't take into account checks for existing rooms or hallways but that is a different question entirely. I'll leave you to it to figure out how to do the L shaped corridors when there is no overlap.
     
  4. Pennywise881

    Pennywise881

    Joined:
    Nov 5, 2015
    Posts:
    26
    Ok so I found the solution and it's actually rather simple and elegant. Thanks to this article I was able to find out that the first step is to calculate the midpoint between 2 rooms. If the midpoint falls within the bounds of the rooms, then stretch a path across the midpoint to the bounds. Otherwise, make an 'L' shaped corridor from the center of one room to the center of the other.

    Thanks to everyone who tried to help out. Take a look at the images below to see what different generations of the map looks like
     

    Attached Files:

  5. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    311
    I still recommend my solution to solve for hallway width. You only have a line for your hallways right now.
     
  6. Pennywise881

    Pennywise881

    Joined:
    Nov 5, 2015
    Posts:
    26
    The line is drawn for visualization only. I am just doing a Debug.DrawLine to show them. In the actual game, I will replace the lines with sprites that have some width.