【问题标题】:Why camera in stage is not required to be centered in libgdx为什么舞台上的相机不需要在libgdx中居中
【发布时间】:2019-01-15 05:41:00
【问题描述】:

如果相机的 (0,0) 默认位于舞台的 (0,0),舞台相机如何能够看到完整的舞台视图。如果没有调用视口的更新方法,也没有调用相机位置设置方法。

【问题讨论】:

    标签: libgdx game-physics physics game-development


    【解决方案1】:

    如果您查看 Stage Constructor:

    public Stage (Viewport viewport, Batch batch) {
        if (viewport == null) throw new IllegalArgumentException("viewport cannot be null.");
        if (batch == null) throw new IllegalArgumentException("batch cannot be null.");
        this.viewport = viewport;
        this.batch = batch;
    
        root = new Group();
        root.setStage(this);
    
        viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
    }
    

    我们在最后一行看到 viewport.update() 以宽度、高度和 true 作为参数。 我们来看看这个 viewport.update() 方法:

    public void update (int screenWidth, int screenHeight, boolean centerCamera) {
        apply(centerCamera);
    }
    

    现在让我们看看 apply() 方法。我们知道 centerCamera 是真的:

    public void apply (boolean centerCamera) {
        HdpiUtils.glViewport(screenX, screenY, screenWidth, screenHeight);
        camera.viewportWidth = worldWidth;
        camera.viewportHeight = worldHeight;
        if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);
        camera.update();
    }
    

    我们在这里找到了答案:if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);

    舞台自行将摄像机位置居中。

    【讨论】:

    • Fitviewport 与相机视口是不同的还是它们是相同的术语。
    • 相机在 Fitviewport 中。 update()apply() 函数在 Viewport 类中。这是 Viewport 类的代码:github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/…。相机有一个由 Viewport 类管理的视口。
    • Fitviewport宽高和相机视口宽高有什么区别。
    • 他们不一样。相机视口宽度和高度是运行程序时可以看到的宽度和高度。所以真正的视口宽度和高度。 FitViewport(以及所有其他视口)只是围绕相机的一个 Wrapper 类,用于处理不同屏幕尺寸的视口宽度和高度,因此您无需考虑它。 Viewport 包含真实屏幕 Size 1920x1080px 和您在 Constructor 中设置的 World size(Viewport 宽度和高度)并计算 Viewport 大小并将其应用于相机。
    • 你能通过一个真实世界的例子来解释一下区别吗?我仍然对相机和视口及其区别感到困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多