Sign in to follow this  
Dr_Asik

2D isometric: screen to tile coordinates

Recommended Posts

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

[Not allowed to post pictures :( here's a link : [url="http://imageshack.us/photo/my-images/840/tilespace.png/"]http://imageshack.us.../tilespace.png/[/url] ]

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.

The best I could figure out so far is this:

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

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!

Share this post


Link to post
Share on other sites
I wrote an article on the subject a while ago: http://www.wildbunny.co.uk/blog/2011/03/27/isometric-coordinate-systems-the-modern-way/

Hope it helps!

Cheers, Paul.

Share this post


Link to post
Share on other sites
Use a mouse map to make things way easier: [url="http://www.gamedev.net/page/resources/_/technical/game-programming/isometric-n-hexagonal-maps-part-i-r747"]Isometric 'n' Hexagonal Maps Part I[/url]
(Skip down to the part labeled '[b]Mouse Matters[/b]' and things will become clear)

Share this post


Link to post
Share on other sites
[quote name='wildbunny' timestamp='1335565948' post='4935482']
I wrote an article on the subject a while ago: [url="http://www.wildbunny.co.uk/blog/2011/03/27/isometric-coordinate-systems-the-modern-way/"]http://www.wildbunny...the-modern-way/[/url]

Hope it helps!

Cheers, Paul.
[/quote]Wow, such a simple approach. I ended up using a transformation matrix composed of a translation, a rotation and a scaling, so that getting going back-and-forth between pixels and tile coordinates is as simple as applying the transformation or its inverse; but this seems even simpler.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this