My drawing function looks like this:
for(std::size_t y = 0; y < mapSizeY; ++y)
{
for(std::size_t x = mapSizeX-1; x != static_cast<std::size_t>(-1); --x)
{
auto tile = GetTile(x,y);
int64_t xPos = (x * TILE_WIDTH / 2) + (y * TILE_WIDTH / 2);
int64_t yPos = (y * TILE_HEIGHT / 2) - (x * TILE_HEIGHT / 2);
xPos += camera.GetXOffset();
yPos += camera.GetYOffset();
yPos -= tile->z * TILE_HEIGHT/2; // everything is at height 1 for now
auto zoom = camera.GetZoomFactor();
xPos *= zoom;
yPos *= zoom;
if(xPos < 0-TILE_WIDTH*zoom || yPos < 0-TILE_HEIGHT*zoom)
continue;
if(xPos > GetScreenDimensions().x || yPos > GetScreenDimensions().y)
continue;
// x is up right
// y is down right
Blit(m_grass,{xPos,yPos,TILE_WIDTH*zoom,TILE_HEIGHT*zoom+TILE_HEIGHT/2*zoom});
}
}
When a user clicks the mouse, I'm trying to get the x,y coord of the tile clicked on. To translate the mouse coords to the map coords I'm doing the following:
// x and y are the mouse coords
auto zoom = camera.GetZoomFactor();
auto xOffset = camera.GetXOffset();
auto yOffset = camera.GetYOffset();
int z = 1; // every tile is at height 1 for now
int32_t xTile = -(TILE_HEIGHT * (2 * xOffset * zoom + TILE_WIDTH * z * zoom - 2 * x) + 2 * TILE_WIDTH * (y - yOffset * zoom)) / (2 * TILE_HEIGHT * TILE_WIDTH * zoom);
int32_t yTile = (-2 * TILE_HEIGHT * xOffset * zoom + TILE_HEIGHT * TILE_WIDTH * z * zoom + 2 * TILE_HEIGHT * x - 2 * yOffset * TILE_WIDTH * zoom + 2 * TILE_WIDTH * y) / (2 * TILE_HEIGHT * TILE_WIDTH * zoom);
I came up with the above by solving my draw positions for x,y
The results I get appear to be on the correct tile. But the issue is that if I click past the "halfway" point of a tile, in either x or y coordinate, it attributes it to the next tile (because they're squares with transparent corners to make the "diamond" shape).
How can I adjust the above to compensate?