1

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.

BittahDreamer
  • 113
  • 1
  • 10
  • I'm not entirely sure about what you mean by "bottom middle" I have some ideas on my mind but it'd be helpful if you could clarify about that with an image – Luis Fernando Frontanilla Aug 24 '18 at 21:43
  • Oh sorry for the confusion I meant to say at the bottom left of the screen (0,0). Though I was wondering is it possible to set the orthographic to different positions on the screen – BittahDreamer Aug 24 '18 at 22:29
  • I'm not sure if it's been mentioned in the other answers, but you never use your camera you made because you didn't pass it to your viewport. And then you also never use the viewport you made because you never pass it to your stage. I know you did use the camera projection matrix on the batch but you didn't draw anything with it and the stage changes it back to its own viewport's camera when you call stage.draw. – Tenfour04 Aug 26 '18 at 00:41

3 Answers3

3

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

Morchul
  • 1,987
  • 1
  • 7
  • 21
0

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.
    }
}
Madmenyo
  • 8,389
  • 7
  • 52
  • 99
  • Unfortunately, it still doesn't work, I tried messing around with the orthographic camera but nothing really happened – BittahDreamer Aug 25 '18 at 10:35
  • Thanks my viewport is fixed but I still can't set my orthographic camera to concentrate on the origin of the screen (0,0) – BittahDreamer Aug 27 '18 at 21:56
  • @BittahDreamer What do you exactly mean with concentrate on the origin of the screen? It makes no sense, a viewport is essentially a screen manager that shows your game world on screen. If you would center the viewport at screen pixel 0.0 you would only see a quarter of the viewport (top right part) if that was even possible. – Madmenyo Aug 27 '18 at 23:19
0

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

icarumbas
  • 1,777
  • 3
  • 17
  • 30