2D isometric: screen to tile coordinates

Posted by Dr_Asik on Game Development See other posts from Game Development or by Dr_Asik
Published on 2012-04-25T19:39:32Z Indexed on 2012/11/13 5:17 UTC
Read the original article Hit count: 326

Filed under:
|
|

I'm writing an isometric 2D game and I'm having difficulty figuring precisely on which tile the cursor is. Here's a drawing:

where xs and ys are screen coordinates (pixels), xt and yt are tile coordinates, W and H are tile width and tile height in pixels, respectively. My notation for coordinates is (y, x) which may be confusing, sorry about that.

The best I could figure out so far is this:

int xtemp = xs / (W / 2);
int ytemp = ys / (H / 2);
int xt = (xs - ys) / 2;
int yt = ytemp + xt;

This seems almost correct but is giving me a very imprecise result, making it hard to select certain tiles, or sometimes it selects a tile next to the one I'm trying to click on. I don't understand why and I'd like if someone could help me understand the logic behind this.

Thanks!

© Game Development or respective owner

Related posts about geometry

Related posts about isometric