Heads-Up Display (HUD)
⚠️ Experimental feature
The HUD is new and experimental. It has not been thoroughly tested in all situations. Use it with caution. You might need to fix some issues with classes from LibGDX. Some behaviors may change in future updates.
In games, a Heads-Up Display (HUD) is the layer on top of the main game world where you display information to the player, such as:
- Score or points
- Health or energy bars
- Buttons or menus (see User Interface (UI) article)
The HUD lives on top of the game world, so it is not affected by the game camera. This makes it ideal for things drawn in pixels instead of world coordinates. It has its own coordinate system that you setup yourself.
Why the HUD is useful
When drawing text or UI elements, you often run into a problem that the game world uses world coordinates, but fonts, icons and buttons need to be drawn using screen pixels.
The HUD solves this by providing: - Its own camera and viewport for pixel-based drawing. - Methods to switch rendering between the world and the HUD. - Methods to get mouse position in HUD coordinates.
This makes drawing text and UI elements much easier and consistent, no matter the camera or screen size.
Adding the HUD to your screen
This functionality is only available in the ScalableGameScreen and the CameraControlledGameScreen.
We have to enable the HUD layer in the show() method of our screen:
public class MyGameScreen extends ScalableGameScreen {
public void show() {
// Enable the built-in HUD
enableHUD(1280, 720);
}
}
Switching between world and HUD rendering
By default your rendering method is rendering in the world (using world coordinates). But with the HUD enabled, we can switch to HUD rendering mode as well:
@Override
public void render(float delta) {
// This make sure all cameras and viewports are correct and we are drawing in world coordinates from the start
super.render(delta);
// Draw all your normal world objects here
GameApp.startSpriteRendering();
...
GameApp.endShapeRendering();
// Now switch to the HUD layer and start rendering in HUD coordinates
switchToHudRendering();
GameApp.startSpriteRendering();
GameApp.drawTextCentered("roboto", "Centered text", getHUDWidth() / 2, getHUDHeight() / 2, "red-500");
GameApp.endSpriteRendering();
// Optionally we can switch back to world rendering and render even more here
switchToWorldRendering();
...
}
switchToWorldRendering()ensures your world objects use the world camera.switchToHudRendering()ensures your HUD elements use the HUD camera, in pixel coordinates. This makes drawing text and UI consistent, even if the world camera moves.
At the start of the render(float delta) method, the mode is automatically switched to world rendering.
Getting HUD dimensions
You can get the width and height of the HUD to position elements:
float hudWidth = this.getHUDWidth();
float hudHeight = this.getHUDHeight();
GameApp.log("HUD size: " + hudWidth + " x " + hudHeight);
- Coordinates are in pixels, so (0, hudHeight) is the top-left of the screen.
- Useful for aligning text, bars, or buttons.
Getting mouse coordinates in HUD space
Use these methods to get the mouse position relative to the HUD:
float mouseX = this.getMouseHUDX();
float mouseY = this.getMouseHUDY();
GameApp.log("Mouse in HUD: X=" + mouseX + ", Y=" + mouseY);
- Automatically converts from screen coordinates to HUD coordinates.
Converting between world and HUD coordinates
Sometimes you need to translate positions between the world and the HUD, for example to place UI elements above game objects or detect clicks on the world through the HUD.
The HUD provides methods to convert full positions or individual X and Y coordinates.
Convert single coordinates
If you only need the X or Y coordinate, you can use:
// From World coordinate to HUD coordinate
float hudX = this.worldXToHUDX(5);
float hudY = this.worldYToHUDY(3);
// Or the other way around:
float worldX = this.hudXToWorldX(hudX);
float worldY = this.hudYToWorldY(hudY);
worldToHUDX(float worldX)returns the HUD X coordinate for a world position.worldToHUDY(float worldY)returns the HUD Y coordinate for a world position.hudToWorldX(float hudX)returns the world X coordinate for a HUD position.hudToWorldY(float hudY)returns the world Y coordinate for a HUD position.
These methods make it easy to align HUD elements with game objects, or translate mouse clicks in the HUD into world positions.
Convert a full position
// Create a point in world coordinates
Vector2 worldPos = new Vector2(5, 3);
// Convert to HUD position
Vector2 hudPos = this.worldToHUD(worldPos);
// Convert HUD position to world position
Vector2 originalWorldPos = this.hudToWorld(hudPos); // HUD -> World
worldToHUD(Vector2)converts a position in world coordinates to the HUD coordinate system.hudToWorld(Vector2)converts a position in HUD coordinates back to world coordinates.