libgdx设置正交相机

时间:2018-08-24 20:54:16

标签: 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);

}

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

3 个答案:

答案 0 :(得分:1)

这是一个如何使用相机和视口的小例子:
首先,我们必须定义相机显示的世界有多大:

private static final int WORLD_WIDTH = 300; 私人静态最终int WORLD_HEIGHT = 250;

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

现在我们需要一个OrthographicCamera,一个视口和一个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
}

在渲染方法中,我们说批处理仅是为了绘制ViewportsetProjectionMatrix()中可以看到的内容

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

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

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

因此,当您将“摄影机”设置为0,0且尺寸为50,50时,您会看到各个方向从-25到25的世界;

要在没有视口的情况下使用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 *宽高比的世界。

正是这种计算可以为您提供视口。根据您使用此计算的Vieport(FitViewport,ScreenViewport,ExtendViewport)的不同,请尝试一下。

我希望这可以帮助您了解摄影机,视口和Spritebatch如何一起工作。

以下是描述视口和相机的libgdx Wiki的有用链接:
https://github.com/libgdx/libgdx/wiki/Viewports
https://github.com/libgdx/libgdx/wiki/Orthographic-camera

答案 1 :(得分:0)

首先,将Camara的宽度和高度设置为等于窗口像素的数量。

camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

然后将其居中于100、150。然后再次将其移至200、300

camera.setToOrtho(false,Width/2,Height/2);
camera.position.set(Width,Height,0);

您还使用了Viewport,但从未使用过它。

我建议仅使用选择的ViewportViewport可以拍摄相机,因此如果您坚持使用自己的相机,则可以创建它,但是在构造它时也可以将其传递到视口。

编辑

以下是经过测试的最小示例。

public class TestScreen extends ScreenAdapter {

    private Viewport viewport;
    private ShapeRenderer sr;

    public TestScreen() {
        // Note that extend viewport extends it's camera so you end up with smaller or larger view of your world depending on the aspect ratio of the physical screen.
        viewport = new ExtendViewport(200, 300);
        viewport.apply();

        System.out.println(viewport.getWorldWidth());

        // Just for testing in the resize method.
        viewport.getCamera().translate(0, 0, 0);
        viewport.getCamera().update();

        // ShapeRenderer for testing
        sr = new ShapeRenderer();
        sr.setAutoShapeType(true);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(.04f, .06f, .1f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Draw circle at 0.0 in the world
        sr.setProjectionMatrix(viewport.getCamera().combined);
        sr.begin(ShapeRenderer.ShapeType.Line);
        sr.circle(0, 0, 100);
        sr.end();
    }

    @Override
    public void resize(int width, int height) {

        // Will center the camera in the world at half it's width and half it's height so left bottom is 0.0 as long as the camera did.
        // By using true here you cannot move the camera since you order it to center on the screen and thus the circle we are drawing
        // remains in the bottom left.
        //viewport.update(width, height, true);

        // This will just update the viewport, we moved the camera slightly to the left so the circle appears slight right from the middle.
        viewport.update(width, height, false);

        // So you want to start your camera centered on something but still want to move it you need to specify that center in the camera
        // by either changing it's position or translating it like I did in the constructor. Unfortunately you only get to know the size
        // of the world that is being displayed once this resize method did it's job so certain parts might get cut off or your world does
        // not fill the screen.
    }
}

答案 2 :(得分:0)

使用camera.position.set(Width, Height, 1);代替0