我正在开发一个等距游戏,但在制定图 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 上绘制这个绿色“光标”。
最佳答案
您可以将屏幕坐标转换为本地系统进行反向计算:
xx = px - basex
yy = py - basey
x' = (xx + 2 * yy) / 40 // integer division to get cell index
y' = (-xx + 2 * yy) / 40
basex
和 basey
是屏幕起点的坐标,其中绘制单元格 0,0
(您的问题中未提及)
关于c - 等距瓷砖拾取/选择算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54620246/