gpt4 book ai didi

c++ - 等轴测图,鼠标位置->平铺坐标公式中的坐标不正确

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:55:13 24 4
gpt4 key购买 nike

EDIT Adjusted all the algorithms posted. It's now working NEARLY flawlessly. It's just the left-most 9 tiles that it fails on (A picture is at the bottom). It selects the completely wrong tile in most cases when hovering one of those nine.

EDIT2 I'm almost certain now that the problem is due to errors when screenX or screenY are negative, so it's probably down to using math.abs on tilePixelX/Y, but it can't just be that, otherwise the results wouldn't be so far off (Although it could well be contributing)

我正在制作一款等距游戏,目前我正在研究 map 渲染引擎。理论上它应该相当简单,但我无法从鼠标位置获取平铺坐标。

我的瓷砖是 64 宽和 32 高。如下图所示:

enter image description here

我的 map 显示相当完美:

实际问题在顶部解释。

这是我现在使用的代码:

int screenX = sf::Mouse::getPosition(window).x - 250;
int screenY = sf::Mouse::getPosition(window).y - 250;

int tilex = floor((screenY / 32) + (screenX / 64));
int tiley = floor((screenY / 32) - (screenX / 64));

int tilePixelX = abs(screenX % 64); // I believe part of the problem
int tilePixelY = abs(screenY % 32); // is absoluting these. Not all though.

sf::Color mCol = mouseMap.getPixel(tilePixelX, tilePixelY);

if (mCol.r == 20)
tilex -= 1;
else if (mCol.r == 40)
tiley -= 1;
else if (mCol.r == 60)
tilex += 1;
else if (mCol.r == 80)
tiley += 1;

我现在使用的是鼠标贴图,效果非常好,但是如上所述,它在菱形中最左边的九个方 block 上仍然失败(当你将它们悬停时,它会选择一个看似随机的大约 4-5 个方 block ,或者没有平铺(我猜超出了 map 的范围):

Pic

这还显示了以红色突出显示的问题图 block 。

这就是 map 的样子,让您了解我的渲染引擎是如何工作的。请注意,透明像素被认为是空的,黑色像素被认为是墙(我没有为上面的示例图片渲染,所以上面图片上显示的瓷砖都不是第 0 列或第 0 行,它们也不是最后一行。

enter image description here

我的鼠标 map 肯定是正确的,因为大部分 map 都在工作,但无论如何:

enter image description here

编辑:

我已经将必要的文件和 DLL 打包到一个 zip 文件中,这样您就可以运行它并亲眼看看发生了什么:

https://dl.dropbox.com/u/37019412/Broken.zip

绘图公式为:

(x - y) * 32 + 250, (x + y) * 16 + 250

在 cometd 之前是 X,之后是 Y,在 x 和 y 内部是图 block 坐标。

最佳答案

看教程,好像需要把鼠标坐标减去256(224加上宽度的一半)。告诉我们这是否有效!

并且请不要每次绘制一个 Sprite 时都动态分配一个新的 Sprite ,只是用相同的方法删除它!好的,我会尽量避免对您的代码做进一步的评论。 ;)

关于c++ - 等轴测图,鼠标位置->平铺坐标公式中的坐标不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10768865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com