Game Math: Quaternion Basics

This post is part of my Game Math Series.

A quaternion is a very useful mathematical object devised by Sir William Rowan Hamilton as an extension to complex numbers. It is often used to compactly represent 3D orientations with just four floating-point numbers, as opposed to using a 3-by-3 matrix that contains nine floating-point numbers, and it has other nice properties that I will talk about later.

As its name suggests, a quaternion is composed of four components, one in the real part, and the other three in the imaginary part. A quaternion is usually denoted as:

    \[ q = w + xi + yj + zk,  \]

where w is the real part, (i, j, k) denotes the three imaginary axes, and (x, y, z) denotes the three imaginary components.

For brevity, I will use the notation below to represent a quaternion:

    \[ q = [w, \overrightarrow{v}], where \overrightarrow{v} = (x, y, z). \]

The Fundamental Formula for Quaternions

Below is the fundamental formula that governs the arithmetics of quaternions:

    \[ i^2 = j^2 = k^2 = ijk = -1 \]

With this formula, we can derive the following identities:

Continue reading

Posted in Gamedev, Math | 1 Comment

My Recommended Book List to Game Devs

Here’s a list of books I have read that I recommend to to game devs.

I’ve divided the books into several categories: computer science, math & graphics, physics, and procedural animation.

I intentionally did not include any “gem” or “pearl” books, since they are collections of multiple stand-alone articles, and not all of them are worth reading.

Continue reading

Posted in Design Patterns, Gamedev, Math, Physics, Programming | 9 Comments

Game Math: Faster Re-Normalization of Unit Vectors.

This post is part of my Game Math Series.

I first saw this technique in an article, Hacking Quaternions, written by Jonathan Blow (author of Braid).

To normalize a vector, you divide its individual components by its length.

const Vec3 Normalize(const Vec3 &v)
{
  const float len_sq = v.x * v.x + v.y * v.y + v.z * v.z;
  const float len_inv = 1.0f / std::sqrt(len_sq);
  return Vec3(v.x * len_inv, v.y * len_inv, v.z * len_inv);
}

The bottle neck of this function is the one divided by a square root, since the square root function is generally not fast. We can do better by using a polynomial approximation of the function f(x) = \frac{1}{\sqrt{x}} around x = 1 if all we want is to re-normalize unit vectors, which is a very common scenario in games.

Jonathan Blow says that it is more than enough to approximate the function with a polynomial of degree 1 (a straight line); for demonstrative purposes, however, I will show how to approximate f(x) = \frac{1}{\sqrt{x}} around x = 1 using a polynomial of degree 2 (a quadratic curve). I will use the method covered in this post.

Our polynomial of degree 2 includes three terms:

    \[ P(x) = a_0 + a_1 x + a_2 x^2 \]

Continue reading

Posted in Gamedev, Math | 4 Comments

Game Math: Faster Sine & Cosine with Polynomial Curves

This post is part of my Game Math Series.

We have seen how to approximate a function using polynomials in this post. A good candidate for polynomial approximation would be the sine function, for it is used a lot in games and is not a cheap function to call. This is essentially the same task as approximating the consine curve, since the cosine curve is just a shifted sine curve.

The sine curve is periodic, so will just focus on the domain x \in [0, 2\pi)

sine

We will first try to match a quarter of the sine curve, a downward “hill”.

hill

Continue reading

Posted in Gamedev, Math | 7 Comments

Game Math: Approximation with Polynomial Curves

This post is part of my Game Math Series.

Polynomials

A polynomial is an expression consisting of linear combination of products of variables raised to non-negative integer powers. In this post, I will specifically focus on polynomials of a single variable, namely polynomials of the form:

    \[ P(x) = a_0 + a_1 x + a_2 x^2 + \dots + a_n x^n = \sum_{i = 0}^{n}{a_i x^i} \]

The polynomial above are said to have degree n if the coefficient a_n is non-zero.

Polynomial Curves

We can create curves with polynomials by using the equation y = P(x).

For instance, we can create a parabola with y = x^2:

parabola

Continue reading

Posted in Gamedev, Math | Leave a comment

Game Math: How to Eyeball the Inverse of a Matrix

This post is part of my Game Math Series.

Matrix Multiplication as Linear Combinations of Columns/Rows

As shown in this post, matrix multiplication can be viewed as linear combinations of columns from the left matrix or rows from the right matrix. We can reverse this process and utilize this fact to help us eyeball inverses of matrices without having to do any calculation by hand or calculator.

The product of a matrix with its inverse gives the identity matrix:

    \[ M M^{-1} = I \]

Viewing from the column perspective, columns of the identity matrix are linear combinations of the columns from the left matrix M, where the coefficients of linear combination are from the columns of the right matrix M^{-1}.

Let us use a 3-by-3 matrix as an example:

    \[ M =  {\left[ {\begin{array}{ccc}    0 & 1 & 1 \\    1 & 2 & 3 \\    0 & 0 & 1 \\   \end{array} } \right]} \]

We want to linearly combine the columns to form the three columns from the 3-by-3 identity matrix, namely {\left[ {\begin{array}{c} 1 \\ 0 \\ 0 \end{array} } \right]}, {\left[ {\begin{array}{c} 0 \\ 1 \\ 0 \end{array} } \right]}, and {\left[ {\begin{array}{c} 0 \\ 0 \\ 1 \end{array} } \right]}.

Continue reading

Posted in Gamedev, Math, Uncategorized | Leave a comment

Game Math: Block Matrices

This post is part of my Game Math Series.

Splitting Matrices into Blocks

When performing matrix multiplication, we can split matrices into “blocks”. The matrix multiplication can then be performed as if each block is a single matrix element, given that we split the matrices in a way that the multiplication of block matrices are of legal dimensions.

