LibGDX yDown and text problem

When drawing when BitmapFont and the y is facing down on the orthographic camera, like this:


//y is facing down
camera.setToOrtho(true, 800, 480);
//...
batch.begin();
font.draw(batch, "Example Text", 10, 30);
batch.end();

the text flips upside down.
Making the y face upwards fixes this problem:

camera.setToOrtho(false, 800, 480);

but if there comes a time when I want to have y facing down, I want to find a solution to this problem.

Why would you ever want to have the y facing down?

The “y isn’t facing down”, your origin is just in different corners. The “true” and “false” just means that you are switching between the top left, and bottom left corners of the screen for the origin. Drawing from the top down will require you to draw a different way than drawing from the bottom up. I would look into the camera.setOrtho method to learn more about the origin and what happens when you change that boolean. You should know about origins though, everything that has anything to do with rendering pixels on a monitor uses origins, so obviously they are pretty important.

When you render something, you can flip Y axis yourself.

float y = Gdx.graphics.getHeight() - 100;

(This means 100 pixels down from the top left corner)

I found another problem with this code. You’re square is rendered not starting top-left vertex, therefore you need to “render lower”. (Rendering is done at bottom-left corner)

float y = Gdx.graphics.getHeight() - (100 + squareHeight);

If you use Y axis facing down, you need to flip the texture of your BitmapFont too. You can achieve this by calling the proper constructor when creating your font.


	/** Creates a BitmapFont from a BMFont file. The image file name is read from the BMFont file and the image is loaded from the
	 * same directory.
	 * @param flip If true, the glyphs will be flipped for use with a perspective where 0,0 is the upper left corner. */
	public BitmapFont (FileHandle fontFile, boolean flip) {
		this(new BitmapFontData(fontFile, flip), (TextureRegion)null, true);
	}