# Isometric Screen View to World View

I am having trouble working out the math to transform the screen coordinates to the Grid coordinates.

The code below is how far I have got but it is totally wrong any help or resources to fix this issue would be great, had a complete mind block with this for some reason.

    private Point ScreenToIso(int mouseX, int mouseY)
{
int offsetX = WorldBuilder.STARTX;
int offsetY = WorldBuilder.STARTY;

Vector2 startV = new Vector2(offsetX, offsetY);

int mapX = offsetX - mouseX;
int mapY = offsetY - mouseY + (WorldBuilder.tileHeight / 2);

mapY = -1 * (mapY / WorldBuilder.tileHeight);
mapX = (mapX / WorldBuilder.tileHeight) + mapY;

return new Point(mapX, mapY);
}


As a side-note, these coordinates make more sense if you interpret them as (Y, X).

You can define a 'vertical line' (from bottom to right) as y = -0.5x + a And you can define a 'horizontal line' (from top to right) as y = 0.5x + b What you need is an algorithm to calculate A. You can obtain it from a grid drawing function - if you draw a line every 50 pixels, the a is 0 for first (border) line, 50 for second line, 100 for third line and so on. Now when you get a mouse coordinates, all you need is to find such a that the equation is true.

y = -0.5x + a
mouse coordinates: 403, -12
-12 = -0.5*403 + a
-12 = -201.5 + a
a = -12+201.5
a = 189.5

y = 0.5x + b
-12 = 0.5*403 + b
b = -12 - 201.5
b = -213.5


a is a value added to 'vertical lines', that is it's an equivalent of X on the tile. b is equivalent for Y.

by using modulo operator %, we can calculate cursor at this position is above tile (4, -5), though I could make a mistake somewhere. if You make a screenshot and show the grid drawing code I'll be able to post an accurate code.

• Your explanation pushed me to right direction. Thanks – Chris Crew Sep 23 '12 at 19:51
• Your Updated code is the same approach I made in the end, turned the lines into equations then used this to calculate the intersect – Chris Crew Sep 24 '12 at 8:14