How stage camera is able to see full stage view if (0,0) of the camera is located by default at (0,0) of stage. if update method for viewport is not called nor camera position set method is called.
Asked
Active
Viewed 258 times
1 Answers
3
If you look into the Stage Constructor:
public Stage (Viewport viewport, Batch batch) {
if (viewport == null) throw new IllegalArgumentException("viewport cannot be null.");
if (batch == null) throw new IllegalArgumentException("batch cannot be null.");
this.viewport = viewport;
this.batch = batch;
root = new Group();
root.setStage(this);
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
}
We see on the last line viewport.update() with the width, height and true as parameters. Let's look at this viewport.update() method:
public void update (int screenWidth, int screenHeight, boolean centerCamera) {
apply(centerCamera);
}
Now let's look on the apply() method. We know centerCamera is true:
public void apply (boolean centerCamera) {
HdpiUtils.glViewport(screenX, screenY, screenWidth, screenHeight);
camera.viewportWidth = worldWidth;
camera.viewportHeight = worldHeight;
if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);
camera.update();
}
And here we find the answer: if (centerCamera) camera.position.set(worldWidth / 2, worldHeight / 2, 0);
The stage centered the camera position on her own.

Morchul
- 1,987
- 1
- 7
- 21
-
Is Fitviewport different from camera viewport or they are same terms. – rsd123 Jan 15 '19 at 08:31
-
the camera is in the Fitviewport. The `update()` and `apply()` functions are in the Viewport class. Here is the Code of the Viewport class: https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/utils/viewport/Viewport.java. The camera has a viewport which is managed by the Viewport class. – Morchul Jan 15 '19 at 08:42
-
What is the difference between Fitviewport width and height and camera viewport width and height. – rsd123 Jan 15 '19 at 08:47
-
They are not the same. The Camera viewport width and height are the width and height what you can see if you run your program. So the real viewport width and height. FitViewport (And all other Viewports) are only a Wrapper class around a Camera to handle the viewport width and height for different Screen sizes so you don't need to think about it. The Viewport holds the real screen Size 1920x1080px and the World size you have set in Constructor (Viewport width and height) and calculate the Viewport size and apply it to the camera. – Morchul Jan 15 '19 at 08:54
-
Would you explain the difference by a real world example i am still bit confused between camera and viewport and their difference. – rsd123 Jan 15 '19 at 09:00
-
Imagine the camera is a speaker who speaks English. Different Screen Sizes are other People who speak German, French, Chinese etc. and the Viewport is the translator. The Translator doesn't change the sense of that what the English Speaker (Camera) says but he adapts it so the others can understand it. Same are camera and Viewport viewport doesn't say what you can see on your screen if you run the program he only handles that you always see the same on different Screen sizes. A Camera can life without Viewport. A Viewport not without Camera. – Morchul Jan 15 '19 at 09:13
-
Suppose i pass width as 500 and height as 500 in pixels to the Fitviewport constructor that means the camera will only be able to see 500 along y axis and 500 along x axis no matter what the screen size is the camera will only be able to see only 500 by 500 of world on whatever screen size it is. Am i right. – rsd123 Jan 15 '19 at 09:26
-
You must think in World units, not pixels. But yes, if you create a Viewport with width 500 and height 500 than you always will see 500x500 of your "World" doesn't matter on which screen. On smaller Screens the objects etc. will be smaller. – Morchul Jan 15 '19 at 09:29
-
finally i want to know is that why on resizing the window Fitviewport add black bars. would you explain by a example what if it didn't added black bars. – rsd123 Jan 15 '19 at 09:33
-
Yes exactly, because FitViewport matters about the ratios of x and y axis. If you take StretchViewport you won't have black bars but all Objects on your screen will be Stretch by resizing. – Morchul Jan 15 '19 at 09:37
-
I cant understand what you mean by ratio of x and y axis. would you give a simple. example of adding black bars. – rsd123 Jan 15 '19 at 09:40
-
I will create a new answer where I can describe it better than in a Comment. I will inform you when I'm done. – Morchul Jan 15 '19 at 09:45
-
Ok and thank you very much i was strugging for three days for understanding these and finally you helped me once again thank to you so so much i hope you will explain the concept of adding black bars when you created your answer i will be waiting for it and thank you once again... – rsd123 Jan 15 '19 at 09:50
-
I'm done: https://stackoverflow.com/questions/54198655/how-camera-works-in-libgdx-and-together-with-viewport/54198656#54198656. Hope this clears your questions. Maybe you have any suggestions to make my answer more understandable. – Morchul Jan 15 '19 at 12:17
-
Thank you man you have given crystal clear explanation i will be looking for future help is there a way so that i can directly ask question with you realated to libgdx instead of othe people answering. you directly answer to me. – rsd123 Jan 16 '19 at 01:09