简体   繁体   English

libGDX在使用视口并移动相机时如何在屏幕的左上角绘制

[英]libGDX how do you draw on the top left corner of the screen when you are using a viewport and moving the camera

public OrthographicCamera camera;
Viewport viewport;

    camera = new OrthographicCamera();
    viewport = new ScalingViewport(Scaling.stretch, 1920, 1080, camera);
    viewport.apply();
    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
    camera.update();

then I move the camera with my mouse with this code 然后我用这个代码用鼠标移动相机

Vector3 v = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
    camera.unproject(v);
        camera.position.set(v);
        camera.update();

everything works fine except I have some text I want to draw on the top left corner of the screen. 一切正常,除了我想在屏幕的左上角绘制一些文本。 I use camera.combined and it draws it on the original screen but it doesn't follow where I move the camera. 我使用camera.combined并将其绘制在原始屏幕上,但它并没有跟随我移动相机的位置。 How do I accomplish this? 我该如何完成?

Excellent question, I created a method to return a Vector3 which is relative to the screen position. 很好的问题,我创建了一个方法来返回相对于屏幕位置的Vector3。 I'm not sure if there is a built in way to do this or easier way but here is my code below. 我不确定是否有内置的方法可以做到这一点或更简单的方法,但这是下面的代码。 Good luck on your program me. 祝您程序顺利,我。

public Vector3 relativeVector(float x, float y,OrthographicCamera c){
    return new Vector3(x+c.position.x-c.viewportWidth/2,y+c.position.y-c.viewportHeight/2,0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM