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)

  • \$\begingroup\$ possible duplicate of 2D isometric: screen to tile coordinates \$\endgroup\$ – MichaelHouse Nov 30 '12 at 16:09
  • \$\begingroup\$ It isn't, Their coords continue is the diagonal direction, it's different to what i currently have. \$\endgroup\$ – Dylan Lundy Nov 30 '12 at 16:20
  • \$\begingroup\$ What direction would you say your coordinates go in? How are you storing your tiles? In a grid, then transforming the view? \$\endgroup\$ – MichaelHouse Nov 30 '12 at 16:21
  • \$\begingroup\$ 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 \$\endgroup\$ – Dylan Lundy Nov 30 '12 at 16:31
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Nov 30 '12 at 17:06

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
| improve this answer | |
  • \$\begingroup\$ -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. \$\endgroup\$ – MichaelHouse Nov 30 '12 at 18:07
  • \$\begingroup\$ 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... \$\endgroup\$ – Shivan Dragon Nov 30 '12 at 23:18
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Nov 30 '12 at 23:23
  • \$\begingroup\$ 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. \$\endgroup\$ – Shivan Dragon Nov 30 '12 at 23:30
  • \$\begingroup\$ 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? \$\endgroup\$ – MichaelHouse Nov 30 '12 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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