等距屏幕到地图问题 [英] Isometric screen to map issue
问题描述
我知道关于等轴测图的很多建议,但是我已经阅读了大多数建议,但并没有解决我的问题. 我为C#重写了代码,以简化操作(此代码将在Android平台上使用) 我需要将屏幕线接到等轴测坐标上.
I know that about isometric map a lot of advice but I've read most of them and didn't solve my problem. I rewrite code for C# for more simplicity (this code will be used on Android platform) I need to get screen cords to isometric coords.
在这里,我为我使用64:32的1:2瓦片,并使用此代码构建菱形贴图
Here we go I used 1:2 tiles for me 64x32, I build diamond map using this code
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);
}
我还使用全局锚来滚动地图 代码在这里
I also use global anchor for scroll my map code here
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;
}
主要问题是如何在鼠标下方平铺哪个公式对我有用.
the main question is how to get tile under the mouse which formula will be useful for me.
推荐答案
由于尚未得到回答,并且这是等距屏幕"的最佳结果之一,因此我想我会回答这个问题(而不是提到我刚刚完成此操作.)
Since this hasn't been answered, and it is one of the top results for "isometric screen" here, I figured I'd answer this (not to mention that I just finished this).
由于您具有一个从iso网格映射到屏幕坐标的函数,并且它是带有反函数的线性变换,因此我们可以向后进行操作以获取另一个函数.因此,让我们做到这一点.
Since you have a function that maps from an iso grid to screen coordinates, and it's a linear transform with an inverse, we can just go backwards to get the other function. So let's do just that.
我们要去:
rx = (x - y) * (surface.Width) / 2 - globX
ry = (x + y) * (surface.Height) / 2 - globY
收件人:
x = <something>
y = <something>
同时解决这些问题最简单.将全局变量添加到两侧:
It's easiest to solve these at the same time. Add the globals to both sides:
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
的情况:
Almost done, let's add both equations together to get rid of the y
's:
(rx + globX) / (surface.Width / 2) + (ry + globY) / (surface.Height / 2) = 2 * x
现在要摆脱x,从第二个方程中减去第一个方程:
Now to get rid of the x's subtract the first equation from the second:
(ry + globY) / (surface.Height / 2) - (rx + globX) / (surface.Width / 2) = 2 * y
将两个方程式都除以2,我们已初步完成:
Divide both equations through by 2, and we're tentatively done:
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
相同的(a / (b / c)) / c
,所以可以摆脱c
的情况,在这种情况下是2
的情况:
Cool, now you have the grid coordinates in terms of the screen. Let's clean some of this up, since we have basically (a / (b / c)) / c
which is the same as a / b
, we can get rid of the c
's, in this case the 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的float值,但是由于您是在android上运行的,因此我认为这并不重要.
So you should be able to write up a function that takes the screen x and y positions and returns the x and y grid positions. I am not incredibly familiar with c# so I don't know how it handles float values that should be int's, but since you're running this on android, I suppose it doesn't really matter.
这篇关于等距屏幕到地图问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!