This stuff is taken directly from my game project and it should work for you and also explain some concepts.
First you need to setup your Camera and Viewport, the Scaling mode is important, read the LibGDX-Wiki and test some Scaling modes yourself to see the difference:
private void setupCamera() {
camera = new OrthographicCamera();
// ww and hw are your world width and height in world units (meters for example!)
// Read the LibGDX Wiki on viewports and try different types
viewport = new ScalingViewport(Scaling.fit, Metrics.ww , Metrics.hw, camera);
// [...]
}
Then you need to calculate your viewport size, do this in your games resize-method, it is called when needed by LibGDX, read the Wiki if you need to know more:
@Override
public void resize(int width, int height) {
// width and height of the window in pixels
Metrics.wv = width;
Metrics.hv = height;
// I want 1:1 texel to pixel mapping on screen for zoom=1.
// You can choose how much of your world you show here, you could also choose a constant width OR height in meters instead.
// world width and height in meters
// window_width_in_pixels * zoom / pixel_per_meter
Metrics.ww = Metrics.wv * camController.getZoom() / Metrics.ppm;
// window_height_in_pixels * zoom / pixel_per_meter
Metrics.hw = Metrics.hv * camController.getZoom() / Metrics.ppm;
// World viewport uses world coordinates (meters) so we pass ww (WidthWorld) and hw (HeightWorld).
viewport.setWorldSize(Metrics.ww , Metrics.hw);
// Always input your screen width and height
viewport.update(Metrics.wv, Metrics.hv, true);
// My stage on the other hand uses pixel coordinates so i pass wv (WidthView) and hv (HeightView).
stage.getViewport().setWorldSize(Metrics.wv, Metrics.hv);
// Always input your screen width and height
stage.getViewport().update(Metrics.wv, Metrics.hv, true);
}
Now the fun part, getting the position in world coordinates where the mouse pointer is:
// v get's overwritten and returned
public static Vector3 mousePositionInWorld(Vector3 v, Camera camera) {
v.set(Gdx.input.getX(), Gdx.input.getY(), 0f);
camera.unproject(v);
return v;
}
Getting the screen-position in pixels from a point in your game world:
// v should contain the world position you want to convert
public static Vector3 worldToScreenPosition(Vector3 v, Camera camera) {
camera.project(v);
return v;
}
The most important step is to always update your viewport! This works irrespective of screen size, fullscreen or not.