One for the mathematicians: mapping screen click to iso grid
Posted: Sun May 02, 2021 12:55 am
Never done iso maps before so never realised how hard this is. I have an isometric tiled map where 1,1 is in the top corner and 1,col is in the right corner of the screen (orientation matters). When there is a mouse-click on a tile, I need to work out which tile was clicked. I watched this very good tutorial where the first 10 minutes explains the maths but then he jumps to C# or C++ or to a language I can't even identify
https://www.youtube.com/watch?v=ukkbNKTgf5U
None the less - I pick through the demo code he provides here:
https://github.com/OneLoneCoder/olcPix ... cTiles.cpp
and I think I've done okay translating the C into LUA:
The approach is to 'overlay' an invisible left/right,up/down grid over the isometric grid, determine which cell was clicked, then determine which corner of the cell was clicked, and then map that to the iso grid.
I have two problems:
a) is my code conversion actually correct (I think it is)
b) in the tutorial, he uses some sort of colour mask thingy to detect corner clicks. Anyone familiar with the subject might have seen this before:
Other languages can check which colour was clicked and thereby know what corner was clicked. Is there a similar way in lua/love2d?
Here is my tiled map for reference:
Finally, I'm happy to use some module or engine or something with this but things like STI and tileclick either don't do isometric or don't do forever expanding random maps (i.e you have to handcraft each map).
This is just day 2 on this project and I'm pretty chuffed with the progress already. With so many isometric dungeon-crawlers out there I'm hoping this is an easy problem to fix. Thanks.
https://www.youtube.com/watch?v=ukkbNKTgf5U
None the less - I pick through the demo code he provides here:
https://github.com/OneLoneCoder/olcPix ... cTiles.cpp
and I think I've done okay translating the C into LUA:
Code: Select all
function ConvertGraphicsToGridXY(x,y)
-- Converts a screen co-ordinate (origin top-left corner) into an isometric grid co-ordinate
local tilewidth = 32
local tileheight = 21
-- Work out active cell. This means the 'normal' left/right aligned grid
local cellx = x / tilewidth
local celly = y / tileheight
-- Work out mouse offset into cell. Will be used later to determine if x,y is in one of the corners of the cell
local offsetx = x / tilewidth
local offsety = y / tileheight
isox = cf.round(celly + (cellx),1) -- cf.round is just a rounding function from one of my personal libraries
isoy = cf.round(celly - cellx,1)
return isox,isoy
end
I have two problems:
a) is my code conversion actually correct (I think it is)
b) in the tutorial, he uses some sort of colour mask thingy to detect corner clicks. Anyone familiar with the subject might have seen this before:
Other languages can check which colour was clicked and thereby know what corner was clicked. Is there a similar way in lua/love2d?
Here is my tiled map for reference:
Finally, I'm happy to use some module or engine or something with this but things like STI and tileclick either don't do isometric or don't do forever expanding random maps (i.e you have to handcraft each map).
This is just day 2 on this project and I'm pretty chuffed with the progress already. With so many isometric dungeon-crawlers out there I'm hoping this is an easy problem to fix. Thanks.