【问题标题】:Mouse position offset by camera position鼠标位置偏移相机位置
【发布时间】:2018-10-21 22:56:54
【问题描述】:

在我的游戏中,我需要得到鼠标下方的六边形。在我添加相机系统以允许我拥有更大的六边形网格之前,这很好用。如果我移动相机,它会开始表现得很奇怪并且没有得到正确的六边形。

我尝试从鼠标位置减去相机位置(定义为 cam.x 和 cam.y),从凸轮位置减去鼠标位置。显然,我不知道在这种情况下我应该做什么。

您可以在下面看到相关代码。

        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

【问题讨论】:

    标签: lua love2d hexagonal-tiles


    【解决方案1】:

    原来我正在使用的相机模块 - gamera - 有两个功能,称为“toScreen”和“toWorld”,后者解决了我的问题。

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

    local mx,my=love.mouse.getPosition()
    local worldMx,worldMy = cam:toWorld(mx,my) -- convert mouse positions to their world coordinates
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多