3

I have an issue when importing Tiled maps with Object layers into a MonoGame 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.

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

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.

Below is an image showing what that position is in MonoGame, and how it is not properly correlating with what that position is in Tiled.

MonoGame screenshot

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
  • You are probably working with 2 different coordinate systems. One for the current view (camera) and one for the world itself, right? I guess you're are simply missing an offset (camera <> world) or assigning the position to the wrong coodinate system. – Pavel Slesinger Oct 09 '19 at 09:45
  • @PavelSlesinger I figured it was some sort of issue like that. I am using a view matrix (camera) which causes the Tiled map to change its scale- its just that the TiledMap object doesnt have a native way to translate its object's positions for a view matrix. – Matthew Yates Oct 09 '19 at 10:04
  • UPDATE** So there wasn't any scaling going on that could have caused it, or a view matrix. I removed any, and left it as plain as it could be. It seems, by default, my tilemap is being rendered with an offset and I don't know what could cause this – Matthew Yates Oct 09 '19 at 13:55
  • @MatthewYates Are you aware that a sprite position or any other object position is given by its **top left corner**? If you want to draw your little guy sprite centered on the tiles you have to calculate an offset vector and add it to your little guy's position. – Javier Silva Ortíz Oct 17 '19 at 20:30

1 Answers1

0

After speaking a bit to the creator of Monogame.Extended, we narrowed down the issue. It was actually two things:

  1. In the Tiled map editor, the grid system for isometric maps is different from the grid system Monogame uses to render everything. Tiled's grid system for isometric maps is a normal grid but rotated to match the isometric view, whereas Monogames is just a normal grid. This causes the position (x,y) of Objects in Tiled to be imported into Monogame with a position value that doesnt match with where it should actually be in Monogame- which explains the position being wrong in the image above. Monogame.Extended's creator took note on the bug but said a fix isnt certain because it would require a formula for converting an isometric Tiled position to a Monogame position.

  2. Because my sprites had extra padding at the top, it was causing the overall map to render x many pixels lower than at what should be the proper 0 position. It had something to do with the map being generated in Monogame via the sprites dimensions, instead of the maps. Not a giant problem, honestly, since a workaround could be presumably made in a custom implementation of the TiledMapRenderer.

I can post the link to the Monogame.Extended discussion later.