Tiled objects not lining up with where Tiled map/tiles are rendered

First, forgive me if my question/explanation is poor - this is my first post, and I will do everything I can to clarify my issue per requests.


I’m currently having an issue when importing Tiled maps with Object layers into a project. Everything is pretty much working great; I can import the Tiled map into the project and render the map with the use of MonoGame.Extended.

However, my issue starts when I try to use an Object from an Object Layer in the Tiled map. I can access the object, and I can access its properties (position, etc) just fine. In fact, when accessing the object’s properties, they output exactly as they should. Below are images to clarify:

This is the image of the object within the Tiled map editor

And below is the code where I import the map, collect the object’s information, and assign that information (position, in this case) to another object (the player)

gameMaps.Add(MapBuilder.LoadTiledMap("TestMap"));

gameMapRenderer = new TiledMapRenderer(graphicsDevice);

var viewportAdapter = new BoxingViewportAdapter(GameInstance.Instance.Window, graphicsDevice, 1440 / 4, 810 / 4);
mainCamera = new Camera2D(viewportAdapter);

var player = new Player();
var playerStartPos = new Vector2();

foreach(var obj in gameMaps[0].ObjectLayers[0].Objects)
{
  if (obj.Name.Equals("PlayerStart"))
  {
    playerStartPos = obj.Position;
    break;
  }
}

player.position = playerStartPos;

entities.Add(player);

mainCamera.LookAt(player.position);

But below is the image that demonstrates the issue:

Issue as an image

EDIT - The sprite/character is the player object that is assigned the position, in case that wasn’t clear

To explain: I am able to get the object’s position and assign it to my ‘Player’ object, and my player is then at the correct position per what the object’s position is in the Tiled map editor. However, the Tiled map itself is actually rendering at an incorrect position- even though I never make any adjustments to the position of the map as a whole.

Some further details that might help:

In the Tiled map editor, the position x0,y0 is the utmost top tile of the map. However, in MonoGame, if I go the same position- the Tiled map is actually rendered/placed some dimensions below. This means that the positioning of the map is incorrect with what it should be.

This kind of problem is pretty difficult to diagnose without being able to debug the code.

At a guess I’d say it’s some kind of mismatch between the position the player or map is in world space and the position the player or map is drawn in screen space. This usually comes about because of scaling from the viewport adapter or camera. Whenever any scaling is applied, you need to apply the same scaling (or in reverse) on everything equally.

To see if this is indeed the issue, the first step I recommend is switching to the the DefaultViewportAdapter. This will effectively remove all the scaling and render things in the same space as the world.

It’s also pretty handy if you render some debug text on the screen. Try displaying the mouse position, map position and player position in text. As you move things around, you’ll see the numbers changing and it might become more obvious what’s going on.

Thank you for some extremely helpful debugging information! I had a hunch it may be related to something in scaling, and I will begin to go through the process with the suggestions you’ve provided.

I saw you were in the MonoGame.Extended discord- I posted my issue there, so if you come across any further suggestions and its more convenient for you then feel free to contact me there.

  • – EDIT –

So I didn’t have any luck after doing some debugging. I had reset all of the scaling, and used the default view adapter- but nothing. I’ve tried debugging text to see some positions, but it was all the same.

I’ve gone ahead and uploaded the project to my github in case anyone wants to take a look and see what they can find. The link itself takes you to the exact script where the issue is occurring; the rest of the project is pretty straight forward.

Hey mate,

First of all, sorry for not having the time to look into this sooner. I only have a certain amount of spare time each day between work, wife and kids that I can dedicate to working on Extended. As you might imagine I get lots of people asking me lots of things all the time.

So, I downloaded your project and spent some time debugging it. I’ve discovered a few things that kind of explains what’s going on to some degree.

First, I noticed that the coordinate system in the Tiled Map editor is not the same as the way things are rendered in MonoGame. You can see this by copying the spawn point to the top corners of the map.

In other words, the top line runs diagonally down and to the right instead of horizontally across like it things draw in MonoGame. To fix this, you’ll need to transform Tiled object positions from this weird diagonal coordinate system back into normal X, Y screen coordinates.

There’s really no way to fix this in the renderer, since it’s not actually a rendering issue, but we might be able to add a helper method to the TiledMapObject to help convert these isometric coordinates into screen coordinates or something. Let me know if you figure out the calculation for this and I’ll implement it into the next version of Extended.

The second issue I ran into had me stumped for quite some time. When I set the player position to 0, 0 which should have placed him right on the top corner of the top tile.

He actually appeared exactly 97 pixels above it.Then eventually I opened up the Tileset01.png image in Paint.NET and noticed that the texture has exactly 97 pixels of padding above each tile.

So that explains that. To be honest, this probably is a genuine bug in the renderer but nobody has ever reported it because most tilesets don’t have this extra padding above the tiles.

There’s probably no great workarounds to this problem but here are some thoughts:

  1. You could remove the padding. This might be a bit of a pain because it could destroy your map. Also, it looks like some of your tiles need to be tall, so to do it properly you might need to split the tileset into two.

  2. A quick hack, might be to just offset your objects down by 97 pixels. Not the greatest solution but it would be the quickest way to keep going.

  3. You could of course submit a pull request to fix the renderer. By the looks of the bug there’s probably somewhere in the renderer that’s using the tileset tile height instead of the maps tile height. They are often the same value, which is probably why nobody has noticed it before.

I hope that helps.

PS. Your game is looking great so far. Don’t let these little hiccups get to you :slight_smile: It won’t be long before you have something playable and it will all be worth it.

Hello Dylan… right? The email said your name was Dylan, so I hope thats okay to use instead of the longer username lol.

I appreciate the response, as well as the detail of it. For starters, all I can really say is that I’m glad the conclusion you’ve had is more or less the same as mine. However, what did slip my attention was the padding in my tileset. I noticed the 97 pixel gap within monogame, but never made the connection that it was from my tileset.

As you noted, this gap was added to the tileset for the tiles with walls.

I’m honestly just happy someone took a look at the project and bounced back more ideas. I’ve been sitting here all week pulling my hair out, and I apologize for my previous comment seeming inappropriate. I’ve been building with the source code of Extended and debugging that to search for bugs I may be able to fix and submit to the repo.

Now that I’ve had your help, I may do a bit better at targetting that bug- especially with the outlining of the issue with the tileset padding.

I’ll continue to work on this, and if I can come up with a solution- I will make sure to provide it to you so that anyone else who might have this issue can resolve it.

Thanks for the cheers, mate, and good work with the new sprite tool you’ve released!

1 Like

How dare you use my real name on the internet!!

Just kidding :slight_smile:

I’m glad to help. To be honest, it’s refreshing to see how other people are using Extended in their projects. Even though I’ve seen lots of code snippets helping people I rarely get to actually compile and run someone else’s game.