java - Libgdx 设置正交相机

标签 java libgdx

我在将正交相机设置为屏幕左下角 (0,0) 时遇到问题

public GameScreen(Main game) {
    this.game = game;
    Width = 200;
    Height = 300;

    view = new ExtendViewport(Width,Height);
    camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    camera.setToOrtho(false,Width/2,Height/2);
    camera.position.set(Width,Height,0);
    camera.update();

    play.Player1();
    staple = new Stage();
    staple.addActor(play);
    staple.addActor(pile);
    Gdx.input.setInputProcessor(staple);
}

@Override
public void render(float delta) {
        Gdx.gl.glClearColor(0, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.graphics.getDeltaTime();
        game.getBatch().begin();
        game.getBatch().setProjectionMatrix(camera.combined);
        game.getBatch().end();
        staple.act();
        staple.draw();
}

@Override
public void resize(int width, int height) {
    view.update(width,height);
    view.setScreenPosition(width,height);

}

我已使用分配的宽度和高度值将视口(viewport)设置为扩展视口(viewport),但我正在努力将相机移至左下角 我的屏幕的一部分 (0,0),它可以聚焦于我的 Android 设备上的图像。

最佳答案

以下是如何使用相机和视口(viewport)的小示例:
首先,我们必须定义相机显示的世界有多大:

私有(private)静态最终 int WORLD_WIDTH = 300; 私有(private)静态最终 int WORLD_HEIGHT = 250;

我们的世界现在有 300 x 250 个单位(不是像素!)大。<​​br/> 重要的是要以单位而不是像素来思考!!

现在我们需要一个 OrthographicCamera、一个 Viewport 和一个 SpriteBatch

OrthographicCamera camera;
Viewport viewport;
SpriteBatch batch;

@Override
public void create () {
    camera = new OrthographicCamera(); // we create a OrthographicCamera
    viewport = new ExtendViewport(WORLD_WIDTH, WORLD_HEIGHT, camera); // we create a new Viewport with our camera and we will display our world 300 x 250 units
    batch = new SpriteBatch(); // we create a new SpriteBatch for draw our textures
}

在我们的渲染方法中,我们说批处理只是为了使用方法 setProjectionMatrix() 绘制我们在 Viewport 中看到的内容

@Override
public void render (float delta) {
    camera.update(); //update our camera every frame
    batch.setProjectionMatrix(camera.combined); //say the batch to only draw what we see in our camera

    batch.begin();
    batch.draw(texture, 0,0); //draw our texture on point 0,0 bottom left corner
    batch.end();
}

在调整大小方法中:

public void resize(int width, int height){
    viewport.update(width, height); //update the viewport to recalculate
}

要了解出现此问题的原因:
在您的代码中,您从未将相机设置为视口(viewport):view = new ExtendViewport(Width,Height);
因此您的视口(viewport)永远不会应用于批处理。

要在没有视口(viewport)的情况下以正确的方式渲染,您必须知道 OrhographicCamera 的位置位于中心。

因此,当您将相机设置为位置 0,0 和大小 50,50 时,您会在每个方向上看到从 -25 到 25 的世界;

要在没有视口(viewport)的情况下使用 OrthographicCamera:

public void create () {
    camera = new OrthographicCamera(WORLD_WIDTH, WORLD_HEIGHT); // we create a OrthographicCamera and we will display our world 300 x 250 units
    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); //we set position of camera, our world point 0,0 is now the bottom left corner in the camera
    batch = new SpriteBatch(); // we create a new SpriteBatch for draw our textures
    texture = new Texture("badlogic.jpg");
}

public void render () {
    camera.update();
    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    batch.draw(texture, 0,0);
    batch.end();
}

重点在于调整大小方法:

public void resize(int width, int height){
    camera.viewportWidth = WORLD_WIDTH;
    camera.viewportHeight = WORLD_HEIGHT * height / width;
    camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
}

通过此计算,您始终会看到一个宽度为 300 且宽度和高度的比率为 250 * 的世界。

正是这个计算为您完成了视口(viewport)。根据您使用的 Vieport(FitViewport、ScreenViewport、ExtendViewport),此计算会有所不同,请尝试一下。

我希望这可以帮助您了解相机、视口(viewport)和 Spritebatch 如何协同工作。

以下是 libgdx wiki 的有用链接,其中描述了视口(viewport)和相机:
https://github.com/libgdx/libgdx/wiki/Viewports
https://github.com/libgdx/libgdx/wiki/Orthographic-camera

关于java - Libgdx 设置正交相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52011592/

相关文章:

java - libgdx 中两个边界框之间的碰撞检测

java - 有没有办法在java小程序中获取HttpOnly cookies数据?

java - 在 viewpager 中复制时,textview 会复制下一个或上一个文本

java - 从 map 中获取数组并转换键

java - 将 LibGDX 与 Android 首选项一起使用

java - libgdx scene2d 返回错误的标签高度

java - 空白图像(帧缓冲区)

java - 一种使用字符串创建新类的方法

java - 如何知道一个国家/地区是否存在夏令时?

java - 如何包含 java src 代码,以便我可以在 Android Studio 中使用它