Breaking News

Creating a labyrinth

Now that I can statically define elements using a tile map I was wondering if there is an easy way to integrate some dynamic aspects as well.

I had been struggling with one problem in particular. If a user has a small play area, in order to give him the feeling that he can wonder around endlessly, I’d have to manually design many rooms with many different wall placements to make them interesting enough. And if a user has a big play area I’d have to place a LOT of walls. That does not sound like something to look forward to. In addition, the transition zones need to connect smoothly somehow, meaning the exit of of room must be the entry of another.

This is when I went to research a bit how labyrinths are constructed. There are multiple approaches and very interesting algorithms. For anyone interested in the details and some great animations I really recommend the Wikipedia page. They differ in some key aspects, like do they tend to generate many short turns or do they favor long corridors. For a labyrinth inside a small play area, too-long corridors would not be very beneficial I figured as that would get boring quickly.

One other aspect I considered was if I would be limited to 2D or if labyrinths could also span multiple floors, connected by elevators for example. Since Maze VR already supports multiple floors this could be a really nice thing to do in the future as that expands the labyrinth potentially indefinitely. Prim’s algorithm should potentially support that. I also found a great Unity 3D labyrinth tutorial by Gaik Software which shows how that can be realized. I’ll shelf that for later investigation.

3D labyrinth supporting multiple floors by Gaik Software

Asset Research

To get started with something and get a feeling if labyrinths would work at all I decided not to code it myself if I can help it but rather reuse some existing library. I checked the Unity Asset Store for promising candidates and indeed one of the good rated assets, and coincidentally in my collection already (I must have picked it up in a sale 2 years ago), was QMaze.

QMaze

The asset is very appealing because it offers some great functions:

  • works with an X by Y grid, almost compatible to Maze VR (just Y is swapped)
  • ability to specify concrete entry and exit points which will create an opening in the labyrinth side
  • option to place obstacles where no maze should be generated
  • very fast
  • does not force me to generate the geometry with the asset as well (as I would generate it myself, I just need the coordinates)
  • good object representation, making it easy to use at runtime
  • and probably the most appealing factor for me: it goes beyond simple default labyrinth tiles but offers quite some more dynamic parts allowing for curved corners, angled corridors etc, as can be seen on the screenshot above

Integration

Since my levels are specified in Tiled I needed to find a good way to declare if a room should contain a labyrinth and what it’s attributes are. Since I wanted to control the transition zones I knew I needed a way to specify the exists, and potentially the obstacles as well. I struggled a bit with how to mark the area that the maze should be contained in. In the end, I went for an explicit tile type, as that is also the most visible in the editor.

Maze definition in Tiled

The screenshot above shows two transition zones on the left and right side (pink). The green part in the middle marks the maze. The green bars mark the exits. Setting the maze to auto fill will ensure it works nicely also in scaled environments. I had to add a new pivot mode for my world builder object placement as the QMaze grid uses pivots in the center on objects (which makes sense) and was then ready to go for a first try.

Putting together all the pieces we have so far, tile map definitions, scalability and QMaze, we finally get this (with the tilemap from above, scaled to 8 by 8):

How cool is that. Now THAT looks really promising. I could not visit it yet in VR since the Unity 2019.b5 somehow broke my Android export but I am REALLY looking forward to it 🙂 Next up is to find a way how to declare multiple labyrinths linked together with multiple exits and ways to create potentially huge labyrinths that can only be traversed if one follows certain hints. So many exciting things to do!

Leave a Reply

Your email address will not be published.