Isometric screen to map issue

无人久伴 提交于 2019-12-08 09:14:33

问题


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.

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.


回答1:


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).

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.

We want to go from:

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

to:

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

Divide by (surface.Width) / 2 and (surface.Height) / 2:

(rx + globX) / (surface.Width / 2)  = x - y
(ry + globY) / (surface.Height / 2) = x + 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

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

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

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

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.



来源:https://stackoverflow.com/questions/9470564/isometric-screen-to-map-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!