LibGDX display text on screen with camera movement.

I implemented a loot/money system using text with LibGDX. It displays how much money you have in the HUD.

The problem: The rendering of the money value does not move smoothly with the camera. Basically, it retains it’s int position, and not it’s float position. This is bad because it follows the camera and player and supposed to be rendered according to where the player is in the world. This makes the money display kind of just shoot over constantly when the player hits a new tile.

The player moves on the x, y axis with float values, which is how the “money display” is set up (so the x and y positions are floats, not ints). However, it does not move smoothly…the money display wont move until the player gets to a certain int (not float). So if the player is moving from tile one to tile two, and it takes him 10 frames to do this smoothly, the money display will retain it’s original position at tile one, and when the player hits tile two, the money display will shoot over to the correct position at tile two, but disregard the movement between tile one and two.

I can upload a video of this after work if it doesn’t make sense.

But my main question is, should I even be using the draw text method in LibGDX for a game that moves around a lot? Or should I have images (which do move correctly with the camera and player). How do you guys display text, and if you use the drawing text method in LibGDX, how do you make it have smooth movement? (I already googled and looked on here but didn’t really find a nice solution).

Thanks.

Here is the code where the money display is rendered:


package ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.mygame.MyGame;

import gameobjects.GameObject;
import loaders.ImageLoader;

/**
 * Extend GameObject to use animation variables.
 * 
 * @author Fabulous Fellini
 *
 */
public class TextBasedUiParent extends GameObject {

	protected BitmapFont font;

	/**
	 * Constructor.
	 */
	public TextBasedUiParent() {
		// The true paramater signifies font is flipped.
		font = new BitmapFont(true);
		// Uncomment this to make text seem smoother.
		//font.getRegion().getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
		float scale = 0.1f;
		font.getData().setScale(scale, scale);
		font.setColor(Color.BLACK);
	}

	/**
	 * 
	 * @param SpriteBatch batch
	 * @param ImageLoader imageLoader
	 * @param MyGame      myGame
	 * @param String      text
	 * @param GameOjbect  player
	 * @param float       xOffset
	 * @param float       yOffset
	 */
	public void renderUi(
			SpriteBatch batch, 
			ImageLoader imageLoader, 
			MyGame myGame, 
			String text, 
			GameObject player, 
			float xOffset, 
			float yOffset
			) {
		font.draw(
				batch, 
				text, 
				player.getX() - xOffset, 
				player.getY() + yOffset
				);
	}
}

And here is where the UI is rendered: maybe this is the problem, or has to be:


public void renderUserInterface(SpriteBatch batch, ImageLoader imageLoader, MyGame myGame) {
		if (!CutScene.anyCutSceneIsInProgress) {
			healthUi.renderHealthUi(batch, imageLoader, myGame);
			GameObject player = PlayerController.getCurrentPlayer(myGame);
			lootUi.renderUi(batch, imageLoader, myGame, player.convertPlayerLootToString(), player, 11.0f, -6.0f);
			playerNameUi.renderUi(batch, imageLoader, myGame, player, 10.5f, 6.0f);
			selectedInventoryUi.renderSelectedInventoryUi(batch, imageLoader, myGame, player);
		}
	}

lootUi.renderUi(batch, imageLoader, myGame, player.convertPlayerLootToString(), player, 11.0f, -6.0f);

That line has to be the problem…11 and -6 should probably reflect the player.getX() - 11.0, and same with the y pos. Thanks guys, writing this out really helped. If anyone sees a different problem let me know, otherwise I think my solution may work.

Also, I tried the game on my girls’ computer as well as work computer and it runs flawlessly, so all my slow downs developing must be from my shitty old computer. But if I can get it to run on that it should run on anything, so that’s good.

I’m not familiar with libgdx but you should be using a different projection matrix for hud’s, one that is orthographic rather than a perspective. The perspective matrix gets used with a view matrix (camera) but your orthographic one doesn’t. It’s stationary and never moves.

Ok, it looks like I have some research to do. When I started this project, I attempted to make 2 screens on top of one another. So the game screen was below the UI screen. But I couldn’t get the screens to work very well, so I decided to just render everything on the main game screen (with the UI getting rendered over everything).

I will try your method after work and see if I can get it to work. Thanks@abcdef