Page 1 of 1

Mouse location on isometric maps

Posted: Fri Jul 16, 2010 6:15 pm
by zugamifk
Wow, this is a tough one. I usually have an easy time figuring out how to solve small problems like this, but I'm stuck.
I'm trying to write a function that return which tile the mouse is currently hovering over on an isometric map. The code I sue to draw the tiles is the same as in the tutorial:

Code: Select all

-- earth
love.graphics.drawq( images.world.tiles.earth[5],
	quads.tiles.earth.default[1][map.tile_map[y+map_y][x+map_x].earth.image],
        ( x - y ) * ( map.tile_w / 2 ) + map.offset_x + scroll_x,
        ( x + y ) / 2 * map.tile_h + map.offset_y + scroll_y,
	0,
	map.scale_x,
	map.scale_y,
	1,
	1
)
Given that, how do I write a function that returns the X and Y values for which tile the mouse is over?

Re: Mouse location on isometric maps

Posted: Fri Jul 16, 2010 7:11 pm
by Robin
Someone else had this problem too (well, a slightly different one, but it is roughly the same): http://love2d.org/forums/viewtopic.php? ... =10#p16192

Note that it uses a z coordinate, which you can discard.

The first one is the function you can use to base your own solution on.

Re: Mouse location on isometric maps

Posted: Sat Jul 17, 2010 2:16 pm
by osuf oboys
I suggest you make functions for going between the two coordinate systems. Write it out the maths. Let (x,y) be the old coordinates and (s,t) the new. There are some constants a,b,c,d,e,f such that

s = ax + by + c
t = dx + ey + f


For you, a would be map.tile_w / 2 , b would be -map.tile_w / 2 etc. All you have to do is invert it, i.e. express x in terms of s and t by cancelling out y, and then the same for y while cancelling out x. (although I would get rid of c and f and leave that for the view)

The case that Robin is referring to is considerably more advanced since your screen coordinates - a 2D space - in general does not correspond to a unique point in a rendered 3D space. Instead they have to generate all the possible candidates and see if there is a corresponding tile. You can use it for inspiration but beware that they are doing much more than you need to.