Isometric grid mouseover?

Started by cilution, October 09, 2011, 01:33:28

Previous topic - Next topic

cilution

Hello, I've been working on this isometric tile game experiment and things are looking great, but I'm having a hard time understanding the math behind converting screen coordinates from Mouse.getX() and Mouse.getY() to my grid in order to be able to have some sort of mouseover behavior on tiles.

Basically, I want a method that will take the mouse's x and y coordinate's on the screen and be able to return the tile, if any, that exists at those coordinates. It would need to take into account scaling and translation. And I suck at math. :(

bartvbl

that really depends ipon whether you use glOrto() or glFrustrum() for your drawing space definition(i.e. do you mimic 3D, or do you use actual 3D?).
For 2D calculations you need to convert the fractional position of the mouse on the screen to the fractional position in your coordinate space;
mouseX / screenWidth = locationOnMap / widthOfOrthographic spectrum.
The calculation for the y axis is similar. Now I assumed here that the left and bottom values of glOrtho are defined as your origin. If they are not, you need to subtract the coordinates of the glOrtho origin from your coordinates. For example, say that you have the origin at the center of the screen;
(mouseX - screenWidth/2) / screenWidth
should give the correct map coordinates.
Now after you did that you will need to follow all the transformations you do toget the final coordinates (believe me; try drawing this stuff out on paper. it really helps).
As for your isometric scene; if your viewing angle is 45 degrees, moving your mouse up 1 unit will give 1 unit on rhe map times a constant.
For 3D scenes you'll have to use gluUnproject.

kulpa

I came upon the same problem and solved it, but now I'm to lazy to put the sources online, still want to make something of it.
But I can point you to the tutorial I found that helped me solve it.
It's for flash, but I think it's very java-convertable ;)

http://www.kirupa.com/developer/actionscript/isometric_transforms.htm

Math is your friend, thats what I keep saying everyone, give it a try ;)

btw. the method introduced there is very powerful, because it let you work in 3d space (y = jump) and have everything projected onto 2d plane (screen) and vice-versa (mouse (2d) -> world (3d)). And it's not that complicated at all!