Skip to content

Camera controlled screen

⚠️ Experimental feature

The camera controlled screen 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.

⚠️ Advanced Topic

This section is intended for students who are comfortable with the basics of object-oriented programming. You should already know how to define classes, create objects and work with object inheritance.

A CameraControlledGameScreen is a special type of screen that automatically handles a camera which follows a target (usually a player or object in your game world). It builds on the basic ScalableGameScreen functionalities.

Some games have worlds that are larger than the screen size. Examples include:

  • Platformers like Mario where the camera follows the player horizontally.
  • Top-down adventure games like Zelda, where the camera follows the player through a map.
  • Shoot ’em ups or RTS games, where the player moves across a large battlefield.

In these games, the camera should move smoothly to follow the player while keeping them in view. You also often want to zoom in or out to see more or less of the world.

CameraControlledGameScreen handles camera movement, smooth following, viewport scaling, and zooming automatically. Students can focus on moving objects, creating levels, and rendering, without worrying about the complex camera math.

Features:

  • Camera automatically follows a target with optional smooth movement.
  • Camera zoom in/out, both additive and multiplicative (factor).
  • Zoom is automatically clamped to prevent seeing outside the world.
  • Utilities for world coordinates, mouse position, and viewport edges.
  • Easy to implement scrolling levels and large worlds.
  • HUD (Heads-Up Display), to draw a layer over the world for static elements such as score, health, items, etc.

Creating a screen

To use a CameraControlledGameScreen, create a new class that inherits from it:

public class MyLevelScreen extends CameraControlledGameScreen {
    private float playerX, playerY;

    public MyLevelScreen() {
        // Define camera viewport (visible area) and world size
        // Example: viewport 16x9 is, world has size 100x50
        super(16, 9, 100, 50);
    }

    @Override
    public void show() {
        // Initialize your objects, e.g., player starting position
        playerX = 50;
        playerY = 25;

        // Start camera centered on the player
        setCameraTargetInstantly(playerX, playerY); 
    }

    @Override
    public void render(float delta) {
        // Handle your game logic and input

        ...

        // When you have moved the player, let the camera know (so it stays in the center of the screen).
        setCameraTarget(playerX, playerY);

        // ALWAYS CALL super.render(delta) AFTERWARDS!!!
        // This applies the camera settings to the shape renderer and sprite batch.
        super.render(delta);

        // Now render all your objects
        GameApp.startShapeRenderingFilled();
        GameApp.drawRectangle(playerX, playerY, 100, 100, "red-500");
        GameApp.endShapeRendering();
    }
}

World, Viewport, Camera, and Window

In CameraControlledGameScreen, it’s important to understand the relationship between world, viewport, camera, and the window. This helps you control what the player sees and how your game scales.

The image below gives you a visual representation of the relationship between world, viewport and camera.

Viewport

Figure 1. Visual representation of world, viewport and camera

World

The world is your complete game level or map.

  • Defined in world units.
  • Each object that you create (players, enemies, etc.) exist in world coordinates (0,0) → (worldWidth, worldHeight).

Example:

super(16, 9, 100, 50);

Here:

  • 100x50 = world size
  • World units are consistent and independent of screen resolution.

Viewport

The viewport is the portion of the world that the camera can see at one time.

  • Defined in world units, e.g., 16x9.
  • Ensures that your game maintains the same aspect ratio, even if the window is resized.
  • Scales the world to fit the window while keeping proportions.
  • The viewport does not move itself; it is the camera that moves within the world.

Camera

The camera defines what part of the world is visible inside the viewport.

  • The camera’s position is the center of the viewport in world coordinates.
  • Camera movement scrolls the visible area across the world.
  • Zooming changes how much of the world fits inside the viewport.

Example:

setCameraTarget(playerX, playerY);    // Camera follows the player
setZoom(1.5f);                        // Zoom out: viewport shows more world

Camera is clamped so the viewport never shows outside the world.

Window

The window is the physical screen area in which the game is displayed.

  • The window has a width and height in pixels.
  • The viewport maps world units to window pixels, scaling automatically.
  • When the window is resized, the viewport adjusts the scaling but keeps the world aspect ratio intact.
  • Example: A viewport of 16x9 may be drawn on a 1600x900 pixel window or a 1280x720 pixel window — the visible area in world units stays the same.

