Questions about x/y coordinates of MapPosition, ChunkPosition, TilePosition

Place to get help with not working mods / modding interface.
Post Reply
fredthedeadhead
Inserter
Inserter
Posts: 28
Joined: Mon Oct 18, 2021 6:13 pm
Contact:

Questions about x/y coordinates of MapPosition, ChunkPosition, TilePosition

Post by fredthedeadhead »

I'm exporting tile-data based on events into a file, and I'm trying to combine them into chunks so that I can make PNGs of them.

I'm getting confused because I'm not clear on the coordinate system that Factorio is using, and whether it's the same for each of these different positions. I have a lot of questions, some are really basic, but I'd really like the answers all in one place.
  1. Are the x/y of Position, MapPosition, ChunkPosition, and TilePosition all zero-indexed? Are they all (x:0, y:0) at the center of the map?
  2. Do all positions use Cartesian or screen coordinates? Does positive-y go up, or down? Does positive-x increase to the left, or to the right?
  3. Is the origin of TilePosition the center of the map, or a corner of a chunk If so, which corner? Should it go from 0 to 31?
  4. Is a ChunkPosition the largest x and y TilePosition in the chunk? Or the middle TilePosition?
  5. The ChunkPosition documentation says "A Position can be translated to a ChunkPosition by dividing the x/y values by 32.". Assuming ChunkPosition is zero-indexed, and the values are rounded down, what happens near the origin? Because that the Positions (x:31,y:31) and (x:-31,y:-31) will have ChunkPosition (x:0,y:0) - does this mean chunk (x:0, y:0) is 64 tiles in length/height?
  6. How can I get the ChunkPosition of any TilePosition? As in, given a list of any number of TilePositions (but no other information), how can I aggregate them so they are grouped per chunk?
Thank you very much for any help - I feel a bit silly for asking these, I'm sure I could figure this out, but I get very lost when using looking at all these different options, and I couldn't find any previous discussion on these.

(I did find a post explaining viewtopic.php?p=447586#p44758 how to convert from a ChunkPosition to a BoundingBox, which is part of the solution)

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Questions about x/y coordinates of MapPosition, ChunkPosition, TilePosition

Post by boskid »

X=right. Y=down.

MapPosition: 0,0 is center of world. Resolution is 1/256th.
TilePosition: same as map position but resolution is 1 (you take MapPosition and round it towards negative infinity ["down"]: coordinate of 1.5 becomes 1; coordinate of -1.5 becomes -2)
ChunkPosition: x,y covers tiles (32x+0, 32y+0) up to (32x+31, 32y+31) which means it covers map positions from (32x+0, 32y+0) up to (32x+31.9961, 32y+31.9961)

fredthedeadhead
Inserter
Inserter
Posts: 28
Joined: Mon Oct 18, 2021 6:13 pm
Contact:

Re: Questions about x/y coordinates of MapPosition, ChunkPosition, TilePosition

Post by fredthedeadhead »

Thanks boskid! The 'round towards negative infinity' tip was really useful. I have something that's working now. Here's a preview

https://i.imgur.com/UhkPlOA.mp4

That's me clicking around the web-map I generated from my Factorio test map. I drew letters in concrete to make sure the map tiles were generated the correct way up! I just have to work on improving the speed now, because it takes a long time to generate the tiles (that's why most of the tiles are unfinished).

Anyway, to answer my questions
  1. Yes, Position, MapPosition, ChunkPosition, and TilePosition are all zero-indexed Yes, they are all (x:0, y:0) at the center of the map.
  2. For all positions, positive-y goes south. Positive x goes east. I believe this is called 'screen coordinates'.
  3. Yes, the the origin any TilePosition is the center of the map, NOT the corner of a chunk. It can range from -2million to +2million (max/min int value).
  4. The ChunkPosition is the top-left corner of the chunk.
  5. "A Position can be translated to a ChunkPosition by dividing the x/y values by 32." - This is not quite correct. First divide by 32, then take the floor of the result. This will round towards minus infinity (not towards zero, or the closest integer).

    There is no overlap near the origin, or any other position. TilePosition (x:31,y:31) is in chunk (x:0,y:0) (the floor of 31/32 is 0). TilePosition (x:-31,y-31) is in chunk (x:-1,y:-1) (the floor of -31/32 is -1).
  6. To get the ChunkPosition from a TilePosition, divide by 32 and floor the result (round towards negative infinity

Post Reply

Return to “Modding help”