Posts Tagged Tiled

Tiled map loader for XNA

Update: Stephen Belanger of Nerd Culture has made some great improvements to the following code, so I encourage you to check out the post about it on his blog.

Early in the development of my game, I used the free and open-source Tiled Map Editor to create levels. It was a big time-saver since it let me worry about more important things instead of investing effort into being able to place tiles down on a map. Later on I decided that the traditional approach to map construction wasn’t right for my project – but I was still glad I’d used Tiled.

Recently I realized that there aren’t very many easy ways for newbie XNA developers to get maps into their games, so I decided it was worth packaging up my Tiled map loader and sharing it with the world. So, I’ve created a simple example that shows how to load Tiled maps in your XNA game on Windows PCs and the XBox 360, and included the loader with it. It’s open-source and free for your use, no strings attached. I hope you find it helpful.

screenshot

Download source code and binaries

Note that it doesn’t have support for isometric tiles or embedded tilesets, because I had no use for either feature. Tiled’s file format is relatively simple, so if you need those features, it should be simple to add them.

And of course, this wouldn’t be possible without the generous contributions of the developers of Tiled, Adam Turk and Bjørn Lindeijer. If you’d like to try it out, you can download it from their website (note: requires Java).

Tags: , , , , , , , , ,

Maps and more sloped surfaces

As a quick way of getting something that resembles content into the game, I wrote a simple loader for for Tiled‘s XML map format. It was pretty straightforward, but I did discover that for whatever reason, XNA’s built in XML loader for the Content Pipeline doesn’t support XML files with a DTD attached. Also, for some reason, it can take up to two seconds to read the map in from XML – I think there’s some sort of bug in XmlReader related to really long lines (the tiled map I’m using has a 50000-character line of base64 text in it) that’s causing it to randomly perform slower. At least it works.

Regardless, I put a simple map together and made some basic tiles, and got it loading into the game. Then I had to figure out how to map collision geometry to the tiles.

The first approach I took was using Tiled’s metadata to attach ‘shape’ information to each tile. It worked, more or less, but I ran into weird issues with there being gaps between the edges of the tiles that the player could get stuck on or fall through, so for now, I abandoned that method. The algorithms I’m using to do motion and collision detection work best with large shapes to collide against, instead of individual tiles, since they operate on pairs of polygons or on single polygons. I think to properly collide against tiles directly, I’ll need a way to build large collision polygons from groups of tiles automatically.

One of the things I quickly discovered is that having lots of sloped surfaces brought out a few bugs – for example, small sloped surfaces with a very steep slope didn’t work right, with the player getting stuck on the left and right edges. The solution to this actually turned out to be pretty simple – instead of determining what the player is standing on, I switched to doing a ‘standing’ check at multiple locations on the player’s X axis – the left side, the right side, and the middle. I use all three of those checks to compute ‘standing’ Y positions, and take the minimum one. This seems to fix the algorithm for all of the test cases I have in this map – he moves up and down sloped surfaces just about right and can cross between surfaces without any glitches.

On an unrelated note, SpriteBatch is pretty damn nice. It’s fast, it’s simple, and it does exactly what you’d want it to do. A lot of people building rendering and game dev libraries tend to lean towards ‘heavy’ tools, like a first-class Sprite object with lots of properties and methods, but the XNA guys decided to let you do everything but the rendering, and I think that was definitely the right choice.

Future milestones:

  • Loading collision geometry from a file
  • Saving and loading the player’s game, along with the necessary integration for it to work right on the 360
  • Background content loading
  • Some basic AI creatures to fight

Tags: , , , , , ,