Summary

Concept What it defines Units
World Full level/map size world units
Viewport Visible rectangle of the world world units
Camera Center position and zoom of the visible area world units
Window Physical screen where the game is drawn pixels

The world is the content, the viewport is what can be seen, the camera decides which part of the viewport is currently visible, and the window is the physical area on the screen.

When you set up CameraControlledGameScreen, you define viewport size (visible area) and world size (total map). The window scales automatically to display the viewport correctly.

Camera target

The camera will follow a target position. Use:

// Smoothly follow the target
setCameraTarget(playerX, playerY);

// Instantly move the camera to the target (no smooth movement)
// Useful for setting the starting position of the player in the show() method
setCameraTargetInstantly(playerX, playerY);

Camera lerp

Lerping is short for linear interpolation. When enabled, the camera smoothly moves toward the target instead of snapping instantly.

Example: if the player moves suddenly, the camera will "catch up" gradually, creating a smooth scrolling effect.

setCameraLerp(0.1f); // Smooth and responsive
setCameraLerp(0f);   // Camera snaps instantly
setCameraLerp(1f);   // Camera moves very slowly (almost stuck)

The default cameraLerp is 0.1f, which feels smooth but responsive.

Camera Zoom

The camera can zoom in and out, allowing you to see more or less of the game world. Zooming affects how the camera looks at the world.

  • Zoom = 1 is the default. It means: 1 world unit = 1 unit visible in the viewport.
  • Zoom < 1 is zooming in. Everything looks bigger because the viewport shows a smaller portion of the world.
  • Zoom > 1 → Zooming out. Everything looks smaller because the viewport shows more of the world.

Zoom is clamped automatically, so the camera never shows outside the world.

Saxion Game App support both additive and multiplicative zoom. Additive zoom increases/decreases zoom by a fixed value, while factor zoom multiplies the current zoom.

Usually in gaming you want to use multiplicative zoom, which feels more natural for pinch-to-zoom and smooth scaling.

Get current zoom level

float currentZoom = getZoom();

Setting the zoom directly

You can directly set the camera zoom using setZoom(float zoom):

setZoom(0.5f); // Zoom in: everything looks bigger
setZoom(2f);   // Zoom out: everything looks smaller

This is useful when you want precise control over the zoom level.

Additive zoom

Changes zoom by a fixed amount:

zoomBy(-0.1f); // Zoom in (everything looks bigger)
zoomBy(0.1f);  // Zoom out (everything looks smaller)
  • Use this when you want precise small adjustments to the zoom level.
  • Zoom is automatically clamped so the camera never shows outside the world.

Multiplicative zoom (zooming by percentage)

This changes zoom proportionally (multiplicative). We have to pass a zoom percentage to the method:

  • 1.1f means: zoom out by 10%
  • 0.9f means: zoom in by 10%;
zoomByFactor(1.1f); // Zoom out: visible area gets larger
zoomByFactor(0.9f); // Zoom in: visible area gets smaller

World Dimensions

The world can be larger than the viewport. Access world size:

float worldWidth = getWorldWidth();
float worldHeight = getWorldHeight();

Visible area of the camera:

float viewportWidth = getViewportWidth();
float viewportHeight = getViewportHeight();

Camera edges

Get the edges of the visible area in world coordinates:

float left   = getViewportLeft();
float right  = getViewportRight();
float bottom = getViewportBottom();
float top    = getViewportTop();

Useful for rendering only objects inside the viewport (culling) or limiting object positions.

Mouse position in world coordinates

This works still the same way as it worked in the ScalableGameScreen.

float mouseX = getMouseX();
float mouseY = getMouseY();

HUD (Heads-Up Display)

A HUD is a user interface layer that sits on top of your game world. It is used to display information that should not be affected by the camera, such as:

  • Score, lives, or health bars
  • Mini-maps
  • Buttons and menus
  • Timers or other game indicators

Unlike the game world, a HUD should always be drawn at a fixed position on the screen, independent of the player’s movement or camera zoom. There is HUD functionality available in the Saxion Game App in both ScalableGameScreen and CameraControlledGameScreen. This article describes all the Heads-Up Display functionality.