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

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.
Athrikesha
Prole
Posts: 7
Joined: Mon Sep 18, 2023 6:46 pm
Contact:

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

Post 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
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post by togFox »

If it works - it works. :)
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
User avatar
BrotSagtMist
Party member
Posts: 661
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 661
Joined: Fri Aug 06, 2021 10:30 pm

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

Post by BrotSagtMist »

Whaaat?
How can you be so wasteful when the fix to pull it to almost zero cpu is just adding two lines?
obey
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 661
Joined: Fri Aug 06, 2021 10:30 pm

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

Post 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.
obey
User avatar
dusoft
Party member
Posts: 676
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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.
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

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

Post 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?
Dragon
User avatar
togFox
Party member
Posts: 828
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

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

Post by togFox »

Code: Select all

If dx == 0 and dy == 0 and executeFunction then
    spank()
end
3 lines? I'll say 2 lines.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/idle-gridiron
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], glitchapp, Google [Bot] and 2 guests