c - 等距瓷砖拾取/选择算法

标签 c algorithm math isometric

我正在开发一个等距游戏,但在制定图 block 选择算法时遇到了困难。

这就是我渲染等距图 block map 的方式:

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

我使用二维数组来存储应绘制的图 block 。 示例:

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,
}

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

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;

任何帮助将不胜感激。

澄清图片:

这是我为游戏教程制作的图像。正如您所看到的,有一个标有绿色轮廓的图 block ,这就是我需要的算法,我想跟踪鼠标,并在鼠标所在的图 block 上绘制这个绿色“光标”。

img example

最佳答案

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

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 (您的问题中未提及)

关于c - 等距瓷砖拾取/选择算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54620246/

相关文章:

c - 合并排序算法中的运行时错误

android - 相当于 Android 中的 arc4random()

C:读取二进制并写入编码文本

C套接字问题-随机客户端地址和端口号

algorithm - 如何提取三个主要掌纹

algorithm - 如何在 jaspersoft adhoc 中使数据以单独的词而不是 XML 中的一个词显示

javascript - 解决这个排序问题的最佳方法是什么?

security - 给定带有时间戳的旧值是否可以预测 future 的 2FA 值?

python : efficient bytearray incrementation

c - 从函数返回一个二维数组到主函数