libgdx设置正交相机 [英] Libgdx set ortho camera

查看:104
本文介绍了libgdx设置正交相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将正交摄影机设置到屏幕的左下角(0,0)

I'm having problems setting my orthographic camera to the bottom left part of my screen (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设备上的图像上.

I've set my viewport as extended viewport using my width and height values I have assigned but I'm struggling to move the camera to the bottom left part of my screen (0,0) where it can focus on my images on my android device.

推荐答案

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

Here are a little example how to use camera and viewport:
First we must define how big is our world the camera shows:

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

private static final int WORLD_WIDTH = 300; private static final int WORLD_HEIGHT = 250;

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

Our world is now 300 x 250 units (not Pixel!) big.
It's importent to think in units not in pixels!!

现在我们需要一个OrthographicCamera,一个视口和一个SpriteBatch

Now we need a OrthographicCamera, a Viewport and a 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()

In our render method we say the batch only to draw what we can see in our Viewport with the method setProjectionMatrix()

@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);
因此,您的视口永远不会应用于该批次.

To understand why you have this issue:
In your code you never set the camera to the viewport: view = new ExtendViewport(Width,Height);
So your viewport never apply to the batch.

要在不使用视口的情况下渲染正确的方式,您必须知道OrhographicCamera的位置在中心.

To render the correct way without Viewport you must know that the position of OrhographicCamera is in the center.

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

So when you set a Camera to position 0,0 and size 50,50 you see the world from -25 to 25 in each direction;

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

To use OrthographicCamera without Viewport:

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();
}

重点在于调整大小的方法:

The important point is in the resize method:

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 *宽高比的世界.

With this calculation you always see a World of 300 width and 250 * ratio of width, and height.

正是这种计算为您提供了视口.根据您使用此计算的哪个Vieport(FitViewport,ScreenViewport,ExtendViewport)而有所不同,请尝试一下.

And exactly this calculation does the viewport for you. Depending on which Vieport (FitViewport, ScreenViewport, ExtendViewport) you use this calculation will be different, try it out.

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

I hope this helps you to understand how camera, viewport and Spritebatch works together.

以下是指向libgdx Wiki的有用链接,该Wiki描述了视口和摄影机:
https://github.com/libgdx/libgdx/wiki/Viewports
https://github.com/libgdx/libgdx/wiki/Orthographic-camera

Here are useful links to the libgdx wiki which descript the Viewport and Camera:
https://github.com/libgdx/libgdx/wiki/Viewports
https://github.com/libgdx/libgdx/wiki/Orthographic-camera

这篇关于libgdx设置正交相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