Page 1 of 2

Optimal way to track mouse-hover/touch over a tile position ?

Posted: Sat Oct 07, 2023 1:39 pm
by Athrikesha
Beginner qn.

i have a tilemap. And i use if loops to draw image/rectangle correspnding the table to render it on screen.

And currently i use a method that calculates the mouse's x-coordinate and dividing it by the individual tile width. The same process is repeated for the y-coordinate to determine the mouse hover on a specific tile.

Code: Select all

local tilemap = {
    {1, 2, 3},
    {4, 5, 6},
    -- Add more rows as needed
}

-- Tile dimensions and tilemap dimensions
local tileWidth = 64
local tileHeight = 64
local numRows = #tilemap
local numCols = #tilemap[1]

-- Variable to store the previously hovered tile
local prevHoveredTile = nil

function love.load()
    -- Set window dimensions
    love.window.setMode(numCols * tileWidth, numRows * tileHeight)
end

function love.update(dt)
    local mouseX, mouseY = love.mouse.getPosition()
    local tileX = math.floor(mouseX / tileWidth) + 1
    local tileY = math.floor(mouseY / tileHeight) + 1

    local newHoveredTile = tilemap[tileY] and tilemap[tileY][tileX]

    -- Check if the hover state has changed
    if newHoveredTile ~= prevHoveredTile then
        if newHoveredTile then
            print("Entered tile: " .. newHoveredTile)
        else
            print("Left the tile")
        end(
        prevHoveredTile = newHoveredTile
    end
end

function love.draw()
    -- Draw the tilemap
    for y = 1, numRows do
        for x = 1, numCols do
            love.graphics.rectangle("line", (x - 1) * tileWidth, (y - 1) * tileHeight, tileWidth, tileHeight)
            love.graphics.print(tilemap[y][x], x * tileWidth - 20, y * tileHeight - 20)
        end
    end
end
1 Is this how it should be done? (i am completely aware that there could be multiple valid implementations)

2 Also is this how major maps and player position on maps work? (hearsay, grid based games)

Gratitude to the supportive community

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Sat Oct 07, 2023 1:51 pm
by togFox
If it works - it works. :)

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Sat Oct 07, 2023 2:59 pm
by BrotSagtMist
^
Yea the check itself is fine.
But you can optimize how often it runs. Eg check if the mouse has actually moved before.

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Sun Oct 08, 2023 11:28 am
by dusoft
BrotSagtMist wrote: Sat Oct 07, 2023 2:59 pm ^
Yea the check itself is fine.
But you can optimize how often it runs. Eg check if the mouse has actually moved before.
That sounds like microoptimization to me. This basic hover check is not CPU intensive at all.

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Sun Oct 08, 2023 11:34 am
by BrotSagtMist
Whaaat?
How can you be so wasteful when the fix to pull it to almost zero cpu is just adding two lines?

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Mon Oct 09, 2023 1:20 pm
by dusoft
BrotSagtMist wrote: Sun Oct 08, 2023 11:34 am Whaaat?
How can you be so wasteful when the fix to pull it to almost zero cpu is just adding two lines?
It depends on what's going on in the background. If zero to nothing, it does not matter. If nothing to something and it's not CPU intensive, it does not matter. Etc. until you actually hit some performance (FPS) issues and then it will matter.

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Tue Oct 10, 2023 12:11 am
by BrotSagtMist
I does not depend.
Its two lines for less cpu usage. That is ALWAYS a profit.
Not adding it if you can is a harmful mindset.
You should be spanked.

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Tue Oct 10, 2023 10:23 am
by dusoft
BrotSagtMist wrote: Tue Oct 10, 2023 12:11 am I does not depend.
Its two lines for less cpu usage. That is ALWAYS a profit.
Not adding it if you can is a harmful mindset.
You should be spanked.
I still beg to differ and call it a microoptimization. Sorry, not into spanking.

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Tue Oct 10, 2023 11:01 am
by Bobble68
BrotSagtMist wrote: Sun Oct 08, 2023 11:34 am Whaaat?
How can you be so wasteful when the fix to pull it to almost zero cpu is just adding two lines?
Just out of curiousity, what would those two lines be?

Re: Optimal way to track mouse-hover/touch over a tile position ?

Posted: Tue Oct 10, 2023 12:09 pm
by togFox

Code: Select all

If dx == 0 and dy == 0 and executeFunction then
    spank()
end
3 lines? I'll say 2 lines.