Reader small image

You're reading from  Mathematics for Game Programming and Computer Graphics

Product typeBook
Published inNov 2022
PublisherPackt
ISBN-139781801077330
Edition1st Edition
Tools
Right arrow
Author (1)
Penny de Byl
Penny de Byl
author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl

Right arrow

Navigating the View Space

One of the most common uses of transformations in computer games is the movement of the camera viewing the scene. While the camera can be considered as another 3D object in the environment with respect to its movement and orientation, its current transformation is also used as the view matrix by which all virtual objects in the world are affected.

The transformations presented in this chapter for moving and rotating the camera within its environment are the exact same operations that would be performed on any object in the environment.

In this chapter we will explore:

  • Flying maneuvers
  • Understanding and fixing compound rotation quirks
  • Improving camera orientations

By the end of this chapter, you will have a greater appreciation of the complexity of 3D rotations and the issues that arise from using them in mathematical operations to combine multiple orientations. The knowledge herein you will use over and over again as you work...

Technical requirements

In this chapter, we will be using the Python, PyCharm and Pygame as used in previous chapters.

Before you begin coding, create a new folder in the PyCharm project for the contents of this chapter called Chapter_15.

The solution files containing the code can be found on GitHub at https://github.com/PacktPublishing/Mathematics-for-Game-Programming-and-Computer-Graphics/tree/main/Chapter15.

Flying maneuvers

Any object in 3D space can have transformations applied to it. It’s no different for the camera. In this section, we will explore how the same mathematics can be applied to the object to move and reorient it.

In Chapter 12, Mastering Affine Transformations, we discussed the three rotations that can take place in 3D space; namely, pitching, yawing, and rolling. These correspond with rotations around the x, y, and z axes respectively.

The 4 x 4 matrix that will perform a pitch around the x axis is:

The matrix that will perform a yaw around the y axis is:

The matrix to perform a roll around the z axis is:

We can use these matrices to rotate the camera and thus the view space. To do this, we multiply the view matrix by one or more of these.

The way the camera, or any other object transformed with these matrices, rotations can be likened to the movement of an aircraft as illustrated in Figure 15.1. In Figure 15.1 (a), the rotational...

Understanding and fixing compound rotation quirks

In the previous section, you had the chance to experiment with moving the camera around in its 3D environment so you could explore looking at the environment from different angles. While moving the camera is elementary, adding rotations seems to introduce undesired results. In this section, you will discover the source of these issues and look at how to fix them.

Imagine you are holding a camera and looking through it toward the horizon. Now bend down at the hip by 90 degrees so that the camera is looking straight at the ground. Next, rotate your upper body 90 degrees upward to the left. You might be thinking along the steps shown in Figure 15.3. First rotating around the x axis turns the camera to face downward making its y axis horizontal and then a rotation around the y axis will face the camera sideways and result in the x axis sitting horizontally. In performing these orientations, we assume each axis can move independently...

Improving camera orientations

Regardless of the direction the camera is facing, when flying around in a 3D environment, being able to yaw around the world’s up axis is far less disorientating than yawing at an unexpected angle, no matter how mathematically correct it may be. Rolling around the forward-facing vector of the camera is however intuitive as it is the way the viewer is facing. Many first-person player controllers are set up to rotate around the world's up axis and the camera-forward axis as shown in Figure 15.5. It prevents the camera tipping accidentally upside down and experiencing gimbal lock.

Figure 15.5: Intuitive camera flying rotation axes

To instigate rotations using these axes, we need to be able to switch between local and world space. In this instance we want to rotate the camera in world space (where it yaws around the up axis) and roll around the local z axis, which is the axis that indicates the direction the camera is...

Summary

Rotations would have to be the single most cause of headaches for programmers working in 3D environments. By now you will have an appreciation for the complexity of their mathematics. It’s often not until you start using the mathematics that you find out what can go wrong. Only then can you really understand how the errors occur and what you can do to fix them. The issues that affect first-person controller rotations also impact the navigation systems of virtual moving objects such as simulated aircraft and spaceships.

In this chapter, we have explored the mathematics required to move and rotate a virtual camera. This knowledge will allow you to position and angle a camera anywhere in the world to view the objects being rendered. Though the use of several key presses, the camera in your project will now be able to move and rotate. This is essential understanding for any graphics or game programmer, as is being able to eye ball a transformation matrix and have an awareness...

Answers

Exercise A:

In Camera.py add extra if statements to handle the ‘A’ and ‘D’ key presses and use the x axis to move the camera as follows:

def update(self):
    key = pygame.key.get_pressed()
    if key[pygame.K_w]:
        self.transform.update_position(
              self.transform.get_position() 
                             + pygame.Vector3(0, 0, 
                                              self.pan_speed),
                              False)
    if key[pygame.K_s]:
        self.transform.update_position
              (self.transform.get_position() 
                            + pygame.Vector3(0, 0, 
                                             -self.pan_speed),
                              False)
    if key[pygame.K_a]:
        self.transform.update_position(
              self.transform.get_position() 
                            + pygame.Vector3(self.pan_speed, 
                                                      0, 0),
        ...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mathematics for Game Programming and Computer Graphics
Published in: Nov 2022Publisher: PacktISBN-13: 9781801077330
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Penny de Byl

Penny de Byl is a full stack developer with an honors in graphics and Ph.D. in artificial intelligence for games. She has a passion for teaching, teaching games development and computer graphics for over 25 years in universities in Australia and Europe. Her best-selling textbooks, including Holistic Game Development with Unity, are used in over 100 institutions. She has won numerous awards for teaching, including an Australian Government Excellence in Teaching Award and the Unity Mobile Game Curriculum Competition. Her approach to teaching computer science and related fields is project-based giving you hands-on workshops you can immediately get your teeth into. The full range of her teaching interests can be found at H3D Learn.
Read more about Penny de Byl