# Quaternion Formulas

Here’s a collection of quaternion formulas. Quaternions are very useful for orientation sensing, game programming and augmented reality applications.

### Magnitude

### Normal

### Quaternion Conjugate

### Axis Angle To Quaternion

Where the axis is v and the angle is theta

### Quaternion To Axis Angle

Where theta is the angle and v is the axis vector

### Quaternion From Angular Velocity

The first step is to convert angular velocity into an axis angle. w is angular velocity and t is the time period (sample rate of gyroscopes)

Using theta as the angle and v as the axis vector, it’s possible to translate this axis angle representation into a quaternion using the above formula.

### Quaternion To Angular Velocity

To do this, translate the quaternion into an axis-angle. Again, v represents the axis, theta is the angle and t is the

### Euler Angles To Quaternion

Euler angles are interesting because the order in which the angles are applied is important. The same angles applied in different orders won’t give you the same result. The most common order of application is heading, pitch then roll.

Euler angles are essentially three axis angles. Heading is a rotation around the z axis, pitch is an angle around the y axis and roll is around the x axis. It’s easy to convert these three axis angles into three quaternions, then multiply them together to create a single quaternion representation of the original euler angles.

### Quaternion To Euler Angles

There’s an elegant solution to this problem.

To extract a heading euler angle from a quaternion

Pitch euler angle from quaternion

Roll euler angle from quaternion

### Quaternion Multiplication

To multiply quaternion q by quaternion r to produce quaternion s.

### Rotating Vectors

Quaternions can be used to translate vectors from one frame of reference to another.

q(x,y,z) is a vector constructed from the x,y,z components of the quaternion q. v is the vector to rotate and rv is the rotated vector.

In Your Quaternion to Euler angles conversion,

1) Are you you haven’t inversed Vx and Vz ?

If Roll is around X axis, then it should be noted Vx, not Vz…

If Yaw is around Z axis, then it should be noted Vz, not Vx…

2) The wikipedia formula is different :

Vx = roll = atan2(2.0 * (qr*qi + qj*qk), 1.0 – 2.0 * (qi2 + qj2) )

Vy = pitch = asin(2.0 * (qr*qj – qi*qk) )

Vz = yaw = atan2(2.0 * (qr*qk + qi*qj), 1.0 – 2.0 * (qj2 + qk2) )

why?

https://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions#Quaternion_.E2.86.92_Euler_angles_.28z-y.E2.80.99-x.E2.80.B3_intrinsic.29

Thanks

Thanks for the comment! I know that quaternion to euler function has worked (or appeared to work) correctly for me before. I’ll look into this and update this blog post if I discover any errors.

Ok, I’ve got an answer. You’re right, Vx and Vz do appear switched around. This is because I’ve been using x-y-z rotations rather than z-y-x rotations on the wikipedia. This is why the quaternion to euler angle formula is different too.

I use x-y-z for euler angles and north-east-down for matrices 🙂 Those are normal in aviation.

with

qi qx

qj qy

qk qz

and

qr qw

…with the following notation equivalence :

qi equivalent to qx

qj equivalent to qy

qk equivalent to qz

qr equivalent to qw

…with the following notation equivalence :

qi equivalent to qx

qj equivalent to qy

qk equivalent to qz

qr equivalent to qw