如何将鼠标坐标调整到等距瓷砖坐标以补偿菱形瓷砖?

时间:2018-04-03 11:11:08

标签: c++ isometric

我的绘图功能如下所示:

    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});
        }
    }

当用户点击鼠标时,我试图点击该拼贴的x,y坐标。要将鼠标坐标转换为地图坐标,请执行以下操作:

        // 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);

我通过solving my draw positions for x,y

提出了上述内容

我得到的结果似乎在正确的瓷砖上。但问题是如果我点击中途过去&#34;瓦片的点,在x或y坐标中,它将它归属于下一个图块(因为它们是带有透明角的正方形,以形成&#34;菱形&#34;形状)。

如何调整上述内容以进行补偿?

0 个答案:

没有答案