Properly aligning text to center

Hey guys,

I’ve implemented a basic pause for my exercise, but I’d like for the “Game Paused” text to appear in the middle. After trying to calculate the middle for any possible screen, I’ve discovered there are certain Align methods. However, manually doing it by diving Display.getWidth by 2 naturally won’t work as it only places the START of the text in the middle. I’ve tried using:




Neither of these properly work. The result is actually text that is about 100-200 ish pixels from the center towards the right side. Could it have to do with my bounds? Here is my code for formatting the text:

    //Setting fonts and texts
    guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    text = new BitmapText(guiFont, false);
    text.setBox(new Rectangle(0, 0, viewPort.getCamera().getWidth(), viewPort.getCamera().getHeight()));
    text.setLocalTranslation(new Vector3f(200, guiFont.getCharSet().getRenderedSize() + 100, 0));
    text.setText("x: "+x);

Thanks for any help :slight_smile: I appreciate it!

You make your text box the full size of the screen… and then position it somewhere up on the screen. That doesn’t really make sense. If you want to use the text box to center your text then align the text box with the screen, ie: set it like you are but then put the text at 0, 0, 0.

I’m sorry, I don’t understand very well. I’m creating the box, that starts at 0, 0 and ends at the end of my screen. Then “put text at 0, 0, 0”? So I have to set the local translation to 0, 0, 0 and then align it? I see. I thought align automatically did that. But apparently text.align aligns text to center starting from its position? So if it were already in the middle and I re aligned it to the middle, it would go to the middle of the middle? Sorry for the confusion and thank you xD

Text alignment aligns the text within the text box. So if you position the text box off the screen then the text won’t be centered on the screen.

I see. I thought building the text box rect at (0, 0, max, max) would position it.