镜头位置导致鼠标位置偏移

在我的游戏中,我需要获取鼠标所在的六边形坐标。在我加入可以让我有更大的六边形网格的镜头系统之前,这个功能运行得很正常。但如果我移动镜头,它就开始表现得很奇怪,无法正确地获取六边形坐标。

我试过从鼠标坐标减去镜头坐标(定义为 cam.x 和 cam.y),或者从镜头坐标减去鼠标坐标。显然,我对如何应对这种情况一窍不通。

你可以在下面看到相关的代码:

        if button==3 then
            local hovering=hexGrid:containingHex(x,y) -- 获取在鼠标单击位置的六边形坐标
            if hovering then
                local data=hexes.getHexagon(hovering)
                data["text"]=data["text"]=="1" and "2" or "1"
            end
        end
点赞
用户6079712
用户6079712

原来我使用的相机模块 gamera 有两个函数叫做 'toScreen' 和 'toWorld',其中后者解决了我的问题。

我获取了鼠标的位置(这段代码在 love.update 中),然后使用 toWorld 将其转换为世界坐标。

local mx,my=love.mouse.getPosition()
local worldMx,worldMy = cam:toWorld(mx,my) -- 将鼠标位置转换为其世界坐标
2018-05-11 14:00:28