3 Replies Latest reply: Feb 2, 2010 1:07 PM by 843853

# Isometric tiles, mouse location and grid lcation

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