## Introduction

Whenever you move a character on a screen, **Linear Algebra** concepts are at play. **Vectors** are constantly being manipulated by **Matrices**; essentially *Transforming*, the coordinate system of the vector. Movement, in 3D Graphics, occurs when a character’s coordinate system is transformed to another coordinate system.

A **Vector** is simply an entity that has magnitude but also contains information about its direction. A vector represents displacement between two objects. A **Matrix** is an entity that contains within itself the information required to transform one coordinate system to another.

A **Transformation** is the process of converting the coordinate system of an object into another coordinate system. A **Transformation Matrix** is a matrix used to transform coordinate systems. By simply multiplying a vector by a transformation matrix, the vector’s coordinate system will be transformed.

## Linear Algebra Concepts

### Vectors

Each vertex of a character can be referred to as a vector with *n*-number of components. Each of these components represent a displacement along the x, y or z-axis. For example, a vertex represented as a vector (2,3,1) represents a *displacement* of two units along the x-axis; three units along the y-axis; one unit along the z-axis.

Vectors have no concept of position. Two vectors located at **different** positions in a coordinate system are identical if they have the same magnitude and direction.

### Matrix

The usefulness of a matrix in computer graphics is its ability to transform geometric data into different coordinate systems. A matrix is composed of elements arranged in rows and columns. The rows and columns of a matrix determines the **dimension** of a matrix.

A matrix containing 2 rows and 3 columns is of dimension **2x3**. Dimensions in matrix arithmetic is very important, since some operations are not possible unless matrices have identical dimensions.

### Transformation

A vector’s coordinate system can be rotated, scaled or skewed. How this occurs depends on the elements of the **Transformation Matrix**. Transformation matrices that rotate, scale or skew a coordinate system are called **Rotation**, **Scale** and **Skew** transformation matrices, respectively.

When a vector is multiplied by a **Rotation Transformation Matrix**, the elements of the matrix manipulate the vector and rotate its coordinate system. The same applies to scale or skew transformations.

A rotation transformation matrix can rotate a coordinate system about the x, y or z-axis. Rotation transformation matrices can also be combined to form double or triple rotations. These type of rotations are called **Euler Rotations**. For example, we can combine a rotation *about the x-axis* with a rotation *about the y-axis*, producing a new transformation matrix that will rotate the coordinate system about the x and y axis, simultaneously.

## Linear Algebra Operations

### Vectors Operations

Vectors can be added and subtracted among themselves. Vectors can be multiplied by a scalar but can’t be multiplied among themselves. There are two multiplication equivalents in vector arithmetic. They are: **Dot Product** and **Cross Product**. The *Dot Product* produces a scalar and is mainly use to determine the angle between vectors. The *Cross Product* produces a vector perpendicular to the multiplicand and multiplier vectors.

### Matrix Operations

Matrices can be added and subtracted. However, they must be of the same dimension. Matrices can be multiplied by a scalar. Unlike matrix addition, multiplication among matrices does not require matrices to be of the same dimensions. However, the number of rows in one matrix must be equal to the number of columns in the second matrix.

### Transformations in 3D

The main *linear algebra* operation we are interested is called a **Transformation**. Transformations occurs when a vector is multiplied by a **Transformation Matrix**. To understand how this is done, we will first go over how a vector is multiply with a matrix. Then you will be introduced to the **Rotation Transformation Matrix** and how it rotates a vector’s coordinate system.

#### A Vector times a Matrix

A vector is made up of components, each representing a displacement along an axis. The vector’s dimension determines the number of components in a vector. A 3D vector will contain three components; x, y and z. Mathematically, a vector is represented as follows:

Where i, j and k represents what is called a

Basis Vector. In simple terms, they represent the x, y and z axis, respectively.

A matrix is composed of elements arranged in rows and columns. The rows and columns of a matrix determines the **dimension** of a matrix. Mathematically, a matrix of dimension 3x3, i.e., containing 3 rows and 3 columns is represented as follows:

The elements of a matrix can be thought of as vectors. How the vectors are ordered in the matrix depends if the matrix is arranged in **Column Major** or **Row Major** form.

In

Column Majorthe vectors are arranged vertically, whereas inRow Majorthe vectors are arranged horizontally. The form of a matrix is very important, since a vector multiplied with either of these forms will have a different result. OpenGL requires all matrices to be inColumn Majorform.

Two matrices can be multiplied if the number of columns in one matrix is equal to the number of rows in the second matrix. Multiplying a vector times a matrix is possible because a vector such as:

can be transpose to form a matrix of dimension 3x1 in *Row Major* form.

To multiply a vector times a matrix simply multiplyi each component of the vector with the respective component of the matrix and adding the result. For example:

Here is an actual example:

#### Rotating a Vector

The *identity matrix* is a perfect matrix to understand how *transformation* works. An **Identity Matrix** is a special kind of matrix which is similar to the concept of “1” in real numbers. Just like a real number multiplied by “1” results in the real number itself, any matrix multiply by the *identity matrix* results in the matrix itself.
The *Identity matrix* is defined as:

An *identity matrix* represents an unaltered coordinate system. Any vector transformed by an *identity matrix* results in the same vector.

However, notice what happens to the vector if the first column of the identity matrix is scaled by a factor of two units.

The x-component of the vector also gets scaled by two units. Why does this happens?

The column’s of the *identity matrix* are special vectors called **basis vectors**. A *basis vector* represents a coordinate system. In the case of a identity matrix, it represents an unaltered coordinate system. When this coordinate system is altered, the coordinate system of vector is also altered.

By manipulating the coefficients of an **Identity Matrix**, a coordinate system transformation occurs.

Thus, to rotate a vector about a particular axis, all that is required is to modify the coefficients of the identity matrix with values that rotate a coordinate system. Mathematically, these rotation coefficients are defined as:

Multiplying a vector by any of the above **Rotation Transformation Matrices** will rotate the vector about the specified axis.

## Quaternions

Using matrices with vectors is enough to produce rotation in computer graphics. However, they are not very efficient since they may produce what is known as **Gimbal Lock**; a state where the axis of rotation is lost. Instead of using matrices to rotate a vector, we will use what are known as **Quaternions**. A more effective and cleaner way to rotate a character in computer graphics.

Quaternions provide a way to represent a rotation about an arbitrary axis. A quaternion has both a *vector* and a *scalar* component. Mathematically it is written as:

Quaternions are favored in games due to its low memory requirements. A quaternion only requires four floating point values to store their information. Unlike matrices, quaternions don’t suffer from *Gimbal Lock* effects. Also unlike matrices, **interpolation** among quaternions is fairly simple. Interpolation is mainly use in animation effects such as a walking movement.

The rotation of a vector is accomplished by converting the vector into a **Pure Quaternion** and multiplying it by a **Unit-Norm Quaternion** and its inverse. Mathematically this is expressed as follows:

## Dual Quaternions

In one word, **Dual-Quaternion** is a beautiful mathematical concept. It combines dual-number theory with quaternion mathematics. A quaternion provides a *rotation* representation about an axis, but it does not provide any translation representation. A *Dual-Quaternion* allows us to represent the rotation and translation of a vector into a single entity.

Instead of using a 4x4 matrix to rotate and translate a vector, all we need to use is a *Dual-Quaternion*. A *Dual-Quaternion* consists of two quaternions called: **Real Quaternion** and **Dual Quaternion**. The *Real Quaternion* represents *rotation*, whereas *Dual Quaternion* represents *translation*.

**PS. Sign up to my newsletter and get development tips**.