Skip to content

Implementation: Triangular tiles

mhwombat edited this page Sep 6, 2012 · 1 revision

Triangular grids aren't used that often in games, probably because there are only three directions to move in. When I searched the web for indexing schemes and equations for working with triangular grids, I didn't find an approach I liked, so I rolled my own. Perhaps there are better approaches than this one, but I wanted to document this approach in case someone else is doing a similar project.

For triangular tiles, it is convenient to define a third component z, where

z = -x - y (when y is even)

z = -x - y + 1 (when y is odd)

Then the minimum number of moves to go from tile (x1,y1) to tile (x2,y2) is given by

maximum (|x2-x1|, |y2-y1|, |z2-z1|)

And the tiles adjacent to a tile at (x,y) are

[(x-1,y+1), (x+1,y+1), (x+1,y-1)] (when y is even)

[(x-1,y-1), (x-1,y+1), (x+1,y-1)] (when y is odd)

The indexing scheme is illustrated below. If the x-coordinate is even, the triangle points up; if it's odd, the triangle points down. A line of tiles on a diagonal like / will have the same value for x.

Triangular grid with triangular tiles

Parallelogram-shaped grid with triangular tiles

Clone this wiki locally