Are there any conventions and/or best known methods around coordinate systems in game maps? Is the origin usually positioned in the center of the map? Or does it live in a corner and the map is built in a single octant of the 3D space?
-
\$\begingroup\$ Hm, you tagged tilemap and the top answer is talking about tilemap concerns, but you also mention 3D and octants which is probably why the second answer is talking about float precision. 3D geometry isn't usually stored in a grid so origins are more arbitrary, and i've never heard of float precision problems in a 2D grid map. Perhaps some clarification or unification is in order… \$\endgroup\$– WeaverCommented Oct 6, 2016 at 23:18
-
\$\begingroup\$ Octant was edited in. I mention 3D because most of the frameworks i have seen have some sort of Z axis. The game map may be flat and in 2D but the camera is offset along a third axis. \$\endgroup\$– TaylorECommented Oct 10, 2016 at 21:34
3 Answers
Most games place the map origin in a corner. The main reason for this is that tilemaps are often internally stored in two-dimensional arrays, and most programming languages don't allow negative array indexes. There are a lot of discussions about which corner should be the origin, but I don't consider any of the arguments particularly strong. In the end it's just personal preference. I would recommend to just use the same corner your graphics API is using to minimize headache.
Exceptions are often games which allow maps which grow infinitely in all directions. That doesn't work properly without negative indexes, so a simple 2d array is out of the question. You will need to store your map data in a more complex data-structure. In this situation it usually makes most sense to place the origin at the start position.
In theory the location of the origin doesn't matter. All math operations you do when the map is rendered or when objects move on the map work fine regardless of where the origin actually.
However in practice there is one reason why you might want to make the center of the map be the origin - and that is limited precision of floating point values.
With floating point numbers the farther you get from 0, the less precision there is in the smaller digits.
While working on an open world game that had a very large map we actually encountered problems where physics and other things would start to break down as you got farther away from the origin.
The fix was to do some trickery where the origin was always pretty near to the player, by intelligently teleporting things (the player, the map tiles, the camera etc) around to not get too far away from the origin.
It doesn't really matter. What some people change in their coordinate system is often the y-axis. Instead of having (0,0) in the top left, they prefer having it in the bottom left for easier physics.
-
\$\begingroup\$ How are physics easier if the origin of the coordinate system is in the bottom left? \$\endgroup\$– user35344Commented Oct 6, 2016 at 13:42
-
4\$\begingroup\$ I guess the last sentence is missing a word: debugging. It's more intuitive if the ground is zero and and everything goes up from there, than if the 'ceiling' is zero and everything is measured w.r.t. that. \$\endgroup\$– Vaillancourt ♦Commented Oct 6, 2016 at 13:52