Skip to content

Camera Movement

rjg1 edited this page Aug 24, 2022 · 1 revision

Introduction

In order to move the camera around the map in the typical RTS style (moving the mouse near a corner of the screen adjusts the camera in that direction) a Component is necessary to determine user input, and adjust the game camera accordingly. CameraInputComponent has been constructed to do this, by extending InputComponent and listening for a mouseMoved and scrolled event.

If the mouse has moved, the CameraInputComponent will check to see if the user's mouse is in an appropriate corner of the screen, and update the direction to move the camera if it is. Each update() call to CameraInputComponent will adjust the camera in the set direction.

If the mouse has scrolled, the camera's zoom will be adjusted at a rate proportional to the scroll amount, up to a limit defined in constant "maxZoom".

Usage

Creating a CameraInputComponent

To use the CameraInputComponent, it must simply be added to the same Entity as the game Camera. Currently, the camera is created in RenderFactory, by the function createCamera().

  public static Entity createCamera() {
    Entity camera = new Entity();
    camera.addComponent(new CameraComponent());
    //Add a CameraInputComponent to manage camera movement around map
    camera.addComponent(ServiceLocator.getInputService().getInputFactory().createForCamera());
    return camera;
  }

  public static Renderer createRenderer() {
    Entity camera = createCamera();
    ServiceLocator.getEntityService().register(camera);
    CameraComponent camComponent = camera.getComponent(CameraComponent.class);

    return new Renderer(camComponent);
  }

This will create the Camera entity, equipped with this component as required to process input.

Updating game max zoom

If it is required to update the max zoom distance, the constant MAX_ZOOM can simply be set:

    /**
     * The maximum distance the player may zoom out of the game
     */
    private final float maxZoom = 10;

For example will set the maximum zoom amount to 10 scroll "clicks"

Updating the distance required to trigger camera movement

If you wish to trigger camera movement by hovering closer - or further- from the screen extremities, adjusting this constant in CameraInputComponent will do so

    /**
     * Distance in pixels between the edge of the screen and a camera movement trigger
     */
    private final float buffer = 80;

Where a smaller number will trigger closer to an edge and vice versa

Updating camera movement speed

If the camera is scrolling too fast, or too slow, the speed is also adjustable by the following constants

    /**
     * Default scrolling speed of camera
     */
    private final float defaultSpeed = 0.3f;
    /**
     * Scrolling speed of camera diagonally
     */
    private final float fastSpeed = 0.4f;

Table of Contents

Home

Game

Game Home

Design Influences

Gameplay Features

Style

Story

Friendly Units
Map
City
Buildings
Unit Selections

Spell

Game User Testing: Theme of Unit Selection & Spell System

UI User Testing

Tutorial

Resource Stats Display

Loading Screen Bar

Health Bars
In Game menu
  • Feature
  • User Testing:In Game Menu

Landscape Tile Design

Landscape Tile Design Feedback

Weather Design

Weather Design Feedback

Camera Movement

Enemy design

Enemy Units

Enemy AI

How Animation Works

Map Flooding

Game Engine

Getting Started

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally