# Rosario 3D

My developer blog

## Monthly Archives: April 2010

## Base quaternion math

10 April 2010

Posted by on Difficult: basic, let’s keep it simple.

A quaternion is a tuple of four number that can be used to represent a rotation. They are composed of number for the real part and a imaginary vector .

## The alternatives

You can say, “*I already represent rotation with Euler angles and matrix, why I need other maths to do things that I already know?*“.

Well, Euler angles are very simple, they only use three numbers but they can give you headaches in some cases, also while summing two rotation is quite simple, rotate a point with Euler angle require expensive trigonometric function, the really bad part about Euler angles is that different combination of angles can give you the same final result and the final result depends on the order in witch you combine the rotation.

Matrix are very generic, a 3×3 matrix can represent rotation, scale and shearing, combining two transformation is more expensive, but rotating a point quite easy. The bad part about rotation matrix is that given a matrix is not easy understand the orientation of the object. Also they use nine numbers, sixteen if you use homogeneous matrices, this can be a problem if you have few memory (for example when you want to pass a lot of parameter to a shader).

## Quaternion operation

Quaternion can save you a lot of time, the basic idea is to store a quaternion to represent the orientation of your object and then use them to compute the matrix to send to openGL or if you are using shader you can use quaternion directly.

It can be strange but rotation in 4D are linear, so you can threat quaternion as a simple polynom:

Addition:

Multiplication (to solve the equation remember that and ):

While adding to quaternion is not meaningful for rotation, the product of two quaternion represent the composed rotation.

We can define the *conjugate* like this:

Conjugate quaternion represent opposite rotation, rotate a point with a quaternion and then rotate again with his conjugate put back in the place.

The norm is defined like the length of the vector.

Now, we would like to define the inverse operator, so we can derived a rotation that multiplied by another give 1 as result. Let’s try. Remember the norm definition? I try to square elevate both sides and I get so

With quaternion compute the inverse is easy as compute a square radix and make a division. Even better is that we will always work with normalized quaternion, so the length will always be one. In this happy case the conjugate is equivalent to the inverse.

Nothing to scare till now, I present only definition, they are quite useless if we didn’t find any practical application, going out with some friend speaking how beautiful quaternion are is pretty lame.

Well as I wrote before, quaternion represent rotation so they can be used to rotate vertexes. If you have a point *p* and a normalized quaternion you can rotate the point with:

Using quaternions multiplications the real part became:

is zero cause is perpendicular to

While the imaginary part became:

Lagrange’s formula say so we can simplify:

There are the coordinate of the rotated point.

If you want to rotate your object by an angle around an axis v you can create create your quaternion like this:

So the axis is related to the imaginary part and the angle represent somewhat the real part, and you can combine multiple quaternion to set complex pose. Now that you have quaternion and we can compose them, you can find useful to convert them to a matrix. Given a quaternion this is the matrix that represent the same rotation:

Quaternion are very fascinating, but they look quite complex, we really need them? Well the best part of quaternion is that you can interpolate them without to much problem and you get a really smooth orientation change. There are different technique to interpolate quaternion with different result, I’ll show them on another post. Stay tuned.