3D Math Primer for Game Programmers (quaternion)

 
DISCLAIMER: You cannot fully understand quaternions in just 45 minutes.
This article is extremely math intensive and is not intended for the weak-hearted.

Introduction

In computer graphics, we use transformation matrices to express a position in space (translation) as well as its orientation in space (rotation). Optionally, a single transformation matrix can also be used to express the scale or “shear” of an object. We can think of this transformation matrix as a “basis space” where if you multiply a vector or a point (or even another matrix) by a transformation matrix you “transform” that vector, point or matrix into the space represented by that matrix.

In this article, I will not discuss the details of transformation matrices. For a detailed description of transformation matrices, you can refer to my previous article titled Matrices.

In this article, I want to discuss an alternative method of describing the orientation of an object (rotation) in space using quaternions.

The concept of quaterinions was realized by the Irish mathematician Sir William Rowan Hamilton on Monday October 16th 1843 in Dublin, Ireland. Hamilton was on his way to theRoyal Irish Academy with his wife and as he was passing over the Royal Canal on theBrougham Bridge he made a dramatic realization that he immediately carved into the stone of the bridge.

William Rowan Hamilton Plaque on Broome Bridge on the Royal Canal commemorating his discovery of the fundamental formula for quaternion multiplication.

Complex Numbers

Before we can fully understand quaterions, we must first understand where they came from. The root of quaternions is based on the concept of the complex number system.

In addition to the well-known number sets (NaturalIntegerReal, and Rational), the Complex Number system introduces a new set of numbers called imaginary numbers. Imaginary numbers were invented to solve certain equations that had no solutions such as:

To solve this expression, we must state that  which we know is not possible because the square of any number (positive or negative) is always positive.

Mathematicians generally can’t accept that an expression does not have a solution so a new term was invented called the imaginary number that can be used to solve such equations.

The imaginary number has the form:

Don’t try to actually understand this term as there is no logical reason why it exists. We just have to accept that  is just something that squares to .

The set of imaginary numbers can be represented by .

The set of complex numbers (represented by the symbol ) is the sum of a real number and an imaginary number and has the form:

It could also be stated that all Real numbers are complex numbers with  and all imaginary numbers are complex numbers with .

Adding and Subtracting Complex Numbers

Complex numbers can be added and subtracted by adding or subtracting the real, and imaginary parts.

Addition:

Subtraction:

Multiply a Complex Number by a Scalar

A complex number is multiplied by a scalar by multiplying each term of the complex number by the scalar:

Product of Complex Numbers

Complex numbers can also be multiplied by applying normal algebraic rules.

Square of Complex Numbers

A complex number can also be squared by multiplying by itself:

Complex Conjugate

The conjugate of a complex number is a complex number with the imaginary part negated and is denoted as either  or .

The product of a complex number and its conjugate gives a special result.

Absolute Value of a Complex Number

We can use the conjugate of a complex number to compute the absolute value (or norm, or magnitude) of a complex number. The absolute value of a complex number is the square-root of the complex number multiplied by its conjugate and is denoted :

Quotient of Two Complex Numbers

To compute the quotient of two complex numbers, we multiply the numerator and denominator by the complex conjugate of the denominator.

Powers of i

If we state that  then it should be possible to raise i to other powers as well.

If we keep writing this sequence, we will see a pattern emerge .

A similar pattern emerges from the increasing negative powers.

You may have seen a similar pattern in mathematics before but in the form  which is generated by rotating a point 90 degrees counter-clockwise on a 2D Cartesian plane and the sequence  is generated by rotating a point 90 degrees clockwise on a 2D Cartesian plane.

Cartesian Plane

Cartesian Plane

The Complex Plane

We can also map complex numbers in a 2D grid called the Complex Plane by mapping the Real part on the horizontal axis and the Imaginary part on the vertical axis.

Complex Plane

Complex Plane

As shown in the previous sequence, we can say that if we multiply a complex number by i, we can rotate the complex number through the complex plane at 90 degree increments.

Let’s see if this is true. We’ll take an arbitrary point p in the complex plane:

and we multiply it by i gives q:

Multiplying q by i gives r:

And multiplying r by i gives s:

And multiplying s by i gives t:

Which is exactly what we started with (p). If we plot these complex numbers on the complex plane, we get the following result.

Complex Numbers on the Complex Plane

Complex Numbers on the Complex Plane

We can also rotate clock-wise in the complex plane by multiplying the complex number by-i.

Rotors

We can also perform arbitrary rotations in the complex plane by defining a complex number of the form:

Multiplying any complex number by the rotor q produces the general formula:

Which can also be written in matrix form:

Which is the method to rotate an arbitrary point around the origin in 2D.