For instance, a product of two 4-by-4 matrices can be viewed as a product of 2-by-2 matrices, where each matrix element is a 2-by-2 block matrix. Since a 2-by-2 matrix can be legally multiplied by another 2-by-2 matrix, such splitting scheme is valid.

Let’s look at an example with numbers:

    \[ A =  {\left[ {\begin{array}{cccc}    1 & 1 & 0 & 0 \\    1 & 1 & 0 & 0 \\    0 & 0 & 1 & 1 \\    0 & 0 & 1 & 1 \\   \end{array} } \right]} , B =  {\left[ {\begin{array}{cccc}    2 & 1 & 0 & 0 \\    1 & -1 & 0 & 0 \\    0 & 0 & 2 & 1 \\    0 & 0 & 1 & -1 \\   \end{array} } \right]} \]

We can split each of these matrices into four 2-by-2 block matrices:

    \[ A =  {\left[ {\begin{array}{cc}    C & O \\    O & C \\   \end{array} } \right]} ,  B =  {\left[ {\begin{array}{cc}    D & O \\    O & D \\   \end{array} } \right]} \]

where C = {\left[ {\begin{array}{cc} 1 & 1 \\ 1 & 1 \end{array} } \right]}, D = {\left[ {\begin{array}{cc} 2 & 1 \\ 1 & -1 \end{array} } \right]}, and O = {\left[ {\begin{array}{cc} 0 & 0 \\ 0 & 0 \end{array} } \right]}.

Continue reading

Posted in Gamedev, Math | Leave a comment

Game Math: Alternate Views on Matrix Multiplication

This post is part of my Game Math Series.

Definition of Matrix Multiplication

Let A_{l \times m} and B_{m \times n} be a l-by-m matrix and an m-by-n matrix, respectively. Let A_{ij} denote the element in the matrix A on row i and column j (zero-based indexing), “element (i, j)” for short.

From the definition of matrix multiplication, we know that:

    \[ (AB)_{ij} = \sum\limits_{k = 0}^{m - 1} {A_{ik} B_{kj}} \]

Essentially, element (i, j) of the matrix product AB is the dot product of i-th row from A and j-th column from B.

We can compute matrix multiplication by performing one dot product per element in the resulting matrix product AB. However, there are two alternative ways to look at the matrix multiplication operation that can sometimes make your life easier. I learned these neat tips from the “Linear Algebra and Its Applications” course when I was studying at National Taiwan University back in Taiwan.

Linear Combination of Columns

The i-th column of the matrix product AB is a linear combination of columns from the left matrix A, where the coefficients of linear combination are from the i-th column of the right matrix B.

Continue reading

Posted in Gamedev, Math | Leave a comment

Game Physics: Updating AABBs for Polyhedrons

This post is part of my Game Physics Series.

In order for most broadphases to work, it is important to update AABBs of colliders after their position and orientation have changed. This post will present 3 methods to update AABBs for, in particular, colliders of arbitrary polyhedron shapes: the brute-force approach, approximation by local AABB, and the support function approach.

The Brute-Force Approach

This is probably the most straightforward approach of updating a polyhedron’s AABB. You simply loop through all vertices in global coordinates and extract the extrema in all principle axes.

aabb.minPoint.Set( FLT_MAX,  FLT_MAX,  FLT_MAX);
aabb.maxPoint.Set(-FLT_MAX, -FLT_MAX, -FLT_MAX);

for (auto Vec3 &vert : mesh.verts)
{
  const Vec3 globalPos = body.LocalToGlobal(vert);

  aabb.minPoint.x = Min(aabb.minPoint.x, globalPos.x);
  aabb.minPoint.y = Min(aabb.minPoint.y, globalPos.y);
  aabb.minPoint.z = Min(aabb.minPoint.z, globalPos.z);

  aabb.maxPoint.x = Max(aabb.maxPoint.x, globalPos.x);
  aabb.maxPoint.y = Max(aabb.maxPoint.y, globalPos.y);
  aabb.maxPoint.z = Max(aabb.maxPoint.z, globalPos.z);
}

It’s fast to implement and easy to understand. However, this approach does not scale very well with numbers of vertices.

Continue reading

Posted in Gamedev, Physics | Leave a comment

Game Physics: Implementing Support Function for Polyhedrons using Half Edges

This post is part of my Game Physics Series.

The half edge data structure is a nice data structure to represent polyhedron meshes. I will first introduce to you the half edge data structure; then, I will show you an example implementation of the half edge data structure; lastly, I will explain how we can implement an efficient support function for polyhedrons using a hill-climbing technique with the half edge data structure.

Please note that this entire post assumes convex polyhedrons.

The Half Edge Data Structure

The half edge data structure is used to represent meshes using 3 lists: a list of vertices, a list of half edges (directed edges, distinguished from regular bi-directional edges), and a list of faces.

A vertex is a point in 3D. Each half edge points from one vertex to another; two half edges consisted of the same pair of vertices but pointing in opposite directions are twins. A face is a triangle. Vertices, edges, and faces are all features of a mesh.

Note that a face does not necessarily have to be a triangle in a half edge data structure; it can be a polygon of any number of vertices. However, for simplicity’s sake, the example implementation shown in this post uses triangle faces.

The implementation I will show below is index-based instead of pointer-based in terms of reference-keeping, because this implementation supports dynamic addition and removal of vertices and faces, managing its internal memory usage. A free list variable for a feature stores the index of a free feature, and each free feature has a free link property that refers to the next free feature; basically, a free list is implemented as a singly linked list.

We define a face by a loop of 3 half edges, where the face is to the left of each half edge (CCW winding).

Continue reading

Posted in Gamedev, Physics | Leave a comment