-
Notifications
You must be signed in to change notification settings - Fork 12
Implementation: Triangular tiles
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.