【问题标题】:Libgdx Box2d multiple SpriteBatch and OrthographicCameraLibgdx Box2d 多个 SpriteBatch 和 OrthographicCamera
【发布时间】:2019-05-04 02:06:26
【问题描述】:

我想要两个 SpriteBatch 对象,一个用于实际精灵,一个用于 HUD。我不知道如何让一个 SpriteBatch 相对于屏幕保持不动,而是让另一个移动,以玩家的身体为中心。我有一个用于 box2d 身体的 OrthographicCamera 和一个用于精灵的。

我以为 setProjectionMatrix 方法可以解决这个问题,但我可能用错了。

In the main file:

public void render () {
        stateManager.getActiveState().update(Gdx.graphics.getDeltaTime());
        spriteBatch.setProjectionMatrix(camera.combined);
        spriteBatch.begin();
        stateManager.getActiveState().render(spriteBatch);
        spriteBatch.end();
        debugRenderer.render(world, b2dCamera.combined);
    }

stateManager.getActiveState().update(Gdx.graphics.getDeltaTime()); calls:

public void update(float dt) {
        this.player.update(dt);
        this.camera.position.set(this.player.getCenter().x * Game.getPpm(), this.player.getCenter().y * Game.getPpm(), 0);
        this.b2dCamera.position.set(this.player.getCenter().x, this.player.getCenter().y, 0);
        this.camera.update();
        this.b2dCamera.update();
        this.joystick.update();
    }

stateManager.getActiveState().render(spriteBatch); calls:

public void render(SpriteBatch spriteBatch) {
        this.playBatch.begin();
        System.out.println(playBatch.getProjectionMatrix());
        System.out.println(spriteBatch.getProjectionMatrix());
        font.draw(spriteBatch, "Hello", 0, 60);
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        this.player.render(playBatch);
        this.joystick.render(spriteBatch);
        this.world.step(Gdx.graphics.getDeltaTime(), 8, 3);
        this.playBatch.end();
    }

这给了我留在左下角的“你好”,一个保持在中心并旋转的身体(因为相机以身体的中心为中心,我的精灵(与身体相关联)移动到与身体同步旋转。

我希望精灵不要移动(在世界中移动,但相机应该像它与身体一样以它为中心)并且与身体完全同步。

当我打印 spriteBatch 的 ProjectionMatrix 时,每次迭代都是一样的,但是 playBatch 会移动

更新 如果我从主文件中注释掉 spriteBatch.setProjectionMatrix(camera.combined);,没有任何变化

这可能不是使用 hud 进行游戏的最佳方式,如果您有更好的选择,请告诉我。谢谢

【问题讨论】:

  • 您是否考虑过为 HUD 添加单独的相机/视口而不是使用两个批次?它应该更容易管理。

标签: java android libgdx box2d


【解决方案1】:

我找到了解决方案。我让两个 spriteBatches 的开头和结尾重叠,这会导致奇怪的事情发生。我现在正在调用 begin ... end of one and begin ... end of another。

【讨论】:

    猜你喜欢
    • 2013-02-06
    • 1970-01-01
    • 2016-12-12
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    相关资源
    最近更新 更多