Libgdx Orthographic Camera 初始位置

时间:2023-03-02
本文介绍了Libgdx Orthographic Camera 初始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我希望正确定位相机,但我得到以下结果:

I would like the camera to be positioned correctly but I am getting the result below:

似乎当我调整窗口大小时,地图没有正确渲染.为什么会这样?

It seems like when I resize the window, the map does not get rendered properly. Why does that happen?

代码:

public void render(float delta){
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camera.update();
    mapRenderer.setView(camera);
    mapRenderer.render(background);
    mapRenderer.render(foreground);
    shapeRenderer.setProjectionMatrix(camera.combined);

    //draw rectangles around walls
    for(MapObject object : tiledMap.getLayers().get("walls").getObjects()){
        if(object instanceof RectangleMapObject) {
            RectangleMapObject rectObject = (RectangleMapObject) object;
            Rectangle rect = rectObject.getRectangle();
            shapeRenderer.begin(ShapeType.Line);
            shapeRenderer.rect(rect.x, rect.y, rect.width, rect.height);
            shapeRenderer.end();
        }
    }
    //done drawing rectangles
}

@Override
public void resize(int width, int height) {
    camera.viewportWidth = width;
    camera.viewportHeight = height;
}

@Override
public void show(){
    //call the tile map here
    //I believe this is called first before render() is called
    tiledMap = new TmxMapLoader().load("data/mapComplete.tmx");
    mapRenderer = new OrthogonalTiledMapRenderer(tiledMap, 1f);

    //initiate shapeRenderer. Can remove later
    shapeRenderer = new ShapeRenderer();
    shapeRenderer.setColor(Color.RED);

    camera = new OrthographicCamera();
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

推荐答案

这应该使 camera 在游戏的 viewport 居中.

This should center the camera at the viewport of the game.

@Override
public void resize(int width, int height) {
    camera.viewportWidth = width;
    camera.viewportHeight = height;
    camera.position.set(width/2f, height/2f, 0); //by default camera position on (0,0,0)
}

这篇关于Libgdx Orthographic Camera 初始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:LibGDX FreeType 字体模糊 下一篇:libgdx- pixmap:我能以某种方式改变线条的宽度吗?

相关文章