I'm facing difficulty understanding the difference between Gdx.graphics.getWidth() vs cam.viewportwidth, What is the difference?
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.