My Journey into the Fundamentals of Computer Graphics - Part 4: The Quaternion Menace
Tags: gamedev, learning, computergraphics, mathematics
Published: 2023-09-20 04:25:00

## Overview

Hi everyone! This week I continued my journey into the fundamentals of computer graphics by learning the basics of quaternions! They are an advanced math concept and I didn't completely grasp everything. However, I think that the introduction that I had will be fine for my current level. Let's go straight into my discoveries of the week!

## Top 3 things I learned

### Quaternions aren't the solution to all your rotational problems

Usually, when you start learning about quaternions, it's easy to fall into the misconception that they can just be used for everything that involves rotations and you won't have any problems. However, this isn't the case. Quaternions are just one of several tools for representing orientations and should be used after careful consideration of their advantages & disadvantages. I actually had a great discussion about this in my post last week in the [Dev.to community](https://dev.to/), which you can [read here](https://dev.to/tandrieu/comment/29aeb). This was specifically regarding the use of matrices vs. quaternions, so a big shoutout to [Thibaut](https://dev.to/tandrieu) for making all these clarifications!

Here is a comparison of the methods covered in the 3D Math Primer book:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695177686944/6c35d555-046c-46d0-bb73-8857743a91a7.png?auto=compress,format&format=webp align="center")

### Don't neglect polar coordinates!

Even though polar coordinates might look weird or scary at first sight, you definitely should learn how to use them and when. The authors of 3D Math Primer describe how they're actually the coordinate system that we use most of the time when having an informal conversation! For the specific purposes of video games, they are very useful when aiming a camera, weapon or any other object at some target.

### Understand the rotation conventions of your game engine

Whenever you need to implement rotations in any aspect of a game that you're developing, you must have a good understanding of the conventions used by the game engine that you're using. It's important to remember that each game engine, and 3D software in general, follows their own convention regarding how they handle coordinate systems, so it's no surprise that they each have their conventions for rotations. A great example of this difference in conventions comes from this image created by [@FreyaHolmer](https://twitter.com/FreyaHolmer):

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1695179678022/d519cf38-1f44-4c97-b475-5b01ba63612b.jpeg?auto=compress,format&format=webp align="center")

When it comes to rotation conventions, I just did a quick research online on the 3 game engines that I plan on using while learning about shaders. I'll leave the explanation of why I'll be using 3 game engines instead of just focusing on one for another day but I can tell you that [Unity's reveal of their new pricing plan last week](https://www.theverge.com/2023/9/12/23870547/unit-price-change-game-development) had some input in that decision. Anyway, here's what I found:

* [Unity](https://discussions.unity.com/t/are-these-rotation-matrices-right/199208/3): The order "YXZ" is used locally and "ZXY" is used in world space.
    
* [Unreal Engine](https://forums.unrealengine.com/t/euler-rotations-and-matrix-questions/4450/4): The order is roll, pitch, yaw, which would mean "XYZ" in Unreal's coordinate system convention.
    
* [Godot](https://docs.godotengine.org/en/stable/classes/class_basis.html): The default order is "YXZ", although you can change this.
    

The first thing that I noticed after my quick research, is that the conventions used aren't very clear to understand by just reading them. So I'll follow up on this later in my journey when I start using rotations in each of these engines.

## This week's math concepts

### Polar Coordinate Systems

I learned about the importance of using this coordinate system, as mentioned in the previous section. Besides, I learned conversions in 2D coordinate space and in 3D with cylindrical coordinates & spherical coordinates.

### Rotation in Three Dimensions

I learned how to represent orientations, including the advantages & disadvantages, of these 4 methods:

* Matrices
    
* Euler Angles
    
* Exponential Map & Axis-Angle Representations
    
* Quaternions
    

## Useful resources

This week's resources are for people who are interested in learning more about quaternions:

* Read an article on [Euler–Rodrigues formula variations, quaternion conjugation and intrinsic connections](https://www.sciencedirect.com/science/article/pii/S0094114X15000415)
    
* Read a [concise description of why rotation quaternions use half the angle](https://math.stackexchange.com/questions/1385028/concise-description-of-why-rotation-quaternions-use-half-the-angle)
    
* Understand the [OpenGL Quaternion to Rotation Matrix](https://songho.ca/opengl/gl_quaternion.html)
    
* Understand [why quaternions double-cover](https://www.reedbeta.com/blog/why-quaternions-double-cover/)
    

## What's next

After covering most of the math fundamentals in both of the books I'm reading, I'll finally start having an introduction to the render pipeline & shader programming. Meanwhile, I invite you to tell me if you've had to use quaternions before!