Rendering a Tiled TMX map as a 3D World in LibGDX

I’ve been meaning to post this for a while, but I never got around to it. I was working on a dungeon crawler POC a while back with some friends and we decided to go with LibGDX as our game engine. For those who don’t know, LibGDX has a super cool new 3D API and since we needed to render a 3D world for our game I have been playing with it a bit lately.

After discussing how we were going to go about creating levels in our game, I was faced with the task of creating a quick and easy way for everyone to create levels that involved no programming. I decided on using Tiled because it’s easy and I have been using it to create levels and worlds in 2d games for a while now, plus LibGDX has some handy built-in functionality to easily handle the TMX files that Tiled exports. So, I needed a way to render a Tiled map export as a 3D world. When I started working on it I figured that someone would have come up with a solution for this already, but surprisingly I was not able to find any existing solutions that did what I needed. So, I hunkered down and wrote up my own world renderer.

The demo is heavily inspired by and built on top of existing code from TheInvader360‘s Arena Roamer tutorial in which a text file is used to render a 3D and a 2D arena with collectable items. Unfortunately, this game has not really gone anywhere since building this demo, but I figured I might as well release the code for the world renderer.

I’m not going to go through a whole tutorial here because the code wasn’t originally written to be a tutorial. However, I will post the important parts of the functionality here and try to explain them a bit. These are just some snippets for explanation purposes, for access to the full source clone from GitHub.

First, the World constructor loads the tmx file and sets up the layers to be looped through.Then the constructor calls generateLevel whichloops through each tile in the map and checks for tile properties to tell whether it’s a wall, a floor, or the player starting position. Depending on what property the tile has generateLevel then creates a new object and adds it to the corresponding array, or it sets the player starting position.

Then inside WorldRenderer.java the generateMap() method creates the model instances based on our arrays inside World and adds the newly created ModelInstances to some local arrays. Once the ModelInstances have been created, renderPlayArea() is called and we first add the player, then render the walls and floors arrays respectively.

So there you have it, it's not too fancy and it's still just in prototype format. I haven't even built it to handle multiple levels yet. However, I wanted to make it available for people to use and improve upon since I wasn't using it for anything. An idea I had recently was to use JSON instead of TMX, but I haven't had any extra time to work on this. Perhaps someone can take this and manipulate it to use JSON.