Quaternions

With this knowledge of the complex number system and the complex plane, we can extend this to 3-dimensional space by adding two imaginary numbers to our number system in addition to i.

The general form to express quaternions is

Were, according to Hamilton’s famous expression:

and

You may have noticed that the relationship between ij, and k are very similar to the cross product rules for the unit cartesian vectors:

Hamilton also recognized that the ij, and k imaginary numbers could be used to represent three cartesian unit vectors ij, and k with the same properties of imaginary numbers, such that .

Visualizing the properties of ij, jk, ki

Visualizing the Properties of ij, jk, ki

The image above visualizes the relationship between the cartesian unit vectors represented by ij, and k.

Quaternions as an Ordered Pair

We can also represent quaternions as an ordered pair:

Where v can also be represented by its individual components:

Using this notation, we can more easily show the similarities between quaternions and complex numbers.

Adding and Subtracting Quaternions

Quaternions can be added and subtracted similar to complex numbers:

Quaternion Products

We can also express the product of two quaternions:

Which results in another quaternion. If we replace the imaginary numbers ij, and k in the previous expression by the ordered pairs (also known as the quaternion units),

And substituting back to the original expression together with  gives:

And expanding this expression into a sum of ordered pairs gives:

If we multiply through with the quaternion unit and extract the common vector components, we can rewrite this equation in this way:

This equation gives us the sum of two ordered pairs. The first ordered pair is a Realquaternion and the second is a Pure quaternion. These two ordered pairs can be combined into a single ordered pair:

And if we substitute,

We get:

Which is the general equation of a quaternion product.

A Real Quaternion

Real Quaternion is a quaternion with a vector term of 0:

And the product of two Real Quaternions is another Real Quaternion:

Which is similar to the product of two complex numbers that contain a zero imaginary term.

Multiplying a Quaternion by a Scalar

We can also multiply a quaternion by a scalar which should obey the rule:

We can confirm this by using the product or Real Quaterions shown above to multiply a quaternion by the scalar as a Real Quaternion:

Pure Quaternions

Similar to Real Quaterions, Hamilton also defined the Pure Quaternion as a quaternion that has a zero scalar term:

Or, written in its component parts:

And we can also take the product of two Pure quaternions:

According to the quaternion product rule shown above.

Additive Form of a Quaternion

We can also express quaternions as an addition of the Real and Pure quaternion parts:

Unit Quaternion

Given an arbitrary vector v, we can express this vector in both its scalar magnitude and its direction as such:

And combining this definition with the definition of a pure quaternion gives:

And we can also describe a unit quaternion that has a zero scalar and a unit vector as such:

Binary Form of a Quaternion

We can now combine the definitions of the unit quaternion and the additive form of a quaternion, we can create a representation of quaternions which is similar to the notation used to describe complex numbers:

This gives us a way to represent the quaternion that is very similar to complex numbers:

Quaternion Conjugate

The quaternion conjugate can be computed by negating the vector part of the quaternion:

And the product of a quaternion with its conjugate gives:

Quaternion Norm

If you recall from the definition of the norm of a complex number:

Similarly, the norm (or magnitude) of a quaternion is defined as:

Which allows us to express the norm of a quaternion as:

Quaternion Normalization

With the definition of a quaternion norm, we can use it to normalize a quaternion. A quaternion is normalized by dividing it by :

As an example, let’s normalize the quaternion:

First, we must compute the norm of the quaternion:

Then, we must divide the quaternion by the norm of the quaternion to compute the normalized quaternion:

Quaternion Inverse

The inverse of a quaternion is denoted . To compute the inverse of a quaternion, we take the conjugate of the quaternion and divide it by the square of the norm:

To show this, we can take the fact that by definition of the inverse:

And multiply both sides by the conjugate of the quaternion gives:

And by substitution we get:

And for unit-norm quaternions whose norm is 1, we can write:

Quaternion Dot Product

Similar to vector dot-products, we can also compute the dot product between two quaternions by multiplying the corresponding scalar parts and summing the results:

We can also use the quaternion dot-product to compute the angular difference between the quaternions:

And for unit-norm quaternions, we can simplify the equation:

Rotations

If you recall we defined a special form of the complex number called a Rotor that could be used to rotate a point through the 2D complex plane as:

Then by its similarities to complex numbers, it should be possible to express a quaternion that can be used to rotate a point in 3D-space as such:

Let’s test if this theory holds by computing the product of the quaternion q and the vectorp. First, we can express p as a Pure quaternion in the form:

And q is a unit-norm quaternion in the form:

Then,

We see that the result is a general quaternion with both a scalar and a vector parts.

