Libgdx set ortho camera

旧街凉风 提交于 2019-12-24 11:25:19

问题


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

}

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.


回答1:


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; private static final int WORLD_HEIGHT = 250;

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

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
}

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

And in the resize method:

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

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.

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

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;

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

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

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.

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

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




回答2:


Use camera.position.set(Width, Height, 1); instead of 0




回答3:


First you set the Camara width and height equal to the amount of pixels of the window.

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

Then you center it at 100, 150. And move it again to 200, 300

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

You are also using a Viewport but never make use of it.

I would recommend just using a Viewport of choice. A Viewport can take a camera so if you insist using your own camera you can create it but then also pass it to the Viewport when you construct it.

EDIT

Following is a tested minimal example.

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


来源:https://stackoverflow.com/questions/52011592/libgdx-set-ortho-camera

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!