c++ - 等轴测图,鼠标 pos->tile 坐标公式中的坐标不正确

标签 c++ math collision sfml isometric

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 渲染引擎。理论上它应该相当简单,但我只是无法从鼠标位置获取图 block 坐标。

我的图 block 宽 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 的样子,让您了解我的渲染引擎是如何工作的。请注意,透明像素被视为空,黑色像素被视为墙壁(我没有为上面的示例图片渲染,因此上面图片中显示的图 block 都不是第 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

逗号之前是 X,之后是 Y,里面的 x 和 y 是图 block 坐标。

最佳答案

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

请不要在每次绘制 Sprite 时动态分配一个新 Sprite ,然后用相同的方法将其删除!好的,我会尽量避免对您的代码做出进一步的评论。 ;)

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

相关文章:

c++ - 冒泡排序中的交换次数

java - (Java) 卡在方法上

c - 用于 RuneScape 体验的 C 数学公式

flash - Box2d As3 contact listener问题

c++ - 实现图形流水线

c++ - 在 Qt 5.4 中跨类使用最新的 OpenGL 函数

c++ - WM_COMMAND内的消息框不起作用! (WIN32 API)

swift - didBeginContact 未调用

swift - xcode中的三体碰撞检测问题

c++ - 从另一个类中修改对象的 std::vector