I'm making a hexagon tile based game, which generates a honeycomb arrangement of hexagons in a spiral shape. My goal is to convert a world position (e.g. a mouse click) to a spiral coordinate (index / layer / position around layer). I can't think of how to do this, so instead I've been looking to simplify the problem by converting to/from axial coordinates first. How can this be done?
My configuration is pointy-topped as follows:
And here are the spiral indexes:
Axial Coordinates for reference:
I already have these equations for spiral coordinates:
const Layer = (index: number): number => {
return Math.floor((3 + Math.sqrt(12 * index - 3)) / 6);
}
const Position = (index: number, layer: number): number => {
return index - 3 * layer * (layer - 1) - 1;
}
const Index = (layer: number, position: number): number => {
return 3 * layer * (layer - 1) + 1 + position;
}