1

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?

NeomerArcana
  • 1,978
  • 3
  • 23
  • 50
  • Consider using a map, to identify the tile instead. When you draw the map, draw the identity of the tiles in another map. Simply look up the tile identity based on the mouse click. It costs a bit of memory but is easy to implement and the technique is useful elsewhere. – Clearer Apr 03 '18 at 12:02
  • @Clearer do you mean like a frambuffer that uniquely identifies each pixel to a tile? – NeomerArcana Apr 03 '18 at 12:04
  • Yes, that's exactly what I mean. Only it's not a *framebuffer*. Just a map. – Clearer Apr 03 '18 at 12:28
  • @Clearer It's not clear how I would be able to produce this map if I can't map a pixel coordinate back to a tile? – NeomerArcana Apr 04 '18 at 00:46
  • When you draw the graphics, you should be able to tell which tile you're dealing with. Simply write the identify of that tile to an identity map that's as larger as the texture you're building. Construct a tile that has only two colours, one transparent and one solid, where the solid is the identify of the tile, then draw that to the identify map, in the same position as the original tile; you can construct it by applying a mask on the original tile, which masks out any transparent pixels and write the identify where solid colours appear. – Clearer Apr 04 '18 at 07:33
  • see [Improving performance of click detection on a staggered column isometric grid](https://stackoverflow.com/a/35917976/2521214) – Spektre May 16 '18 at 09:19

0 Answers0