【问题标题】:libgdx batch draws outside of FitViewportlibgdx 批处理在 FitViewport 之外绘制
【发布时间】:2016-03-02 17:10:14
【问题描述】:

我已经尝试解决这个问题 3 周了。我开始使用 libgdx wiki 的视口,并浏览了这个论坛中关于视口、虚拟视口等的许多问题,并发现我的游戏的最佳解决方案是拥有 2 个视口:1 Fitviewport 用于我的游戏渲染(不使用舞台),以及我的 UI 的另一个 Screenviewport(使用舞台)。 UI 绘制得很完美,尽管我认为涉及 DPI 的问题有一个特定的问题,因为在三星 S4 上控件变得非常小并且它们几乎无法使用,但我会就此提出一个单独的问题。 我的问题是游戏渲染和 libgdx 上的 FitViewport。 我在实现 Screen 的 PlayScreen 构造函数上以这种方式设置游戏摄像机及其视口:

gameCamera = new OrthographicCamera();
gameCamera.setToOrtho(false);
gameViewport = new FitViewport(800, 480,gameCamera);

然后在渲染方法上我这样应用它:

input(Gdx.graphics.getDeltaTime());

update(Gdx.graphics.getDeltaTime());

Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

gameCamera.update();
uiCamera.update();
MyGame.batch.setProjectionMatrix(gameCamera.combined);
gameViewport.apply();
MyGame.batch.begin();   
//DRAW CODE AFTER THIS
MyGame.batch.end();

//apply stage ScreenViewport and draw:
game.stage.getViewport().apply();
game.stage.draw();

然后在调整大小的方法中,我只有这 2 行:

game.stage.getViewport().update(width,height,true);
gameViewport.update(width,height);
gameCamera.position.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,0);

绘图方法基于 Gdx.graphics.getHeight() 和 Gdx.graphics.getWidth() 坐标。 我的游戏世界是一个自上而下的射击游戏,它使用 Gdx.graphics.getWidth() 和 getHeight() 来生成生物并限制玩家的移动。

我再次遇到的问题如下: - FitViewport 似乎无法正常工作。当我在我的设备上尝试它时,视口适合,创建排水沟线,但我看不到相对于 Gdx.graphics 坐标绘制的玩家。 - 当我在具有奇怪分辨率的台式机上进行试驾时,我可以看到子弹在视口外被绘制到排水沟线中。我不知道为什么会这样。

我相信我的问题可能是我应该相对于不是 Gdx.graphics 尺寸的其他坐标创建我的世界,但我似乎无法找到可能的解决方案。 但我真正不明白的是为什么我会在排水沟线上看到子弹。

【问题讨论】:

    标签: java libgdx viewport spritebatch


    【解决方案1】:

    我看到很多人都在为此苦苦挣扎,我必须承认我在这方面苦苦挣扎有点为时过早。但是,一旦您了解了基础知识,就很容易实现。

    让我们以FitViewport 为例,当你初始化这个new FitViewport(float worldWidth, float worldHeight, camera) 时,你正在指定世界坐标。这意味着无论如何,视口都将绘制世界的给定部分。

    • 如果你的世界更大,它不会吸引所有人
    • 如果您的视口在屏幕边界之外,您将看不到所有内容
    • 如果您的视口方面不等于它正在绘制的这些世界方面,它将被拉伸。

    我们还没有设置屏幕边界,所以让我们这样做吧:

    vp.setScreenBounds(Gdx.graphics.getWidth() / 2, 0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight());
    

    这里询问screenWidthscreenHeight 代表屏幕坐标。由于每个人都使用不同的屏幕,我们从图形本身获得它。您可以猜测它在哪里绘制视口;)。

    无论如何,这个视口new FitViewport(1920 / 2, 1080) 将始终在屏幕的右半边呈现我的世界的960 X 1080

    如果我的世界是 .9f x 1.6f 大,那么我可以像 new FitViewport(.9f, 1.6f, camera) 这样初始化 vp。

    PS: 在开始绘图之前不要忘记在render() 申请VP。

    【讨论】:

    • 好的,我明白了,但我正在阅读:void com.badlogic.gdx.utils.viewport.Viewport.setScreenBounds(int screenX, int screenY, int screenWidth, int screenHeight)。你给出的论点似乎与函数的描述不符。我不应该通过:(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight())?
    • 如果你想使用视口全屏,可以。我的示例将其放在右半部分。
    • 好的。我已经尝试过 gameViewport.setScreenBounds(0,0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight());和 gameViewport.setScreenBounds(Gdx.graphics.getWidth() / 2, 0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight());游戏后视口 = new FitViewport(800, 480,gameCamera);在 PlayScreen 的构造方法中,它根本没有改变行为。也许我错过了其他东西?顺便说一句,我确实在 render() 函数中的 batch.begin() 之前使用了 gameViewport.apply()
    • 你的世界是 800x480 并且你的相机在它的中间吗?
    • 我的世界是一个自上而下的射击游戏,它使用 Gdx.graphics.getWidth() 和 getHeight() 来生成生物并限制玩家的移动。相机在resize方法中居中:gameCamera.position.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2,0); .在问题中有一个错字,它是 cameCamera 而不是 gameCamera ,但这就是这里。也许我不清楚这个问题。问题是播放器出现在你看不到的位置,当我在桌面上测试它时它应该出现在 Gdx.graphics.getWidth() 和 Height() 尺寸内。
    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多