Randomly Generated Rooms Colliding

So, I’ve written this messy script to generate a “dungeon” of sorts based on some rooms I created and stored in a folder in replicated storage. Essentially, we have a start room with an exit. The exit is the start room’s primary part. For all other rooms, the primary part is the entrance of the room. We connect the randomly selected room’s primary part CFrame to the CFrame of the start room. Then we connect the next randomly selected room’s primary part to the previous room’s exit. If there are multiple exits, then branches are created.

This is where I’m running into trouble. The creation of branches means rooms can circle back around on each other and hallways will run into each other like so:

Two long hallways colliding

Basically, I need a way to determine if the next room will collide with any other room. If it does, I need to go back a couple steps in the generation process and create either a dead end or a different room that won’t collide. Any ideas?

I would recommend some sort of table that stores the rooms positions, so that way you can check if the next generated room overlaps on another’s position. If there are no spots it can go, the branch would end.

1 Like