0

In my game, I need to get the hexagon that is underneath the mouse. This worked fine before I added a camera system to allow me to have bigger hexagon grids that go off-screen. If I move the camera, it starts acting weird and not getting the correct hexagon.

I've tried subtracting the camera position (defined as cam.x and cam.y) from the mouse position, subtracting mouse position from cam position. Obviously, I have little idea of what I'm supposed to actually do in this situation.

You can see the relevant code below.

        if button==3 then
            local hovering=hexGrid:containingHex(x,y) -- get hexagon at mouse position onclick
            if hovering then
                local data=hexes.getHexagon(hovering)
                data["text"]=data["text"]=="1" and "2" or "1" 
            end             
        end
Ducktor
  • 337
  • 1
  • 9
  • 27

1 Answers1

0

Turns out that the camera module I'm using - gamera - has two functions called 'toScreen' and 'toWorld', where the latter fixed my problem.

I get the mouse position (this code is inside love.update) and then convert it to world coordinates using toWorld.

local mx,my=love.mouse.getPosition()
local worldMx,worldMy = cam:toWorld(mx,my) -- convert mouse positions to their world coordinates
Ducktor
  • 337
  • 1
  • 9
  • 27