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
toGrid
andtoIso
functions are clearly not reversible. If they are both accurate, you should be able to calltoIso
on a set of coordinates; and then calltoGrid
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. \$\endgroup\$