0
\$\begingroup\$

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);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Touch coordinates have the origin(0,0) at the top-left, screen coordinates have it at the bottom-left. Bottom-left is the normal origin in Maths / OpenGL, top-left is how android reports it's screen touches. There's not really a good reason as to "Why", it's just how things were originally coded by the Android/Libgdx devs.

OrthographicCamera has a few methods with a yDown boolean parameter which lets you flip it, but since you're using a Viewport that won't work.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .