我在libgdx中的鼠标位置遇到了一些问题。我的鼠标位置在世界和真实位置之间是有区别的。要使我的鼠标位置我使用:
Vector3 mousePos = gamecam.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
speechBaloon.setPosition(mousePos.x, mousePos.y); //texture that follows cursor
其中:
public static final int WIDTH = 1920;
public static final int HEIGHT = 1080;
gamecam = new OrthographicCamera();
gamePort = new FitViewport(MyGame.WIDTH, MyGame.HEIGHT, gamecam);
gamecam.setToOrtho(false, MyGame.WIDTH, MyGame.HEIGHT);
在这里img1
img2-完整分辨率
您可以在 img2 中看到x位置之间存在差异,而在y之间,y之间存在差异。我不能发布超过2张照片,但是当我拖动光标时从 img1 的情况下到Y轴的中心,我的纹理和光标正在覆盖自己。
我具有调整大小函数(我的类实施屏幕(。
@Override
public void resize(int width, int height)
{
gamePort.update(width, height);
hudPort.update(width, height);
}
事先感谢您的帮助!
像tenfour04一样,只要使用viewport( gamePort
(,我们就需要在fiewport实例上调用.unproject((。
来自libgdx摄像机的文档:
功能将屏幕坐标中给出的点转换为世界 空间。... 假定视口跨越整个屏幕,并从 graphics.getWidth((和graphics.getheight((。
最终结果:
Vector3 mousePos = gamePort.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));
speechBaloon.setPosition(mousePos.x, mousePos.y); //texture that follows cursor