Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm not all that good with Maths. I'm trying to make a function to convert mouse coordinates into a particular tile in an isometric view.

enter image description here

All of the algorithms I have seen so far work with the X & Y axes going diagonal, my game is currently set up like this, and I would like to keep it so.

Is there an algorithm so that if the mouse was at the red dot, it would return the coordinates of the tile that it is sitting on? (6,2)

share|improve this question
    
possible duplicate of 2D isometric: screen to tile coordinates –  Byte56 Nov 30 '12 at 16:09
    
It isn't, Their coords continue is the diagonal direction, it's different to what i currently have. –  Dylan Lundy Nov 30 '12 at 16:20
    
What direction would you say your coordinates go in? How are you storing your tiles? In a grid, then transforming the view? –  Byte56 Nov 30 '12 at 16:21
    
The tiles aren't stored anywhere yet, they're just rendered, in each collumn every second tile is offset a little to make the darker one. And the x coordinates go horizontally and the y coordinates go vertically –  Dylan Lundy Nov 30 '12 at 16:31
    
Sounds like the algorithm will be very particular to your design. You'll have to update your question with more details about the way you're rendering your tiles. Sounds like a regular grid with some offsets, shouldn't be too hard to get a picking algorithm straight from screen coordinates. –  Byte56 Nov 30 '12 at 17:06
show 1 more comment

1 Answer

Well, I'm assuming that for a given tile, you know the real-axes coordinates for each of its corners. i.e for the given tile (1,0) in your picture, you know its left-most corner is at, let's say, (5,10) on the actual xy axes. If so, you can use Java's Polygon class: create a Polygon instance containing a given tile's xy coordinates for each of its corners, then use the contains(x,y) method giving it as arguments the position of the mouse cursor, and it will tell you if mouse of tile or not:

int[] tileXcoords = new int[tileLeftMostCornerX, tileLowerCornerX, tileRightMostCornerX,tileUpperCornerX];
int[] tileYcoords = //... similar for the y coors (keep the order of the corners the same as for the x array)

Polygon tile = new Polygon(tileXcoords, tileYcoords, 4);
if(tile.contains(mouseX, mouseY)) {
    //mouse is over tile
}
share|improve this answer
    
-1 You recommend making a new Polygon for every tile on screen and checking each one with tile.contains()? This is not to check to see if tile X is the one being selected, it's to find the tile being selected. –  Byte56 Nov 30 '12 at 18:07
    
Well, the OP says " if the mouse was at the red dot, it would return the coordinates of the tile that it is sitting on". If you have a Tile object that stores its tile-system coordinate, using Polygon to calculate if the mouse is on it or not will effectively tell you that the mouse is on that tile.True the OP asks for an algorithm that does this calculation, and I propose an already implemened solution, but I think it would work... –  Shivan Dragon Nov 30 '12 at 23:18
    
Your example doesn't show what happens when it's not contained in tile, what happens when it's not in that tile? Clearly some tile is selected. –  Byte56 Nov 30 '12 at 23:23
    
Ah, I see. Agreed, this only works for space covered by tiles. Having your mouse outside any tile will not give you any tile-system information about your cursor position using my solution. –  Shivan Dragon Nov 30 '12 at 23:30
    
No, I mean, why even test if the cursor is in the bounds of the tile? If you've already selected the tile, and you're not going to even test any other tiles, why even test to see if it's in bounds? This presumes we can already select the tile that's at the mouse coordinates, then use that tile to test to see if it's at the mouse coordinates. See what I'm saying? –  Byte56 Nov 30 '12 at 23:37
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.