Class CameraControlledGameScreen

All Implemented Interfaces:
com.badlogic.gdx.InputProcessor, com.badlogic.gdx.Screen, com.badlogic.gdx.utils.Disposable, HUD, WorldSize

public abstract class CameraControlledGameScreen extends GameScreenWithHUD
A CameraControlledGameScreen is a GameScreen with a camera that follows a dynamic target.

Features:

  • Camera follows a target with optional smooth movement (lerp)
  • Zoom in/out with additive or multiplicative (factor) adjustments
  • Zoom clamped so viewport never shows outside the world
  • Automatic viewport and projection handling
  • Utility methods for converting mouse coordinates to world coordinates
  • HUD support: fixed coordinate drawing independent of world zoom/movement
  • Constructor Details

    • CameraControlledGameScreen

      public CameraControlledGameScreen(int viewportWidth, int viewportHeight, float worldWidth, float worldHeight)
      Parameters:
      viewportWidth - Width of the viewport (visible area)
      viewportHeight - Height of the viewport (visible area)
      worldWidth - Total width of the level/world
      worldHeight - Total height of the level/world
  • Method Details

    • setCameraTarget

      public void setCameraTarget(float x, float y)
      Set the position the camera should follow.
      Parameters:
      x - X-coordinate in world units
      y - Y-coordinate in world units
    • setCameraTargetInstantly

      public void setCameraTargetInstantly(float x, float y)
      Immediately moves the camera to the specified target position (no smooth transition).
      Parameters:
      x - X-coordinate in world units
      y - Y-coordinate in world units
    • render

      public void render(float delta)
      Description copied from class: GameScreen
      Called once per frame to render the screen.

      This is the only abstract method — subclasses must implement it to define what happens every frame.

      Specified by:
      render in interface com.badlogic.gdx.Screen
      Specified by:
      render in class GameScreen
      Parameters:
      delta - the time in seconds since the last frame was rendered
    • setZoom

      public void setZoom(float zoom)
      Set the camera zoom, clamped so the viewport never shows outside the world.
    • zoomBy

      public void zoomBy(float amount)
      Adjust the camera zoom by a delta, clamped to min/max automatically.
    • zoomByFactor

      public void zoomByFactor(float factor)
      Zoom the camera by a factor.
      Parameters:
      factor - >1 zooms in, <1 zooms out
    • screenToWorld

      public com.badlogic.gdx.math.Vector2 screenToWorld(float screenX, float screenY)
      Converts screen coordinates to world coordinates.
      Parameters:
      screenX - screen X position
      screenY - screen Y position
      Returns:
      world coordinates as a Vector2
    • resize

      public void resize(int width, int height)
      Description copied from class: GameScreenWithHUD
      Updates the HUD viewport when the window is resized.
      Specified by:
      resize in interface com.badlogic.gdx.Screen
      Overrides:
      resize in class GameScreenWithHUD
      Parameters:
      width - the new window width in pixels
      height - the new window height in pixels
    • setCameraLerp

      public void setCameraLerp(float lerp)
      Set the camera lerp factor for smooth following.
      Parameters:
      lerp - cameraLerp in [0,1], 1 = no movement, 0 = instant
    • getCamera

      public com.badlogic.gdx.graphics.OrthographicCamera getCamera()
      Returns:
      the OrthographicCamera used by this screen
    • getViewport

      public com.badlogic.gdx.utils.viewport.FitViewport getViewport()
      Returns:
      the FitViewport used by this screen
    • getZoom

      public float getZoom()
      Returns:
      current zoom level
    • getMouseWorldPosition

      public com.badlogic.gdx.math.Vector2 getMouseWorldPosition()
      Convert current mouse position to world coordinates.
      Returns:
      a Vector2 representing the mouse position in world space
    • getMouseX

      public float getMouseX()
      Get the current mouse x position in the world
      Returns:
      x coordinate in world coordinates
    • getMouseY

      public float getMouseY()
      Get the current mouse y position in the world
      Returns:
      y coordinate in world coordinates
    • getWorldWidth

      public float getWorldWidth()
      Returns total world width
      Returns:
      the world width
    • getWorldHeight

      public float getWorldHeight()
      Returns total world height
      Returns:
      the world height
    • getCameraLerp

      public float getCameraLerp()
      Returns:
      current camera lerp factor
    • getViewportBounds

      public com.badlogic.gdx.math.Rectangle getViewportBounds()
      Returns the current visible bounds of the viewport as a rectangle in world coordinates.

      The rectangle's x and y correspond to the bottom-left corner of the visible area.

      Returns:
      a Rectangle representing the visible area in world coordinates
    • getViewportLeft

      public float getViewportLeft()
      Returns the X-coordinate of the left edge of the viewport in world units.
      Returns:
      left edge of the visible area (world coordinates)
    • getViewportRight

      public float getViewportRight()
      Returns the X-coordinate of the right edge of the viewport in world units.
      Returns:
      right edge of the visible area (world coordinates)
    • getViewportBottom

      public float getViewportBottom()
      Returns the Y-coordinate of the bottom edge of the viewport in world units.
      Returns:
      bottom edge of the visible area (world coordinates)
    • getViewportTop

      public float getViewportTop()
      Returns the Y-coordinate of the top edge of the viewport in world units.
      Returns:
      top edge of the visible area (world coordinates)
    • getViewportWidth

      public float getViewportWidth()
      Returns the width of the viewport in world units.
      Returns:
      visible width of the viewport (world coordinates)
    • getViewportHeight

      public float getViewportHeight()
      Returns the height of the viewport in world units.
      Returns:
      visible height of the viewport (world coordinates)
    • getWorldCamera

      public com.badlogic.gdx.graphics.Camera getWorldCamera()
      Description copied from interface: WorldSize
      Returns the combined matrix of the world camera.

      This is used by the HUD to switch back to world rendering.

      Returns:
      the world camera's combined matrix