# LibGDX hexagonal tilemap getting adjacent tiles

How do I get all adjacent tiles to a hexagonal tile, my tile map is using a staggered co ordinate system with an odd stagger index and I am using libgdx java.

if (x % 2 == 0) {
hexAdj[0] = new Vector2(x    , y + 1);
hexAdj[1] = new Vector2(x + 1, y + 1);
hexAdj[2] = new Vector2(x + 1, y    );
hexAdj[3] = new Vector2(x    , y - 1);
hexAdj[4] = new Vector2(x - 1, y    );
hexAdj[5] = new Vector2(x - 1, y + 1);
}else{
hexAdj[0] = new Vector2(x    , y + 1);
hexAdj[1] = new Vector2(x + 1, y    );
hexAdj[2] = new Vector2(x + 1, y - 1);
hexAdj[3] = new Vector2(x    , y - 1);
hexAdj[4] = new Vector2(x - 1, y - 1);
hexAdj[5] = new Vector2(x - 1, y    );
}


I think the simplest way would be to convert your coordinates to cube coordinates, then use a lookup table with the offsets to get the coordinates for the adjacent tiles, and then convert the coordinates back.


Vector3 coordsToCube(Vector2 coords) {
Vector3 cube = new Vector3();
cube.x = coords.x - (coords.y - ((int) coords.y & 1)) / 2
cube.z = coords.y;
cube.y = -cube.x - cube.z;
return cube;
}

Vector2 cubeToCoords(Vector3 cube) {
Vector2 coords = new Vector2();
coords.x = cube.x + (cube.z - ((int) cube.z & 1)) / 2;
coords.y = z;
return coords;
}

static Vector3[] offsets = new Vector3[] {
new Vector3(+1, -1,  0), new Vector3(+1,  0, -1), new Vector3( 0, +1, -1),
new Vector3(-1, +1,  0), new Vector3(-1,  0, +1), new Vector3( 0, -1, +1)
}

Vector3[] getAdjacent(Vector3 cube) {
Vector3[] adjacent = new Vector3[6];