Jump to content

  • Log In with Google      Sign In   
  • Create Account


Converting mouse coords to isometric tile map coords?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Relutap   Members   -  Reputation: 125

Like
0Likes
Like

Posted 25 March 2014 - 12:34 AM

I'm using C#/XNA to make a 2D game.  Currently I have made a program that draws 64x32 isometric tile map to the screen.  I want to know what tile the user clicked on by getting the cursor's screen position.  Only problem is that the math is confusing me... I'm not sure how to convert cursor coords to tile map coords without writing a billion if statements.  I could figure this out very easy if I was using square tiles instead.  Any advice would be helpful, thank you!

 



Sponsor:

#2 Lactose!   GDNet+   -  Reputation: 3207

Like
0Likes
Like

Posted 25 March 2014 - 03:00 AM

Assuming 64 is the x resolution and 32 is the y resolution.

The final answer will depend on how your tiles are ordered. If you keep x and y indices separate, it'll be:

tile_x = cursor_x / 64

tile_y = cursor_y / 32

 

If you're storing them as just tile index, you'll need to do some modulo and division work to get where you want (e.g. if you want to get "tile 15" instead of "tile [3, 2]").

If so, you'll need to provide a bit more information.

---

EDIT: Wow, completely missed the part about isometric. I'll update with a better reply later on, unless someone beats me to it. Sorry for the confusion.


Edited by Lactose!, 25 March 2014 - 03:02 AM.


#3 Lactose!   GDNet+   -  Reputation: 3207

Like
0Likes
Like

Posted 25 March 2014 - 03:13 AM

Right, after some morning caffeine, here's a slightly better reply.

 

The actual answer will depends on both the shape & size of your tiles, and how you're drawing the map (e.g. where 0,0 is), etc.

 

Without any more specifics (an image with coordinates would be nice), I think the best I can do is to refer you to the following:

http://gamedev.stackexchange.com/questions/34787/how-to-convert-mouse-coordinates-to-isometric-indexes



#4 diventurer   Members   -  Reputation: 827

Like
0Likes
Like

Posted 25 March 2014 - 04:08 AM

If you want to know whether the mouse is over a tile or not, you can use a mask.

bool mouse_over_mask(Tile* tile, Mouse* mouse, Image* mask) {
    if (mouse->over(tile->position(), tile->size()) {
        MaskX = mouse->x() - tile->x();
        MaskY = mouse->y() - tile->y();
        return (mask->pixel_at(MaskX, MaskY) == 0xFF000000); // remember to check endianness
    }
    return (false);
}

Here is an example pseudo-codeish implementation.


Edited by diventurer, 25 March 2014 - 05:23 AM.


#5 dworm   Members   -  Reputation: 346

Like
0Likes
Like

Posted 25 March 2014 - 06:28 AM

if you are unfamiliar with a matrix just use basic geometry

you can calculate the distance between each axe and your point and factor for your tile dimension



#6 kseh   Crossbones+   -  Reputation: 1987

Like
1Likes
Like

Posted 25 March 2014 - 10:07 AM

An old article from here:

 

http://www.gamedev.net/page/resources/_/technical/game-programming/isometric-n-hexagonal-maps-part-i-r747






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS