0

我知道关于等轴测地图的很多建议,但我已经阅读了其中的大部分内容并没有解决我的问题。为了更简单,我为 C# 重写了代码(此代码将在 Android 平台上使用)我需要将屏幕线转换为等距坐标。

在这里,我为我使用了 1:2 的 64x32 瓷砖,我使用此代码构建了菱形地图

private void drawIsoGrid(PaintEventArgs e)
{
    for(int y=0;y<20;y++)
        for(int x=0;x<20;x++)
        {
            float rx = (x - y) * (surface.Width) / 2 - globX;
            float ry = (x + y) * (surface.Height) / 2 - globY;
            e.Graphics.DrawImage(surface,rx,ry);
        }

我还使用全局锚点在这里滚动我的地图代码

protected override void OnMouseMove(MouseEventArgs e)
{
    mouseCoordsX = e.X;
    mouseCoordsY = e.Y;
    if(e.Button==MouseButtons.Left)
    {
        globX += prevX - e.X;
        globY += prevY - e.Y;
        this.Invalidate();
    }
    prevX = e.X;
    prevY = e.Y;            
}

主要问题是如何在鼠标下获得瓷砖哪个公式对我有用。

4

1 回答 1

0

由于这个问题尚未得到解答,而且它是此处“等距屏幕”的最佳结果之一,我想我会回答这个问题(更不用说我刚刚完成了这个问题)。

由于您有一个从 iso 网格映射到屏幕坐标的函数,并且它是一个具有逆向的线性变换,我们可以向后获取另一个函数。所以让我们这样做。

我们想从:

rx = (x - y) * (surface.Width) / 2 - globX
ry = (x + y) * (surface.Height) / 2 - globY

到:

x = <something>
y = <something>

同时解决这些问题是最容易的。将全局变量添加到两侧:

rx + globX = (x - y) * (surface.Width) / 2
ry + globY = (x + y) * (surface.Height) / 2

除以(surface.Width) / 2(surface.Height) / 2

(rx + globX) / (surface.Width / 2)  = x - y
(ry + globY) / (surface.Height / 2) = x + y

几乎完成了,让我们将两个方程加在一起以消除y's:

(rx + globX) / (surface.Width / 2) + (ry + globY) / (surface.Height / 2) = 2 * x

现在摆脱 x 从第二个方程中减去第一个方程:

(ry + globY) / (surface.Height / 2) - (rx + globX) / (surface.Width / 2) = 2 * y

将两个方程除以 2,我们暂时完成了:

x = ((rx + globX) / (surface.Width / 2) + (ry + globY) / (surface.Height / 2)) / 2
y = ((ry + globY) / (surface.Height / 2) - (rx + globX) / (surface.Width / 2)) / 2

很酷,现在您有了屏幕上的网格坐标。让我们清理一下,因为我们基本上(a / (b / c)) / c与 相同a / b,我们可以去掉c's,在这种情况下是2's:

x = (rx + globX) / surface.Width + (ry + globY) / surface.Height
y = (ry + globY) / surface.Height - (rx + globX) / surface.Width

因此,您应该能够编写一个函数来获取屏幕 x 和 y 位置并返回 x 和 y 网格位置。我对 c# 不是很熟悉,所以我不知道它如何处理应该是 int 的浮点值,但是由于你在 android 上运行它,我想这并不重要。

于 2012-07-30T01:31:25.847 回答