My Journey into the Fundamentals of Computer Graphics - Part 3: Return of the Rotational Issues
Tags: gamedev, learning, computergraphics, mathematics
Published: 2023-09-13 16:45:14

## Overview

Hi everyone! This week I continued my journey into the fundamentals of computer graphics by learning about linear & affine transformations. However, before I dive into the things that I learned, I would like to celebrate that today is Programmer's Day 🥳! As part of the celebration, I'm sharing with you this [free Fanatical bundle](https://www.fanatical.com/en/bundle/international-day-of-the-programmer-2023-free-giveaway) which I just discovered. The bundle includes 4 programming books in pdf & epub format. One of the books included in the bundle is [**Mathematics for Game Programming and Computer Graphics**](https://www.goodreads.com/book/show/66087612) by Penny de Byl, which is one of the books I've been reading during my journey into the fundamentals of computer graphics. So definitely check out the bundle if you're interested in learning the things that I've learned!

## Top 3 things I learned

### Watch out for the order of your transformations!

Whenever you're calculating the matrix for a transformation that you want to apply to an object, you need to understand how the library you're using will process the matrix. Otherwise, you'll end up with a different transformation than the one that you expected.

For example, in OpenGL, the transformations are processed in reverse order. This means that if you want to translate, then rotate and then scale an object, in OpenGL you need to first scale, then rotate and finally translate. This is because the multiplication of a matrix with another matrix isn't commutative, which means that the final result will be different.

In a very particular example of translating, rotating & scaling an object, if you don't reverse the order of operations in OpenGL, the object won't appear on the screen:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694570972918/bc9e19e7-5961-4b91-898c-bfb229a7cd43.png?auto=compress,format&format=webp align="center")

This is the result of the matrix transformation if you don't reverse the order:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694571116743/c1ce66c7-e2df-4de1-b256-dee60c672fd5.png?auto=compress,format&format=webp align="center")

While this is the expected result of the transformation:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694571166218/f2c901af-2a47-453f-ad93-1cdfac79d4e2.png?auto=compress,format&format=webp align="center")

And the expected result on the screen:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694571197045/25e0317c-a156-47f0-8c87-b79f46538f8d.png?auto=compress,format&format=webp align="center")

### Apollo 13, you're heading for gimbal lock...

Out of all the transformations that you can apply to an object, rotation is by far the most complex. This is because a lot of issues arise when you try to do rotations in a 3D space. One of the most common problems is called **gimbal lock**. This is basically when an entire dimension is lost because you make a 90° rotation in one of the axes and it gets aligned with another axis, which locks them. This was a very big problem during the [mission of Apollo 13 to the Moon](https://history.nasa.gov/afj/ap13fj/12day4-approach-moon.html). Here's a diagram that will hopefully make it more clear:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694571788431/987efc5e-8956-4fc8-9b84-3d8b0552d13d.png?auto=compress,format&format=webp align="center")

The result of having a gimbal lock in a camera is that it starts to tip roll around the z-axis and it will eventually end upside down.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694572192203/c61e80c9-8bb2-4af8-979e-99cdcbf2124a.gif?auto=compress,format&format=webp align="center")

### An approach to first-person cameras

In order to avoid having gimbal locks in first-person cameras, several restrictions are often placed. These restrictions include setting up the controller to rotate around the world's up axis and the camera's forward axis. Which means making switches between local/object & world space. The minimum and maximum angles of rotation for the y and z axes are also restricted to prevent the camera from going upside down. Here's an example of this implementation for a first-person camera:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694572609801/55e92c21-a916-472a-ae01-363c5fa50200.gif?auto=compress,format&format=webp align="center")

## This week's math concepts

### Matrices & Linear Transformations

I learned that the difference between linear transformations and affine transformations is that the former doesn't include translations. This means that affine transformations need to be represented with 4x4 matrices.

I also learned about these 6 linear transformations:

* Translation
    
* Rotation
    
* Scaling
    
* Orthographic Projection
    
* Reflection
    
* Shearing
    

It was also noted that reflection and shearing are usually not used in 3D graphics and they aren't part of a typical graphics API.

### More on Matrices

The determinant of a matrix is related to the change in size that results from transforming the matrix. This means that the absolute value is related to the change in area or volume, while the sign indicates if there is a reflection or projection. I learned that the inverse of a matrix allows us to compute the reverse/opposite of a transformation, which can be useful for undoing it. Besides, I also learned about 4x4 homogeneous matrices and how to calculate a perspective projection matrix.

## Useful resources

This week, the resources focus mostly on further explanations of matrix concepts and calculators for some of their operations.

* You can learn how to calculate determinants of large matrices in [Math Insight](https://mathinsight.org/determinant_matrix).
    
* You can make quick calculations of determinants with this [online calculator](https://matrix.reshish.com/determinant.php).
    
* You can learn more about matrix math in [Math is Fun](https://www.mathsisfun.com/algebra/matrix-introduction.html) and [Khan Academy](https://www.khanacademy.org/math/algebra-home/alg-matrices).
    
* You can multiply matrices with this [matrix multiplication calculator](https://matrix.reshish.com/multiplication.php).
    

## What's next?

You can't possibly expect to solve all the rotational issues that arise in 3D computer graphics by just applying restrictions in the axes. Usually, a better approach for rotations involves the use of **quaternions**, which is an advanced math concept. This will be my focus for the next week. Meanwhile, I invite you to tell me if you already knew about the gimbal lock and if it has caused you any problems!