I have a problem that can't resolve. I use a stage in my game with FitViewport style.
stage = new Stage(new FitViewport(SCREEN_WIDTH, SCREEN_HEIGHT));
But there's a problem when I'm trying get the touchdown coordinates while black bars appear on screen (if the screen has the right ratio there's no problem).
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)
{
xTouch = (int) (screenX * SCREEN_WIDTH/Gdx.graphics.getWidth());
yTouch = (int) ((SCREEN_HEIGHT) - screenY * (SCREEN_HEIGHT)/Gdx.graphics.getHeight());
}
The coordinates that I get with this method, give the position on screen, not on my game world. camera.unproject does the same thing. My question is, How can I ignore the black bars to get the position on my game world? I spent a lot of time searching but I did not find any useful solution.
Keep a Vector2 handy so you don't have to create new ones each time:
private Vector2 tmpVec2 = new Vector2();
Then use viewport's unproject method:
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
stage.getViewport().unproject(tmpVec2.set(screenX, screenY));
xTouch = tmpVec2.x;
yTouch = tmpVec2.y;
}