# VictorErmakov

Member

0

100 Neutral

• Rank
Newbie
1. ## Coordinates in Hexagon-Based Tile Maps

Thanks for useful info. I can propose my C++ implementation for you algorithm. In my case i use inverted grid (X -> Y). [code] u32 SectX = (ScrPosition.X + m_CameraScroll.X) / (float)MAP_TILE_WIDTH / 0.75f; u32 SectY = (ScrPosition.Y + m_CameraScroll.Y) / (float)MAP_TILE_HEIGHT; u32 SectPxlX = (TScreenPosition.X + m_CameraScroll.X) % (int)((float)MAP_TILE_WIDTH * 0.75f); u32 SectPxlY = (TScreenPosition.Y + m_CameraScroll.Y) % MAP_TILE_HEIGHT; m_GridPosition.X = SectX; m_GridPosition.Y = SectY; if(SectX%2) { // right side if(SectPxlY >= (float)MAP_TILE_HEIGHT / 2.0f) { if(SectPxlX < (2 * (float)MAP_TILE_WIDTH * 0.25f - SectPxlY * 0.5)) { m_GridPosition.X = SectX - 1; m_GridPosition.Y = SectY; } else { m_GridPosition.X = SectX; m_GridPosition.Y = SectY; } } // left side if(SectPxlY < (float)MAP_TILE_HEIGHT / 2.0f) { if(SectPxlX < (SectPxlY * 0.5f)) { m_GridPosition.Y = SectY; m_GridPosition.X = SectX - 1; } else { m_GridPosition.Y = SectY - 1; m_GridPosition.X = SectX; } } } else { // middle m_GridPosition.Y = SectY; m_GridPosition.X = SectX; // left Edge if(SectPxlX < ((float)MAP_TILE_WIDTH * 0.25f - SectPxlY * 0.5f)) { m_GridPosition.Y = SectY - 1; m_GridPosition.X = SectX - 1; } // right Edge if(SectPxlX < (- (float)MAP_TILE_WIDTH * 0.25f + SectPxlY * 0.5f)) { m_GridPosition.Y = SectY; m_GridPosition.X = SectX - 1; } } [/code]