Skip to main content
3 of 4
replaced http://gamedev.stackexchange.com/ with https://gamedev.stackexchange.com/

Screen-space mouse coordinates to tile coordinates

The general dimetric projection matrices in my answer to the question linked above will get you the transformation from screen coordinates to tile coordinates. In particular, this matrix can be used for transformation of the screen coordinates to tile coordinates:

The values to input are:

  • w: half of the width of a single tile on screen
  • h: half of the height of a single tile on screen
  • fx: X-Position of the upper corner of the (0,0) tile in screen coordinates
  • fy: Y-Position of the upper corner of the (0,0) tile in screen coordinates

The mouse coordinates have to be extended by a constant value of 1 to be a 3-dimensional vector (xmouse, ymouse, 1) and multiplied by this matrix to arrive at the tile coordinates.

Tile coordinates to tile indices

Unfortunately if you apply them straight away to your image, you'll find that the actual tile indices aren't quite in line with their coordinates.

![enter image description here][1]

The somewhat thicker green lines in the picture are at coordinates x=0 and y=0; the others are spaced one unit of length apart. Remember that the y coordinate goes down as usual with computer graphics, so the tile indexed by (1,0) is in the tile with coordinates x=[1, 2) and y=[-1, 0).

Given the "normal" integer tile coordinates x and y (in blue on the picture above), you can calculate your tile indices xind and yind with the following formulas(1):

xind = floor(x - y + 1)/2)

yind = x + y


(1) In this context, the floor() function rounds down towards -∞. [1]: https://i.sstatic.net/1JHgT.png

Martin Sojka
  • 5.7k
  • 31
  • 30