Libgdx正交摄影机初始位置

Libgdx正交摄影机初始位置

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

似乎当我调整窗口大小时, map 无法正确渲染。为什么会这样呢?

代码:

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的中心。

@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)
}

关于java - Libgdx正交摄影机初始位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21913894/

10-09 04:36