Convert object x/y from infinite map to fixed map

(David Frank) #1

I am convert object position from a infinite map to a fixed map outside of the editor. What’s the easiest way to calculate the new position?

Say I have an infinite map like this:

And I have parse and serialized the map into a fixed map like this:

I need to shift these object positions correctly, but because the origin object x/y use is implied and not explicitly stored, I am not sure how I can calculate that.

To add, my tile layer boundaries are calculated using min/max x/y values, so the generated fixed map is always as small as possible, it is different from Tiled editor’s conversion.

(David Frank) #2

Current status: using the same min/max value approach I can put objects in the map, but without figuring out the relative position between object layer and tile layer in the original map, I still had a hard time putting the objects in the correct position relative to tile layer.

(David Frank) #3

OK I think I got it now: I just need to use Tile layer’s x/y min value to offset objects layer.

The reason this works for me:

  • When I parse the tile layer and object layer, internally I already put them in the same coordinate system with the same origin.

  • Hence the shift in coordinate is the same for tile layer and object layer.

  • And I could have make the fixed map width and height covering objects as well. I will need another loop to shift tile layer as they were processed independently from object layer.

For what’s worth, if anyone else is doing this conversion, but want to preserve the chunks’ coordinate (basically follow Tiled’s conversion), here some interesting pointers:

  • For an infinite map, currentl the origin used by automated tile layer chunking and object layer, is the first tile being painted.

  • So if you go through each tile layer chunks and find the one with x/y at {0,0} (aka the chunk containing the origin), its layer’s startx/starty value will be the shift in coordinate when you convert the infinite map into a fixed map.

  • For example, in my test map, the red point at the bottom is the origin, in Tiled editor it will be shown as {0,0} when in infinite mode, and for my map, the layer containing this origin has startx/starty of {-16,-48}, and if I convert it to a fixed map using Tiled, then said origin will be at {16,48} when the conversion is done. Hence it represents the shift in tile coordinate.

That’s it, hope it helps.

(Thorbjørn Lindeijer) #4

Right, I plan to fix up Tiled’s editor conversion so that it will also make the map as small as possible.

The origin is simply 0,0. If you place your first tile on 100,100, that will not affect the origin in any way. So, I’m not sure what you’re saying here.

There can be any number of layers with a chunk at 0,0, and their startx / starty value, in combination with width / height, only specifies the bounding box of that layer. If you want to shift an infinite map into a fixed map and “normalize” all content to not go into negative space, you need to shift it by the lowest values of startx / starty of all layers. This has nothing to do with a layer containing a chunk at the origin or not.

(David Frank) #5

Ah yes, this makes sense.

Previously I thought the origin was determined at the time of placing first tile, but I later realized it wasn’t the case, my conclusion above was wrong.