3
\$\begingroup\$

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};
}

Tile sheet

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

Tile picking

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


\$\endgroup\$
12
  • \$\begingroup\$ Does this help? gamedev.stackexchange.com/questions/14274/… \$\endgroup\$
    – Vaillancourt
    Sep 14 '17 at 15:45
  • \$\begingroup\$ It seems not to work in my case. Thanks though. \$\endgroup\$ Sep 14 '17 at 16:03
  • \$\begingroup\$ I don't fully understand what the problem is. Could you improve the question to highlight what you get and what you expect instead? \$\endgroup\$
    – Trilarion
    Sep 15 '17 at 10:33
  • \$\begingroup\$ 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. \$\endgroup\$ Sep 15 '17 at 11:07
  • \$\begingroup\$ 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. \$\endgroup\$
    – Jason H
    Sep 15 '17 at 17:07
1
\$\begingroup\$

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};
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .