Best way to create a map file?

Apr 27, 2009 at 8:20 AM
Hi there, here I am with more questions.

I'm working on a 2D platformer (well it's a little more complicated than that, but anyway...) and I'm thinking on creating a map editor. But before start working on it, I would like to ask to the more experienced users (you!) for some advices. My main questions are: Which file format should I use for map creation? For example a "mask" image for creating all the walls and floors (then create the object from this file), and another one with the textures? Or an XML file containing all the objects and their position in the world?

Thanks for your help.
Apr 27, 2009 at 8:58 AM
i don't think there is a best approach, it really depends on what you need. we're modeling our scene and collisions in 3ds max and exporting it via script.
Apr 27, 2009 at 10:28 AM
Thanks yobiv for your answer. I think I'll try with an xml file containing the objects, their position in the world and their properties as I want to keep it simple and have an accesible map editor. I guess it shouldn't have problems with xbox compatibility.
Apr 27, 2009 at 4:24 PM
 You could look at something like this:
and get ideas from it. (Might even want to use it, if it serves your purpose). It creates the tiles, numbers them (e.g 0 = grass, 1 = tree etc..). You can then export it and use it in an XML file for further customisation. Are you using a specific engine for the game, or have you created one from scratch?

Apr 27, 2009 at 4:46 PM
Thanks Euler, I think that this could help me a lot :)
Yes, I'm creating the engine from scratch (using Farseer)
Jul 17, 2009 at 1:17 PM

Hi there, if anyone I found this great application for creating maps: GLEED2D

See how easy is to create a map here:

Hope this helps to someone else :)

Jul 17, 2009 at 4:58 PM

@pnikosis: Is there anyway you can post your demo code? This looks like a great way to make levels.

Jul 17, 2009 at 5:53 PM
Edited Jul 17, 2009 at 6:06 PM

@Matt, no problem! You are one of my heroes here in the dev forum, so I couldn't say no :P The zip file that comes with the program has a Level.cs class file for reading the contents of the xml file. This class is what I use here. By the way, I'm not very good programming, so probably there are better ways to do some of this stuff (any suggestions is more than welcome):


class TestScreen : GameScreen
Level level;
Camera2D camera; // <- RogueCommander's awesome Camera Class
Hero hero;

public override void Initialize()
level = Level.FromFile("level1.xml", ScreenManager.ContentManager); // <- Make sure that the xml file is stored in the same foldr than the executable
Item item = level.getItemByName("Hero"); // <- Here I look where should the hero appear in the map

hero = new Hero(item.Position, input, PhysicsSimulator); <- My hero constructor :)

// Here I look for items in the "Main" layer (see video)
// if the item contains the custom property (you can add custom properties to any item)
// called "Type" with the value "CollisionRectangle", then
// create a "CollisionRectangle" (is just a rectangle body an its Geom, without any texture)
foreach (Item it in level.getLayerByName("Main").Items)
if (it.CustomProperties.ContainsKey("Type"))
RectangleItem ri = (RectangleItem)it;
if ((string)ri.CustomProperties["Type"].value == "CollisionRectangle")
CollisionRectangle cr = new CollisionRectangle((int)ri.Width,


public override void Draw(GameTime gameTime)

// As I want a Parallax effect, I will draw
// each layer separately, with their own
// scroll speed taken from the layer's property
foreach (Layer layer in level.Layers)
Vector2 oldcameraposition = camera.Position;
camera.Position *= layer.ScrollSpeed;



// If I'm in the "Main" Layer, draw the hero
if(layer.Name == "Main")


camera.Position = oldcameraposition;



Jul 17, 2009 at 5:58 PM
Edited Jul 17, 2009 at 6:02 PM

Oh, almost forgot. The Level.cs file must be modified. By default its "Load" method looks like this:


public override void load(ContentManager cm)
            throw new NotImplementedException();

            //TODO: provide your own implementation of how a TextureItem loads its assets
            //for example:
            //this.texture = Texture2D.FromFile(<GraphicsDevice>, texture_filename);
            //or by using the Content Pipeline:
            //this.texture = cm.Load<Texture2D>(asset_name);

            if (texture != null) Origin = new Vector2(texture.Width / 2, texture.Height / 2);


You must change it for your own implementation, I mean how you want to load each item. I used the simpliest method that is to load directly the texture (uncommenting the last line):


public override void load(ContentManager cm)
            //throw new NotImplementedException();

            //TODO: provide your own implementation of how a TextureItem loads its assets
            //for example:
            //this.texture = Texture2D.FromFile(<GraphicsDevice>, texture_filename);
            //or by using the Content Pipeline:
            this.texture = cm.Load<Texture2D>(asset_name);

            if (texture != null) Origin = new Vector2(texture.Width / 2, texture.Height / 2);

But instead a Texture2D for each item, you can create a Body, or a more complex element, or whatever :)


