等距瓷砖拾取/选择算法

时间:2019-02-10 19:38:48

标签: c algorithm math isometric

我正在开发一个等距游戏,在制定图块选择算法时遇到了麻烦。

这就是我渲染等轴测图的方式:

for (int x = 0; x < 50; x++) {
    for (int y = 0; y < 50; y++) {
        //Check if tile should be drawn
        if (mapdata[x][y] == 1) {
            float px = (x - y) * 20;
            float py = (x + y) * 20 / 2;

            ...

            window.draw(quad, &tile);
        } 
    }
}

我使用2D数组存储应绘制的图块。 例如:

int mapdata[5][5]
{
    0,1,1,1,0,
    0,1,1,1,0,
    0,1,1,1,0,
    0,1,1,1,0,
    0,1,1,1,0,
}

这是我目前“选择”图块的方式:

mh = the map tile height, in the above example this would be 5.
w = the width of the isometric tile.

int mouse_grid_y = (((mousey * 2) - ((mh * w) / 2) + mousex) / 2) / w;
int mouse_grid_x = (mousex - mouse_grid_y) / w;

任何帮助将不胜感激。

要澄清的图片:

这是我为游戏教程制作的图像。如您所见,有一个用绿色勾勒出的图块,这就是我需要的算法,我想跟踪鼠标,并在鼠标所在的图块上绘制绿色的“光标”。

img example

1 个答案:

答案 0 :(得分:1)

您可以将屏幕坐标转换为本地系统,以进行反向计算:

xx = px - basex
yy = py - basey
x' = (xx + 2 * yy) / 40   // integer division to get cell index
y' = (-xx  + 2 * yy) / 40

basexbasey是屏幕上绘制单元格0,0的起点坐标(问题中未提及)