Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Viewport and camera in Libgdx?

Tags:

libgdx

I am new to LibGdx framework and having trouble working with viewport and camera. Can anyone give a simple difference between each and use of both?

like image 380
Rajesh Mainali Avatar asked Oct 15 '16 13:10

Rajesh Mainali


3 Answers

The camera defines the vantage point and how much of the world is seen on screen. Everything in your game is seen through a camera that defines a view of the world. In the case of 2D games, you use an OrthographicCamera that defines a rectangle of the world that is currently visible. OpenGL works with matrices to calculate what is seen, so to use the camera, you update it and take its combined matrix to pass into the SpriteBatch.

Viewport has three different meanings in LibGDX:

  1. The OpenGL viewport, the rectangular area of the screen OpenGL is currently set to draw to in pixel dimensions. No matter what dimensions an OrthographicCamera has, the viewable rectangle of the world that it defines is stretched to fit the OpenGL viewport, which means it can look distorted/squashed.

  2. The viewportWidth and viewportHeight of an OrthographicCamera . These terms are an unfortunate relic of LibGDX's infancy, when it was assumed you'd always be using a camera sized to equal the size of the OpenGL viewport. These terms actually describe the size of the rectangle of the world you can see, but this visible rectangle can have arbitrary dimensions and the final image is stretched to fit the OpenGL viewport.

  3. The Viewport class, which is a class that simultaneously manages an OrthographicCamera and the OpenGL Viewport to achieve a certain strategy for dealing with the infinite possibilities of screen densities and aspect ratios.

LibGDX Viewport classes are set up by providing a world width and world height, which define a rectangle of your game world that you want to be visible on screen. Depending on which type of Viewport you use, this rectangle will be used in some manner to target multiple sizes of screen.

Some examples of LibGDX's included viewports:

StretchViewport: It stretches your world width/height to fit the screen, even if this causes distortion. This should never be used for a released game because players will hate it. Only useful for very quick-and-dirty prototyping.

FitViewport: It enlarges your world width/height to fit the screen, and letterboxes the OpenGL viewport such that the image is not distorted. This is like the black bars you see when watching a movie on a TV. This is the quickest/dirtiest way to design your game since you know the viewable rectangle of your world is exactly the same on all devices. But your players might not like wasted screen space. If you use this for an iOS game, Apple will reject it.

ExtendViewport: It enlarges your world width/height to fit the screen, and then enlarges either your world width or height to fill any remaining space on the screen so the image won't be distorted. This is the best option if you don't want letterboxing, but it means you must design your game keeping in mind that you have to draw enough to cover the possible extra area outside your planned world width/height, and try to keep the gameplay reasonably fair.

ScreenViewport: This one is unique because it does not have an input for world width/height. It simply makes the world width and height match the screen size in pixels. This should never be used for game play because you could be seeing a rectangle five or more times as big on a large XHD device as compared to a small MD device. It has its uses for UI. You can design multiple UI assets for various screen densities and use ScreenViewport so text and buttons look very crisp on all devices.

I personally always use ExtendViewport for gameplay so screen space is not wasted and the game can be released on iOS.

For UI, I use ExtendViewport for the gameplay HUD (to push the widgets against the edges of the screen), and FitViewport if there is a pause screen that overlays the gameplay. For this use, FitViewport is fine because you won't actually see black bars. It's easier to manage a complicated pause screen UI if you know the rectangle it fits in will always be the same.

To keep UI looking reasonably crisp with minimal effort I generate it at a size that would be approximately pixel-to-pixel on a ~200-250dpi phone screen, and give it trilinear filtering.

like image 126
Tenfour04 Avatar answered Nov 04 '22 04:11

Tenfour04


Camera

In simple terms, the camera is like a real life camera. In LibGDX, Camera is used to show our game area.

For example: When making a movie, the director will do many preparations and everything will be capturing through the camera. In the same way in libgdx for our gameplay, we create many sprites, textures, etc.. and all these things are captured by a camera and rendered to the screen. in a simple way we can tell that the camera displays the game area.

Viewport

Viewport is mainly used to make our game visually compatible with multiple devices.

Different devices have different aspect ratios. When the game is ran on different devices, some graphical issues may occur (stretching the image, character out of screen, etc..). LibGDX provideds Viewport to solve this issue.

There are three main types (not limited to) of Viewport are available :

  1. StretchViewport
  2. FillViewport
  3. FitViewport

For more details, please check out the links below:

LibGDX - Viewports

LibGDX - Projection, Viewport & Camera

like image 45
Gokul Sreenivasan Avatar answered Nov 04 '22 02:11

Gokul Sreenivasan


The camera acts as an eye, it shows what it sees. The viewport just decides how will the thing observed by the camera be presented. For instance if you have a game that uses really small sprites and decided to put it on a big screen the image would be very small. That's where the viewport comes in. It's task is simply to "Widen" the image properly so you don't have to worry about different screen resolutions.

P.S.

  • I know that I'm a bit late with the answer and that it's probably oversimplified but I hope it will be helpful to a beginner. And I would like to make it clear that I'm no expert myself.
like image 29
Robert Baricevic-Petrus Avatar answered Nov 04 '22 04:11

Robert Baricevic-Petrus