Edit: One last thing! The author just released a new version with a Snap to Grid feature, but has a few small bugs yet, he told me that he will release a new version soon. One of the nicest things it has, is that it comes with a Path Generator, which I plan to use for creating more complex geometries instead the rectangles :)


Oct 18, 2009 at 4:45 PM
Edited Oct 18, 2009 at 4:59 PM

Hey were you able to get it to work with paths yet? I've gotten my code working with the Rectangle primitives but I just can't get my path code to work. Any help would be amazing!

Oct 18, 2009 at 6:40 PM
sonic4305 wrote:

Hey were you able to get it to work with paths yet? I've gotten my code working with the Rectangle primitives but I just can't get my path code to work. Any help would be amazing!

Hi Sonic, with paths is pretty simple. Here's an example on how create a body with a path (it is an Item taken from the xml file):

if(it is PathItem)
PathItem pi = (PathItem)it;
Vertices _vertices = new Vertices(pi.LocalPoints);
Vector2 _position = pi.Position + _vertices.GetCentroid(); // This way we are sure to have the polygon at the same world place
Body _body = BodyFactory.Instance.CreatePolygonBody(physicsSimulator,
_body.Position = _position
 And tadaaa... you have your polygon :)

Oct 19, 2009 at 8:27 PM

You didn't use a geom or anything?

Oct 19, 2009 at 9:44 PM

Ooops, sorry, yes I did.

Just create the geom like the body (using the vertices) and attaching it to the body. Something like:

_geom = GeomFactory.Instance.CreatePolygonGeom(physicsSimulator,


Oct 21, 2009 at 7:13 PM
Edited Oct 21, 2009 at 7:40 PM

Ok for some reason, the GetCentroid() is not working. Here is a picture of it:

See how the + is not centered. There are only three points that I send it as well. You can see them in the picture.

This is the code I'm using:


                    else if ((string)item.CustomProperties["Collision"].value == "Path")
                        PathItem pItem = (PathItem)item;
                        Vertices vertices = new Vertices(pItem.LocalPoints);
                        Vector2 position = pItem.Position + vertices.GetCentroid();
                        body = BodyFactory.Instance.CreatePolygonBody(Farseer.Physics, vertices, 3000);
                        body.Position = position;
                        body.IsStatic = true;
                        body.IgnoreGravity = true;
                        geom = GeomFactory.Instance.CreatePolygonGeom(Farseer.Physics, body, vertices, 0);
                        geom.CollisionCategories = CollisionCategory.Cat10;
                        geom.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat10;


Any help would be amazing!


Oct 22, 2009 at 7:20 AM

Uhm, you are right, well the GetCentroid() function works good. Is my implementation which is not working well, I'll check this out.

Oct 22, 2009 at 11:58 PM

I had old code that found the center of the body but I felt it would be best to go with code already implemented so I'm going to have to write it out again :D

I'll post it once I get it back.

Oct 23, 2009 at 8:43 AM

sonic, the + is not centered because your geometries are triangles, right? The case is that in triangles (at least not the equilaterals) and other "irregular" polygons, the centroid is not centered, remember that.

Oct 23, 2009 at 1:42 PM

Oh I know that. I though that the getCentroid() just took the lowest and max Vector points from the path and found the center of those. My old code worked and I'll put it on here for anyone else who has this problem.


Oct 25, 2009 at 2:50 PM
Edited Oct 25, 2009 at 3:17 PM

So I finished my code which does find the center but I've come to a new conclusion. CreatePolygonBody() is what offsets the center of the body. I tried switching to instead to CreateRectangleBody() and then finding the width and height of my vertices and it works but I just don't like doing it this way. Any help to how to get CreatePolygonBody() to work would be amazing :)


Here is a picture off what it does as well. I honestly don't get it.

Oct 26, 2009 at 8:01 PM

The CreatePolygonBody() sets the position of the body to be the centroid of the vertices. Once you call CreatePolygonGeom() it will also translate (offset) the vertices using the centroid. When you draw your character, you need to draw it at the centroid instead of width/2, height/2.

It is also described in our manual.

Oct 26, 2009 at 8:23 PM

Would it be okay to use CreateRectangleBody() with CreateGeomBody()? These are statit objects that never move (terrain ^_^) and I would think it would be easier to do and possibly quicker. I have it working like that right now and I'm thinking about leaving it like that but if you don't think so genbox, I'll do it the way you said I should.

Oct 26, 2009 at 8:57 PM

I've not been following the whole thread - but if it works for you, I see no problem in using one over the other.

Oct 26, 2009 at 9:23 PM

Ok cool. Thanks for the help genbox and I love Farseer!


And for everyone else, I'm going to spend the day fixing the code so the angle thing works and then I'll post it in a new thread and give you guys the link :) (unless this isn't the best way to do it?)

Oct 27, 2009 at 3:18 PM

Here is the link to the angle Discussion I created. Talks of a new way and how to get a hold of me if wanting old code:

That should stop the combining of two separate discussions.