Introduction to OpenGL

Part 9: Adding a moving camera

Using a moving camera in physics simulations or games enhances user experience by providing dynamic perspectives, emphasizing key elements, and creating immersion. It offers flexible viewing angles and makes applications feel more realistic. In virtual or augmented reality, a moving camera with real-time visual effects greatly improves application quality and user engagement.

Which computer programs need a moving camera

Many computer programs rely on moving cameras to provide dynamic perspectives, enhance user interaction, or simulate real-world scenarios. Here’s a breakdown of programs and software categories that often require moving cameras:

  1. Video Games
    • Third-person action/adventure games (e.g., The Legend of Zelda, Assassin’s Creed).
    • First-person shooters (e.g., Call of Duty, Halo).
    • Racing games (e.g., Forza Horizon, Gran Turismo).
    • Virtual/Augmented reality (VR/AR) applications (e.g., Half-Life: Alyx, Beat Saber).
  2. Simulation and Training Software
    • Flight Simulators (e.g., Microsoft Flight Simulator, X-Plane): Moving cameras provide cockpit views and external perspectives.
    • Driving Simulators (e.g., City Car Driving, Assetto Corsa): Cameras track vehicle movement and surroundings.
    • Medical Simulators: Moving cameras simulate surgical procedures or patient interactions.

In summary, moving cameras are essential in programs that require dynamic perspectives, real-time tracking, or immersive experiences. These include game engines, 3D animation tools, simulation software, video editing programs, and many others across industries.

Some common moving camera techniques

Depending on the application and its custom needs several techniques have been developed. Here are some of them:

  1. First-Person Camera: The camera is positioned from the player's perspective, simulating what the character sees. This technique is common in first-person shooters and immersive exploration games.
  2. Third Person Camera: The camera is placed behind and slightly above the player's character, providing a broader view of the surroundings. This technique is popular in action-adventure games.
  3. Follow Camera: The camera follows the player's character, maintaining a constant distance and angle. This ensures that the character is always in view, regardless of their movement.

How can we implement these camera techniques in games?

Implementing these camera techniques in games involves a combination of programming, design, and careful tuning to ensure they enhance the gameplay experience. Here’s a general overview of how developers can implement each technique:

  1. First-Person Camera
    • Setup: Position the camera at the character's eye level.
    • Controls: Link camera rotation to the player's mouse or right analog stick for looking around.
    • Code: Ensure the camera's position updates based on the character’s movement.
    • Tips: Consider adding a field-of-view (FOV) slider for player comfort.
  2. Third-Person Camera
    • Setup: Position the camera behind and slightly above the character.
    • Controls: Use an orbit control system to allow the player to rotate the camera around the character.
    • Code: Implement collision detection to prevent the camera from clipping through objects.
    • Tips: Adjust camera distance and angles dynamically based on the environment.
  3. Follow Camera
    • Setup: Attach the camera to the character with a fixed offset.
    • Controls: Allow minor adjustments to the camera angle for player control.
    • Code: Smoothly interpolate the camera's position and rotation to follow the character.
    • Tips: Use damping to avoid sudden camera movements.

Hands on approach

This tutorial covers two types of moving cameras. First, we will create a First-Person Camera, where the camera represents the user's eyes. Second, we will create a Third Person Camera that follows the action from a distance.

We developed a car simulation application to demonstrate these concepts. You can drive the car in a virtual city, switch between the driver’s view and the following camera, and observe the differences between the two camera types.

The basics

In our examples, we use a viewport and camera to handle perspective, as detailed in Part 4 of this tutorial. For our moving camera, we will rely on these objects:

  • The `gl_viewport` class controls the viewing volume.
  • The `gl_camera` class holds the viewer's position and orientation.

The viewer controls the camera's movement and tilt. Position is determined by the `vLocation` variable, and orientation by the view direction `vForward` and the up-pointing vector `vUp`. Our camera already supports all the necessary functionality for the moving camera concept

First-person camera

For the first-person camera, the camera is positioned at the center of the car and moves together with the car. The car's velocity vector is normalized to determine the forward direction of the camera. The global ‘y’ axis represents the upward direction for the viewer/camera. As the position and orientation of the car are updated, the driver's point of view is also updated accordingly.

Third-person camera

This camera is programmable with various effects achievable from a third-person perspective, enhancing user engagement in the application.

Consider a camera mounted on a virtual drone that can initially be placed anywhere in the simulated environment. A point is selected at a specific distance behind the car, designated as the target position for the drone. This target point continuously updates as the car moves.

The drone, carrying the camera, flies to this updated target position while ensuring the camera always focuses on the car. This method ensures a smooth transition for the camera and perspective.

Summary

In this part we covered the need for a moving camera in our application and how we can create one.

Introduction to OpenGL