Skip to content
MarkL edited this page Feb 25, 2014 · 3 revisions

Introduction

There are several different co-ordinate systems which are used throughout CorsixTH. When the terms X and Y appear, it also needs to be known which co-ordinate system they are in. This page describes the co-ordinate systems which they might be in.

World co-ordinates

Generally, all world co-ordinates refer to a position in the game world. The world is always viewed in an isometric perspective, and is made up of tiles like the following:

cpp_tile_example

It is worth noting that when viewed individually (like above), a tile is 64 pixels wide, and 32 pixels tall. World co-ordinates are also known as tile co-ordinates, though tile co-ordinates are typically integers (whereas world co-ordinates do not have to be integers). When directions are mentioned with respect to world co-ordinates, they should be interpreted as in the following image:

directions

C++ world co-ordinates

In this co-ordinate system, the origin is (0, 0), the X unit vector (1, 0) represents movement one tile to the east, and the Y unit vector (0, 1) represents movement one tile to the south. To give an example of this, the following image has the C++ world co-ordinates at the intersections of the red lines annotated on it:

cpp_world_example

C++ tile co-ordinates

C++ code is typically only concerned with tiles, and so tile co-ordinates are usually used (which, as previously mentioned, are integer world co-ordinates). The distinction is subtle but important; the world co-ordinate (3, 4) refers to a tile and a position on that tile - the tile co-ordinate (3, 4) refers to an entire tile. The following image has the C++ tile co-ordinates for each tile annotated on in the centre of each tile:

cpp_tile_example

Lua world co-ordinates

The most common type of world co-ordinates, at least in Lua code, are Lua world co-ordinates. They are similar to C++ world co-ordinates, except that the Lua world origin is (1, 1) rather than (0, 0). The reason for this is historical, as in Lua, arrays are 1-based rather than 0-based, and the world was originally represented as 2D Lua array. To give an example of this, the following image has the Lua world co-ordinates at the intersections of the red lines annotated on it:

lua_world_example

Lua tile co-ordinates

Lua tile co-ordinates are to Lua world co-ordinates as C++ tile co-ordinates are to C++ world co-ordinates. The following image has the Lua tile co-ordinates for each tile annotated on in the centre of each tile:

lua_tile_example

Screen co-ordinates

Generally, all screen co-ordinates are integers which identify a particular pixel on the screen.

Absolute screen co-ordinates

Screen co-ordinates which are relative to the world origin are called absolute screen co-ordinates because they do not change as the screen is scrolled. To give an example of this, the following image has the absolute screen co-ordinates at the intersections of the red lines annotated on it:

abs_screen_example

Relative screen co-ordinates

Relative screen co-ordinates, also called canvas co-ordinates, give a position relative to the top-left hand corner of the (client area of the) window. To give an example of this, the following image has the relative screen co-ordinates at the intersections of the red lines annotated on it:

rel_screen_example

Converting between co-ordinate systems

Screen to/from World

By comparing the images for C++ world co-ordinates and absolute screen co-ordinates, it should be clear that the origin (0, 0) is the same in both systems, that the vector (1, 0) in world co-ordinates is equivalent to the vector (32, 16) in screen co-ordinates, and that the vector (0, 1) in world co-ordinates is equivalent to the vector (-32, 16) in screen co-ordinates. Thus we can express a transformation from C++ world co-ordinates (Xw, Yw) to absolute screen co-ordinates (Xs, Ys) by the following matrix:

\begin{pmatrix} x_s \\ y_s \end{pmatrix} = \begin{pmatrix} 32 & -32 \\ 16 & 16 \end{pmatrix} \begin{pmatrix} x_w \\ y_w \end{pmatrix}

By using some elementary matrix operations, the inverse transformation (from absolute screen co-ordinates to C++ world co-ordinates) can be found:

\begin{pmatrix} x_w \\ y_w \end{pmatrix} = \begin{pmatrix} \frac{1}{64} & \frac{1}{32} \\ \frac{-1}{64} & \frac{1}{32} \end{pmatrix} \begin{pmatrix} x_s \\ y_s \end{pmatrix}

World to Tile

Converting world co-ordinates to tile co-ordinates is a simple - just discard the fractional part of the world co-ordinates. Mathematically, this is the floor operation.

Clone this wiki locally