I am trying to create a UI for a game that I’m making in LibGDX. However, when I use world units, the mouse input does not correspond correctly and is off depending on the screen size. How can I make sure that the mouse input is always the same as what we see on the screen?
public class MainMenuScreen implements Screen {
final ClientLauncher game;
private Texture playButtonInactive;
private Texture playButtonClicked;
private Texture playButtonActive;
// Camera and Viewport stuff
private OrthographicCamera camera;
private Viewport viewport;
Sprite theWorld;
// World unit
private static final float WORLD_WIDTH = 1920;
private static final float WORLD_HEIGHT = 1080;
float x = (float)ClientLauncher.width / 9;
float y = (float)ClientLauncher.height / 2;
if ((Gdx.input.getX() > x && Gdx.input.getX() < x + playButtonActive.getWidth()) && (Gdx.input.getY() < y - 100 && Gdx.input.getY() > y - 100 - playButtonActive.getHeight())){
game.batch.draw(playButtonInactive,x,y + 100);
if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
game.batch.draw(playButtonClicked,x,y + 100);
game.setScreen(new GameScreen(game));
}
}
else{
game.batch.draw(playButtonActive,x,y + 100);
}
the ClientLauncher has a width and height of 1920 and 1080 so it’s the same as my world units.
Thank you!
This is only a quick demo so I am hard coding the values but I’m trying to understand the structure of world units and how to use them when drawing and have everything in sync.
Mehrbod Mehrabi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.