# Isometric tiles drawing and picking - JAVA

I'm trying to draw isometric tiles in Java and implement a tile picking system using the mouse cursor. I draw the tiles using math formulas I found and adapted to my tile textures which you can find below. Tiles are 64x64px but flat tiles are only 32px height even if I draw them using the 64x64 sprite.

public int[] toIso(int x, int y){

int i = (x - y) * tileWidthHalf;
int j = (x + y) * tileHeightQuarter;

i+=xOffset;
j+=yOffset;

return new int[]{i,j};
}

public int[] toGrid(int x, int y){

x-=xOffset;
y-=yOffset;

int i = (x/tileWidthHalf + y/tileHeightQuarter)/2;
int j = (y/tileHeightQuarter - x/tileWidthHalf)/2;

return new int[]{i,j};
}


The map is a simple 2d array where my tiles are represented by their id.

Here is a video showing what is actually happening: youtu.be/baCVIfJz2Wo

• Does this help? gamedev.stackexchange.com/questions/14274/… – Vaillancourt Sep 14 '17 at 15:45
• It seems not to work in my case. Thanks though. – RomainImberti Sep 14 '17 at 16:03
• I don't fully understand what the problem is. Could you improve the question to highlight what you get and what you expect instead? – Trilarion Sep 15 '17 at 10:33
• Well, I'm trying to detect which tile is under my mouse when hovering the grid and render it with a different texture for the moment. The problem is, the tile selection isn't accurate at all, as you can see in the video I provided. – RomainImberti Sep 15 '17 at 11:07
• The toGrid and toIso functions are clearly not reversible. If they are both accurate, you should be able to call toIso on a set of coordinates; and then call toGrid on that result and end up with essentially the same coordinates you started with (working with integers may mean you are off by a pixel or so however). Write yourself a quick unit test which dumps the coordinates into the console and see what results you get. If they vary wildly, then your calculations are likely wrong. – Jason H Sep 15 '17 at 17:07

just wanted to say that I finally solved it. It was just a conversion to int issue. These are the final methods that I use. Hope it will help people who were trying to work with isometric tiles. Thank you !

public static int[] toIso(int x, int y){

int i = (x - y) * TILE_WIDTH_HALF;
int j = (x + y) * TILE_HEIGHT_QUARTER;

i += xOffset-TILE_WIDTH_HALF;
j+=yOffset;

return new int[]{i,j};
}

public static int[] toGrid(double i, double j){

i-=xOffset;
j-=yOffset;

double tx = Math.ceil(((i / TILE_WIDTH_HALF) + (j / TILE_HEIGHT_QUARTER))/2);
double ty = Math.ceil(((j / TILE_HEIGHT_QUARTER) - (i / TILE_WIDTH_HALF))/2);

int x = (int) Math.ceil(tx)-1;
int y = (int) Math.ceil(ty)-1;

return new int[]{x, y};
}