Mouse location on isometric maps

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
zugamifk
Prole
Posts: 28
Joined: Sun Jul 26, 2009 4:01 pm

Mouse location on isometric maps

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Mouse location on isometric maps

Post 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.
Help us help you: attach a .love.
osuf oboys
Party member
Posts: 215
Joined: Sun Jan 18, 2009 8:03 pm

Re: Mouse location on isometric maps

Post 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.
If I haven't written anything else, you may assume that my work is released under the LPC License - the LÖVE Community. See http://love2d.org/wiki/index.php?title=LPC_License.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 78 guests