Jump to content
  • Advertisement
Sign in to follow this  
Ziden

Mouse Coords X Tile Coords

This topic is 2718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello there.

Im using a simple 4 dir isometric tiles, 30 px by 16 px each tile. All i wanted to do it, by given a specific mouse X and Y i could know witch tile it was clicked, Ive tryed doing this in many different ways, but i really couldnt solve this. Ive got a real problem on math. I assume this isnt that complicated, and indeed its kinda ignorance of mine posting it here, but it was a last solution, i just couldnt figure out how to do-it.

Any ideas ?

Share this post


Link to post
Share on other sites
Advertisement
If you could post the isometric grid you are using, then I could help you a lot easier. Otherwise there is a lot of guesswork to be done with the question currently.

Share this post


Link to post
Share on other sites
What I would do is pass the mouse location through a function which checks the x and y position values of tiles and see if they match up. What language are you using?

Share this post


Link to post
Share on other sites
30 by 16; I guess this means your "perspective displacement" at the top of a tile is 8 pixels, and that gap shrinks by 1 every 2 pixels going down.

So, given mouse world coordinates of (mouse_x, mouse_y) you can calculate the (x,y) position in the tilemap of the tile that is directly under the cursor by doing:

1. Find out which "non-isometric" 30x16 tile the mouse is in by dividing the mouse x and y coordinates by 30 and 16 respectively
2. Find out which vertical pixel of the tile the cursor is on by doing mouse_y % 16 (this is modulo, the remainder of the division of mouse_y by 16) -- this is how far from the top of the current tile the cursor is located. To find the world y coordinate of the top edge of the current tile, you do current_tile_top_edge_y = (mouse_y - (mouse_y % 16)).
3. (16 - (mouse_y % 16)) / 2 is equal to the horizontal distance between your mouse cursor and the left edge of the current tile (we divide by 2 because the gap shrinks by 1 every 2 vertical pixels). Just add (tile's y position in the tilemap) * 8 to it, and you've got the world coordinates of the left edge of the current isometric tile the cursor is currently on.
4. The tilemap coordinates of the top-left corner of the isometric tile the cursor is on are (current_tile_top_edge_x / 30), (current_tile_top_edge_y / 30)

Hopefully that makes sense and I didn't make any mistakes :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!