2
\$\begingroup\$

I'm facing difficulty understanding the difference between Gdx.graphics.getWidth() vs cam.viewportwidth, What is the difference?

\$\endgroup\$
3
\$\begingroup\$

Gdx.graphics.getWidth() returns the width (in pixels) of the window (or on mobile, the screen) that your application is running in.

cam.viewportWidth is the width of the view of the camera, in whatever unit is applicable for you.

Let's say your game world is 100 meters wide, but for some reason you only want to show a portion of that at the time, if you set your cam.viewportWidth to 10, the you can see 1/10 of the world's width. The width that the camera is "seeing" is then mapped onto the screen/window, so on a 1024x768 pixel screen (assuming you're running full screen) those 10 units of camera viewport width is 1024 pixels wide on screen.

The Camera relates to where and how much of the game world you see, in world units. The Gdx.graphics.getWidth() is the physical pixels of the screen or window.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.