Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX]Do we really need OrthographicCamera?  (Read 2017 times)
0 Members and 1 Guest are viewing this topic.
Offline P0jahn

Senior Member


Projects: 3



« Posted 2014-03-13 13:57:13 »

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.
Online Grunnt

JGO Wizard


Medals: 69
Projects: 8
Exp: 5 years


Complex != complicated


« Reply #1 - Posted 2014-03-13 14:04:49 »

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

1  
batch.setProjectionMatrix(camera.combined);


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

Offline trollwarrior1
« Reply #2 - Posted 2014-03-13 14:29:10 »

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..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline P0jahn

Senior Member


Projects: 3



« Reply #3 - Posted 2014-03-13 15:18:54 »

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

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #4 - Posted 2014-03-13 15:19:45 »

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

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #5 - Posted 2014-03-13 16:35:46 »

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.

Why are all OpenGL tutorials written in Brainf**k?
Offline P0jahn

Senior Member


Projects: 3



« Reply #6 - Posted 2014-03-13 16:36:53 »

Quote
You might need to know deeper linear algebra to make a rotating, moving 2D Camera.

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.
Offline The Lion King
« Reply #7 - Posted 2014-03-13 17:02:53 »

Quote
You might need to know deeper linear algebra to make a rotating, moving 2D Camera.

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.
This makes no sense.

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

"You have to want it more than you want to breath, then you will be successful"
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #8 - Posted 2014-03-13 17:12:39 »

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

Why are all OpenGL tutorials written in Brainf**k?
Offline P0jahn

Senior Member


Projects: 3



« Reply #9 - Posted 2014-03-13 18:04:20 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
   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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline The Lion King
« Reply #10 - Posted 2014-03-13 18:04:52 »

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

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.

"You have to want it more than you want to breath, then you will be successful"
Offline The Lion King
« Reply #11 - Posted 2014-03-13 18:06:51 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
   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?

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.

"You have to want it more than you want to breath, then you will be successful"
Offline P0jahn

Senior Member


Projects: 3



« Reply #12 - Posted 2014-03-13 18:14:34 »

I am confused now.

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



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

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #13 - Posted 2014-03-13 18:43:19 »

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:
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/Camera.java
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/Camera.java

It's true. The rotation and translation are simple matrix operations. But there is more than that Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #14 - Posted 2014-03-13 18:53:14 »

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

Why are all OpenGL tutorials written in Brainf**k?
Offline P0jahn

Senior Member


Projects: 3



« Reply #15 - Posted 2014-03-13 19:53:53 »

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 :O
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #16 - Posted 2014-03-13 19:58:05 »

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

Offline The Lion King
« Reply #17 - Posted 2014-03-13 19:58:20 »


no need to be fancy just abstract away this flip as so
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Entity extends Sprite
{
float x, y;
...
    public void move(float dx, float dy)
    {
             x += dx;
             y -=  dy;
    }  
}

"You have to want it more than you want to breath, then you will be successful"
Offline Phibedy

Senior Member


Medals: 8



« Reply #18 - Posted 2014-03-13 20:04:16 »

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

Senior Member


Projects: 3



« Reply #19 - Posted 2014-03-13 20:08:27 »

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?


no need to be fancy just abstract away this flip as so
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Entity extends Sprite
{
float x, y;
...
    public void move(float dx, float dy)
    {
             x += dx;
             y -=  dy;
    }  
}


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.
Offline The Lion King
« Reply #20 - Posted 2014-03-13 20:11:24 »

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

This is why OO design is so important...

Also a simper fix is just deal with the "backwards" y axis.

"You have to want it more than you want to breath, then you will be successful"
Offline pitbuller
« Reply #21 - Posted 2014-03-13 20:26:36 »

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

Just to note here that View matrix is inverse of camera transformation.
Offline Troubleshoots

JGO Knight


Medals: 36
Exp: 7-9 months


Damn maths.


« Reply #22 - Posted 2014-03-13 20:45:28 »

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

Just to note here that View matrix is inverse of camera transformation.

And to explain why, it's because when you're transforming the camera, you're actually transforming the vertices of all the meshes in the "world" (in space). The camera does not exist, it's simply an object that calculates and holds the matrices which describe the transformation of vertices. Matrices are used since they simplify the calculations that are needed to describe a transformation. When you want to translate "the camera" on the X axis +100px, you're actually wanting to transform all the vertices that are drawn in the world -100px.

Why are all OpenGL tutorials written in Brainf**k?
Offline pitbuller
« Reply #23 - Posted 2014-03-15 14:00:17 »

Camera is real object in same sense than anything else. You can use all the same code for calculating its transform than for rest of objects. But then you just calculate view matrix by taking the inverse of its transform matrix.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!