Demystifying Quaternions…

SPIDER R&D
Spider R&D
Published in
5 min readMay 27, 2018

Almost all of us have awed at the magnificence of the VFX and graphics in console games and movies. Be it the moment of destruction, when Thanos closes his fist or when the ball curls in the air to reach the top corner of the goal in a game of FIFA.

All these involve 3D models and their manipulation in space. It’s done so seamlessly that they almost blend into the real world scenario making them so much life-like.

Ever wondered how these are done and the mathematics behind it?

Most of us are quite used to real numbers of while a few of us,with the two dimensional ones. How well do we know about these 2D complex numbers?

Euler representation
The trigonometric representation of Euler’s Formula.

These numbers help us to represent any point on a 2-dimensional plane. They possess a few important properties which enables us to arrive at interesting results.

Now, let’s consider a problem of rotating a 2D vector over a 2D plane through 90 degrees.

To solve this,when we use Vector Algebra,we generally follow the normal procedures taught to us in school. We assume an unknown vector,and try to formulate equations and arrive at a solution.

As we have two unknowns here, one equation is based on the dot products between our new and old vector being zero, and the other based on the magnitude of the new vector being equal to the old one.

Ideally, we must arrive at two solutions: one for 90 degree clockwise rotation and the other for 90 degree anticlockwise rotation.

There’s also another way to do it using 2D complex numbers.To perform rotations in the 2D plane through 90 degrees, we first have to represent our vector in the form of a complex number and multiply the complex number with “ i ” or “- i ”.

And yeah, there you go. The result is your solution. Pretty cool,isn’t it?

Similarly, to rotate through any other angle, we consider a unit 2D complex number of the required argument(angle) and multiply with our initial number and we easily arrive at our solution.

When we try to extend the same concept to a 3-dimensional space, things become more tricky. It’s not as obvious as it looks from the outset.

Now, when we try to rotate this point through the 3D space through a desired angle along a specific direction, it was found out that it was not possible to do it using another 3D number of the similar form as we did in the case of 2D planes.

In the previous case, we just had to multiply a 2D complex number with another 2D complex number to obtain the required rotation. But here, the same concept does not work out so well.

Multiplying a 3D number with another 3D number does not help us in rotation a 3-dimensional space.

This problem baffled quite a lot of mathematicians,and people started thinking about solutions for the same.

It was in the year 1843 when a famous mathematician William Rowan Hamilton came up with an interesting solution. I guess you would have guessed it already. He proposed, which is now called as “The Quaternions” as 4 dimensional numbers.

They are represented as :

The idea was so simple, yet groundbreaking that it opened up new avenues of research on multi dimensional numbers(Octonians and Sedenions being 8 and 16 dimensional numbers respectively). This allowed him to multiply and divide vectors,to rotate and stretch them.

He concluded that to perform rotations in 3D space,we need a number from the 4D space.

Now coming to the point, when we have to perform rotation in 3D space,the coefficients of our Quaternions(say, h) are formulated based on the direction and magnitude of rotation required.

The rotation is achieved by,

[y] = [h][x][h*]

Here , if h = a + bi + cj + dk

then, h* = a — bi — cj — dk

where x and y are represented in the quaternion space.

(if the point to be rotated is [u,v,w] in the 3D space,it is represented as ui + vj + wk in the quaternion space.

If the direction along which the rotation has to be performed is represented by an unit vector v = [v1 v2 v3] and Φ is the angle for rotation around the vector.Then,the coefficients of h,a = cos (Φ/2),b = v1 sin (Φ/2),c = v2 sin (Φ/2),d = v3 sin (Φ/2) )

Coming to the technical perspective, some of its applications lies in

  • Computer Graphics software.
  • Robotics.
  • Aircraft Navigation and determination of orientation of Spacecrafts and other Aerial Vehicles,etc.

Quaternions are often employed in 3D engines to rotate points in space quickly. Since quaternions contain only four variables, they are very compact and require less memory and fewer calculations when compared to 3D matrix methods, which are numerically intensive. This makes them ideal for scripting languages and this is widely used by graphic programmers.

XNA 3D Camera & Billboard Camera Facing Rotation

Python comes with a pre-built package to handle quaternions. Numpy, the fundamental package for scientific computing with Python has a quaternion type, which will help us to manipulate and perform operations on them easily. So, next time if you’re on a project involving 3D rotations, make sure you check this out.

I hope that this article was informative. Cheers!

— Written by Veejay Karthik.

--

--