I had a similar issue to the question here: Why is my text is too large even when scaled to .05f in libGDX?
So I created a second camera for my text elements. I'm rendering the text above some other elements, and under others, so I was trying to map between the two camera's coordinates when rendering.
That seems to be working OK, but I need to invert the y
coordinate during the transformation. I'm wondering why that's the case?
Here's the code:
// Setup camera
OrthographicCamera camera = new OrthographicCamera(VIEWPORT_UNITS, VIEWPORT_UNITS * (height / width));
camera.position.set(camera.viewportWidth / 2f, camera.viewportHeight / 2f, 0);
// Setup text camera
FitViewport fitViewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
fitViewport.apply(true);
gameContext.setHudViewport(fitViewport);
OrthographicCamera textCamera = fitViewport.getCamera();
// ... later, during render(), convert between coordinates
Vector3 textPosition = new Vector3(fontX, fontY, 0);
camera.project(textPosition);
textPosition.y = Gdx.graphics.getHeight() - textPosition.y; // why?
textCamera.unproject(textPosition);
font.draw(spriteBatch, text, textPosition.x, textPosition.y);