[LibGDX]Do we really need OrthographicCamera?

Do we really need the cryptic OrthographicCamera?

Personally, I think it is easier to work with the Matrix4 class.

SpriteBatch have setMatrix(Matrix4), which should afaik be able to do everything OrthographicCamera can plus more.

Correct me if I am wrong.

Do you know what OrthographicCamera does? I find it quite easy to use:

batch.setProjectionMatrix(camera.combined);

Bacically OrthographicCamera as I see it is a wrapper for Matrix4 specifically suited for orthographic projection.

Hmm I have kinda small question kinda related to cameras and stuff.

Does opengl render everything in 3d by default?
What I mean is that everything seems to be rendered in 3d space, even though I would like it to be only 2d…

^^Leave the z value to 0 on all occurrences and you will get two dimensional effect.

@OP: I don’t know so well. I think OrthographicCamera is a nice abstraction over the Matrix4’s math stuff. You might need to know deeper linear algebra to make a rotating, moving 2D Camera.

Also, OrthographicCamera is not only a Matrix4. It’s two of them :wink: (the view and projection matrix)

The Camera classes are designed to try and hide all of the maths behind calculating the projection of vertices in space. They’re pretty much there for the Layman to use. I personally find it easier to use the Camera classes even though I understand the maths behind 3D projection transformation, simply because they do all the necessary calculations for you.

[quote] You might need to know deeper linear algebra to make a rotating, moving 2D Camera.
[/quote]
I am not so sure about that. Matrix4 have the method translate(x,y,z) which can be used to move the screen. We also have rotate and setRotation.

I am not so sure about that. Matrix4 have the method translate(x,y,z) which can be used to move the screen. We also have rotate and setRotation.
[/quote]
This makes no sense.

Its a perspective matrix. There is no translation or rotation.

I’m not sure what you mean. The Camera class holds a view and a projection matrix. The view matrix is the combined translation, rotation and scale transformation whereas the projection matrix is set to the orthographic projection matrix.

i.e the view matrix describes the camera orientation and the projection matrix describes the camera lens (field of view, zoom, etc.)

So I have been experimenting with the Ortho-something-Camera and I cant get the behavior I want.

For example, when I render a sprite at position 0,0, it should be rendered at the top left corner. But thats not the case here.
Furthermore, when I increase the y-variable, the entity moves up. It should move down.

Here is my code:

	float x, y;

	@Override
	public void create() 
	{	
		cam = new OrthographicCamera(800, 600); //This is the size of the window.
		batch = new SpriteBatch();
		
		texture = new Texture(Gdx.files.internal("data/libgdx.png"));

		sprite = new Sprite(new TextureRegion(texture));
		sprite.setOrigin(0,0);
		sprite.setPosition(-sprite.getWidth()/2,-sprite.getHeight()/2);
	}

	@Override
	public void render() 
	{	
		cam.position.set(0,0,0);
		cam.update();		
	
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		batch.setProjectionMatrix(cam.combined);
		batch.begin();
		
		batch.draw(sprite,x,y);
		
		batch.end();
	}

What am I doing wrong?

Pojhan claimed that you could make a moving 2D camera with just the translate and rotate functions. This is enough for the view matrix but the perspective matrix can not be done this way. That is all I meant.

in openGL Y+ is up, X+ is to the right, and Z+ is towards you. Right hand rule with your thumb as X and your index as Y.

I am confused now.

I thought the image below was the standard coordinate system for 2d games(at least in Slick2D):

http://s27.postimg.org/giwr45337/2d_graph.png

The top-left corner is x:0, y:0. Moving right increaes x and moving down increases y.

It’s more complicated.

The coordinate system is specified by the projection matrix.
In the case of 2D Orthographic projection this means, it tells:

Where is the origin located on the screen? (middle, topleft, or topright?)
In which direction do x (and especially) y values increase? (Y up / Y down, X up / X down?)
What is the ration between OpenGL coords and pixel values? (1:1? (standard: 800, 600 is top right edge on a 800, 600 window), Box2D: 1 : 32 pixel? (32 pixel are 1 Box2D Meter))

Especially zooming and similar are abstracted away by the OrthographicCamera, too (with that I mean zooming with the center being the pivot point).

Simply take a look at the code:


It’s true. The rotation and translation are simple matrix operations. But there is more than that :slight_smile:

If you want to set the coordinate system to Y-down then you can simply call setToOrtho(true) when you create the camera, or you can change the up vector to (0, -1, 0) and the direction vector to (0, 0, 1), which is pretty much what that method does (note that you have to call update to recalculate the matrices).

Well, that did not work. The X position is correct now but the Y is still way of. Plus, the image is up side down now :open_mouth:

Well what did you expect to happen when you flipped the drawing direction upside down?

no need to be fancy just abstract away this flip as so


public class Entity extends Sprite
{
float x, y;
...
    public void move(float dx, float dy)
    {
             x += dx;
             y -=  dy;
    }  
}


To have y pointing upwards: orthographicCamera.setToOrtho(false);
You have to flip the textureregion or use sprite if the texture is upside down.

Mistake in my post above. The positioning is now correct. draw(texture,0,0) will render the image at the top left corner.

So my last problem is that the entire thing is upside down. How do I resolve this?

I am converting a pretty big engine. Switching every upward and downward movements is going to be a nightmare.

Edit: Phibedy, flipping seems to work. But flipping every single image? That will also probably destroy my pixel perfect collision method.