Let’s first consider the “special” case where p is perpendicular to  in which case, the dot-product term  and the result becomes the Pure quaternion:

In this case, to rotate p about  we just substitute  and .

As an example, let’s rotate a vector p 45° about the z-axis then our quaternion q is:

And let’s take a vector p that adheres to the special case that p is perpendicular to k:

Now let’s find the product of qp:

Which results in a Pure quaternion that is rotated 45° about the k axis.
We can also confirm that the magnitude of the resulting vector is maintained:

Which is exactly the result we expected!

We can visualize this by the following image:

Quaternion Rotation (1)

Quaternion Rotation (1)

Now let’s consider a quaternion that is not orthogonal to p. If we specify the vector part of our quaternion to 45° offset from p we get:

And multiplying our point p by q we get:

And substituting p and  gives:

Which is no longer a pure quaternion, and it has not been rotated 45° and the vector’s norm is no longer 2 (instead it has been reduced to ).

This result can be visualized by the image.

Quaternion Rotation (2)

Quaternion Rotation (2)

Technically, it’s incorrect to represent the quaternion  p’ in 3D space because it’s actually a 4D vector! For the sake of simplicity, I will only visualize the vector component of quaternions.

However, all is not lost. Hamilton recognized (but didn’t publish) that if we post-multiply the result of qp by the inverse of q then the result is a pure quaternion and the norm of the vector component is maintained. Let’s see if we can apply this to our example.

First, let’s compute :

For  gives:

And combining the previous value of qp and  gives:

Which is a pure quaternion and the norm of the result is:

which is the same as p so the norm of the vector is maintained.

The image below visualizes the result of the rotation.

Quaternion Rotation (3)

Quaternion Rotation (3)

So we can see that the result is a pure quaternion and that the norm of the initial vector is maintained, but the vector has been rotated 90° rather than 45° which is twice as much as desired! So in order to correctly rotate a vector p by an angle  about an arbitrary axis , we must consider the half-angle and construct the following quaternion:

Which is the general form of a rotation quaternion!

Quaternion Interpolation

One of the most important reasons for using quaternions in computer graphics is that quaternions are very good at representing rotations in space. Quaternions overcome the issues that plague other methods of rotating points in 3D space such as Gimbal lockwhich is an issue when you represent your rotation with eular angles.

Using quaternions, we can define several methods that represents a rotational interpolation in 3D space. The first method I will examine is called SLERP which is used to smoothly interpolate a point between two orientations. The second method is an extension of SLERP called SQAD which is used to interpolate through a sequence of orientations that define a path.

SLERP

SLERP stands for Spherical Linear Interpolation. SLERP provides a method to smoothly interpolate a point about two orientations.

I will represent the first orientation as  and the second orientation as . The point that is interpolated will be prepresented by  and the interpolated point will be represented by . The interpolation parameter  will interpolate  from  when  to  when .

The standard linear interpolation formula is:

The general steps to apply this equation are:

  • Compute the difference between  and .
  • Take the fractional part of that difference.
  • Adjust the original value by the fractional difference between the two points.

We can use the same basic principle to interpolate between two quaternion orientations.

QUATERNION DIFFERENCE

The first step dictates that we must compute the difference between  and . With regards to quaternions, this is equivalent to computing the angular difference between the two quaternions.

QUATERNION EXPONENTIATION

The next step is to take the fractional part of that difference. We can compute the fractional part of a quaternion by raising it to a power whose value is in the range 0…1.

The general formula for quaternion exponentiation is:

Where the exponential function for quaternions is given by:

And the logarithm of a quaternion is given by:

For t=0, we have:

And for t=1, we have:

FRACTIONAL DIFFERENCE OF QUATERNIONS

And to compute the interpolated angular rotation, we adjust the original orientation  and adjust it by the fractional part of the difference between  and .

Which is the general form of spherical linear interpolation using quaternions. However, this is not the form of the slerp equation that is commonly used in practice.

We can apply a similar formula for performing a spherical interpolation of vectors to quaternions. The general form of a spherical interpolation for vectors is defined as:

This is visualized in the following image.

Quaternion Interpolation

Quaternion Interpolation

This formula can be applied unmodified to quaternions:

And we can obtain the angle  by computing the dot-product between  and .

CONSIDERATIONS

There are two issues with this implementation which must be taken into consideration during implementation.

First, if the quaternion dot-product results in a negative value, then the resulting interpolation will travel the “long-way” around the 4D sphere which is not necessarily what we want. To solve this problem, we can test the result of the dot product and if it is negative, then we can negate one of the orientations. Negating the scalar and the vector part of the quaternion does not change the orientation that it represents but by doing this we guarantee that the rotation will be applied in the “shortest” path.

