This discussion is archived
3 Replies Latest reply: Feb 2, 2010 11:07 AM by 843853 RSS

Isometric tiles, mouse location and grid lcation

843853 Newbie
Currently Being Moderated
Hi! I am making an RTS game like Age of Empires and have a problem with isometric tiles.
I managed to make a map with square-shaped tiles but to improve the game graphics I decided to move to isometric. The problem is that now I don´t know how to get the mouse position in the map according to each tile.
I used an int[][] array to hold every tile and did

locX=mouse_locX/TileSize;
locY=mouse_locY/TileSize;

mouse_flag= map[locX][locY];

But this won´t work on non-squared-shaped tiles... Any ideas? (I googled this problem but couldn´t find any practical solution...)

Thanks!
  • 1. Re: Isometric tiles, mouse location and grid lcation
    843853 Newbie
    Currently Being Moderated
    Make a shape out of them and use the Shape API to check if your click coordinates are within each tile.
  • 2. Re: Isometric tiles, mouse location and grid lcation
    843853 Newbie
    Currently Being Moderated
    I came across some posts that would do that, but I was wondering if there is any way to use mouse coordinates to calculate it straight forward just using that.
    I found a blog where a guy posted something like this, but I don´t understand the code...
    public PointF MapToAbsolute(PointF mapPoint)
    {
    PointF screenPoint = new PointF();
    screenPoint.X = (Scale / 2) * (mapPoint.X + mapPoint.Y);
    screenPoint.Y = (Scale / 4) * (mapPoint.X - mapPoint.Y);
    return screenPoint;
    }
    public PointF AbsoluteToMap(PointF screenPoint)
    {
    PointF mapPoint = new PointF();
    screenPoint.X /= (Scale / 2);
    screenPoint.Y /= (Scale / 4);
    
    mapPoint.X = (screenPoint.X + screenPoint.Y) / 2;
    mapPoint.Y = (screenPoint.X - screenPoint.Y) / 2;
    return mapPoint;
    }
    It is C++, but I guess it can be implemented in Java... the problem is that I don´t really get what is he doing.
    The whole article is here:
    http://experimentgarden.blogspot.com/2009/06/translating-mouse-position-over.html
    (It´s Andy´s answer)
  • 3. Re: Isometric tiles, mouse location and grid lcation
    843853 Newbie
    Currently Being Moderated
    That does not do what you think that does at all, it just gives coordinate conversion between scales, not from screen to tile. The easiest way, that I know of or can come up with, is as I've already suggested, that is unless you are going to make calculations for each piece of your tile external to a square--you'd have to make these relative addressed so you can apply the formulas to each piece of your tile area and across your map. Much Much easier to just use Shape.