LibGdx:鼠标位置与所选磁贴(世界位置)不匹配



我创建了一个十六进制图块地图,我在组合两个功能时遇到了问题:1)通过用鼠标/手指拖动来滚动地图2) 在鼠标悬停位置上突出显示磁贴

我的世界包含一张地图,它由自定义 Tile() 对象组成。每个图块都保存了一个 x 和 y 坐标。在我的游戏类中,我使用正交相机和磁贴选择器,该类获取当前鼠标位置并在这些坐标下方突出显示相应的磁贴。此外,我的 render() 方法检查鼠标/手指是否向下,如果是,则每个图块的位置会根据手指/鼠标按钮向下移动的距离进行更新。

以下代码显示了相关部分。只要我不移动地图,突出显示就可以了。但是一旦我移动了瓷砖,突出显示就会开始关闭(通过移动的距离

)。
//initialiting orthographic camera
camera = new OrthographicCamera(800, 480);
camera.setToOrtho(false, 800, 480);
//draw screen black
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
//drawing to batch
game.batch.begin();
//drawing the tiles, simply getting x,y coords and putting on screen
world.render(game.batch); 
//highlighting tile under the current mouse/finger coords and drawing highlight on screen
selector.select(Gdx.input.getX(), Gdx.graphics.getHeight() - Gdx.input.getY(), game.batch, world);
game.batch.end();
//scrolling routine
if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);
        deltaX = (float)Gdx.input.getDeltaX();
        deltaY = (float)Gdx.input.getDeltaY();
        //moving each tile by the distance moved
        for(int i = 0; i< world.getMap().getWidth(); i++){
            for(int j = 0; j< world.getMap().getHeight(); j++){
                tile = world.getMap().getTile(i, j);
                tile.x += deltaX;
                tile.y -= deltaY;
                world.getMap().setTile(tile, i, j);
        }
    }
}

请参阅可视化屏幕截图:

1)地图未移动,鼠标(红色箭头)在磁贴上并突出显示https://i.stack.imgur.com/3d7At.png

2)地图已移动,但鼠标停留在旧位置,不应突出显示任何磁贴,但磁贴会突出显示,就好像地图没有移动一样https://i.stack.imgur.com/dSibc.png

现在我明白问题似乎是由于没有正确映射/更新屏幕和世界坐标而引起的。但是由于我对LibGdx/OpenGL/投影/翻译缺乏了解,我无法确定我的错误。那么这个渲染算法可以按原样修复吗?或者我应该从移动我的磁贴切换到移动我的相机(如何)?请让我知道,如果您需要更多代码,谢谢。

好吧,我能够自己找到解决方案,因为问题出在逻辑上,而不是在 LibGdx API 上。如果其他人将来遇到同样的问题,我将在这里提供解决方案。

起初,我按照天使的提示,开始移动相机而不是地图/世界。其次,主要问题是,我使用鼠标/手指的屏幕坐标来突出显示磁贴,而不是世界坐标。

新代码:

    //scrolling routine
    if (Gdx.input.isTouched()) {
        //get mouse/finger position in screen coordinates and save in a Vector3
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        //get the distance moved by mouse/finger since last frame and translate camera to that position
        //(I have no idea yet why the x-axis is flipped)
        deltaX = (float)Gdx.input.getDeltaX();
        deltaY = (float)Gdx.input.getDeltaY();
        camera.translate(-deltaX, deltaY, 0);
        //this is the solution, the coordinates that you use for tile highlighting
        //are translated to world coordinates
        touchPos = camera.unproject(touchPos);
    }
    //update the camera position and the math that is involved
    camera.update();
    game.batch.setProjectionMatrix(camera.combined);
    game.batch.begin();
    world.render(game.batch);
    //now the highlighting routine is working with world coordinates
    selector.select(touchPos.x, touchPos.y, game.batch, world);
    game.batch.end();

我希望这将在未来有所帮助。如果有人对我如何获得正确的磁贴进行突出显示感兴趣,我使用此线程中评分最高的答案中描述的方法:

边形网格,你如何找到一个点在哪个六边形中?

祝你好运,玩得开心!

最新更新