The other problem arises when the angular difference between  and  is very small then becomes 0. If this happens, then we will get an undefined result when we divide by . In this case, we can fall-back to using linear interpolation between  and .

SQUAD

Just as a SLERP can be used to compute an interpolation between two quaternions, aSQUAD (Spherical and Quadrangle) can be used to smoothly interpolate over a path of rotations.

If we have the sequence of quaternions:

And we also define the “helper” quaternion () which we can consider an intermediate control point:

Then the orientation along the sub-cuve defined by:

at time t is given by:

Conclusion

Besides being extremely difficult to understand, quaternions provide a few obvious advantages over using matrices or Euler angles for representing rotations.

  • Quaternion interpolation using SLERP and SQUAD provide a way to interpolate smoothly between orientations in space.
  • Rotation concatenation using quaternions is faster than combining rotations expressed in matrix form.
  • For unit-norm quaternions, the inverse of the rotation is taken by subtracting the vector part of the quaternion. Computing the inverse of a rotation matrix is considerably slower if the matrix is not orthonormalized (if it is, then it’s just the transpose of the matrix).
  • Converting quaternions to matrices is slightly faster than for Euler angles.
  • Quaternions only require 4 numbers (3 if they are normalized. The Real part can be computed at run-time) to represent a rotation where a matrix requires at least 9 values.

However for all of the advantages in favor of using quaternions, there are also a few disadvantages.

  • Quaternions can become invalid because of floating-point round-off error however this “error creep” can be resolved by re-normalizing the quaternion.
  • And probably the most significant deterrent for using quaternions is that they are very hard to understand. I hope that this issue is resolved after reading this article.

There are several math libraries that implement quaternions and a few of those libraries implement quaternions correctly. In my personal experience, I find GLM (OpenGL Math Library) to be a good math library with a good implementation of quaternions. If you are interested in using quaternions in your own applications, this is the library I would recommend.

Download the Demo

I created a small demo that demonstrates how a quaternion is used to rotate an object in space. The demo was created with Unity 3.5.2 which you can download for free and view the demo script files. The zip file also contains a Windows binary executable but Using Unity, you can also generate a Mac application (and Unity 4 introduces Linux builds as well).


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《3D数学基础:图形与游戏开发(英文版)pdf》是一本以图形和游戏开发为背景的数学教材。这本书主要介绍了在图形和游戏开发中所需的基本数学概念和技巧。 首先,这本书详细介绍了三维几何学的基本原理。它讲解了点、向量和矩阵等基本概念,以及它们在三维坐标系中的表示和操作。读者将学习如何使用三维几何学来描述和变换图形对象。 此外,该书还介绍了常见的三维变换,包括平移、旋转和缩放等。它解释了这些变换是如何通过矩阵乘法来实现的,并且给出了实际应用的示例。 另外,书中还介绍了向量和矩阵运算的常见技巧,如点积、叉积和矩阵逆运算等。这些概念在图形和游戏开发中使用频繁,对于处理光照和碰撞检测等问题非常重要。 此外,该书还涵盖了一些高级主题,如三维投影、三角函数和曲线插值等。这些主题对于实现更复杂的图形效果和动画很有帮助。 总的来说,《3D数学基础:图形与游戏开发(英文版)pdf》是一本非常实用的数学教材,适合对图形和游戏开发感兴趣的读者。通过学习这本书,读者将建立起在图形和游戏开发中所需的数学基础,并能够应用这些知识来实现各种图形效果和游戏功能。 ### 回答2: 《3D数学基础:图形和游戏开发(英文版)PDF》是一本关于计算机图形学和游戏开发中的3D数学入门手册。在此书中,作者详细介绍了各种与3D图形学相关的数学原理和技术。 该书的主要内容包括向量、矩阵、变换、投影、相机、光照、纹理、几何形状等方面的基础知识。通过学习这些数学概念,读者可以更好地理解和掌握3D图形的建模、渲染和动画等技术。 这本书突出了实践性和应用性。每个数学概念都配有详细的解释、示例和应用,读者可以通过实际的编程实践来加深对数学原理的理解。此外,该书还提供了大量的习题和练习题,读者可以通过解答这些问题来加强自己的数学能力和应用能力。 《3D数学基础:图形和游戏开发(英文版)PDF》适合初学者和有一定编程基础的读者阅读。对于计算机图形学和游戏开发的爱好者和从业者来说,掌握3D数学是非常重要的基础。 总之,这本书是一本介绍和应用3D图形学中数学原理和技术的入门手册。读者可以通过学习这本书来建立起对3D数学的基本理解,为进一步深入学习和应用图形学和游戏开发打下坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值