Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Before you make a thread asking for help, read this
- Posts: 28
- Joined: Sun Jul 26, 2009 4:01 pm
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
( x - y ) * ( map.tile_w / 2 ) + map.offset_x + scroll_x,
( x + y ) / 2 * map.tile_h + map.offset_y + scroll_y,
Given that, how do I write a function that returns the X and Y values for which tile the mouse is over?
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
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.
- Party member
- Posts: 215
- Joined: Sun Jan 18, 2009 8:03 pm
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.
Users browsing this forum: No registered users and 93 guests