Quaternions and Euler Angles Revision #1 This document describes how the VR Audio Kit handles the orientation of the 3D Sound One headset using quaternions and Euler angles, and how to convert between these formalisms. Revision #1 Page 1/8
Table of Contents 1 Description of the 3D Sound One headset...4 Orientation formalisms...4.1 Quaternions...4. Euler angles...5 3 Conversion from quaternions to Euler angles...5 4 Algebra of rotation coordinates and reset heading...6 4.1 Quaternions...6 4. Euler angles...6 5 Application to the HOA library...7 6 Display in OpenGL...7 6.1 Quaternions...7 6. Euler angles...8 Revision #1 Page /8
Document History Date Author Revision Notes Feb 1 st 016 David Le Bansais 1 Initial revision Revision #1 Page 3/8
1 Description of the 3D Sound One headset This headset includes a head-tracking system that knows, in real-time, the position of the user's head in space. This information is available to computers if they connect to the device and request it using Bluetooth. The position of the headset reflects that of the user's head, and is provided as rotations in 3D space. These rotations are relative to the initial position of the headset when it was powered up. This document explains how to interpret the data sent by the headset, for instance to display a headset on the monitor that follows the movements of the user, or to tell the spatialization library how to apply 3D sound effects. Orientation formalisms There are at least two ways to describe orientation in 3D space: Quaternions, a 4-numbers coordinate system that describes orientation as a vector in the x,y,z axis, and an angle θ around this vector. Euler angles, a set of 3 rotations around each of the x,y and z axis. While Euler angles are more intuitive, quaternions are better suited for calculations. For this reason, the headset data is sent as quaternions. It can be converted to Euler angles if desired, and this document explains how to do it..1 Quaternions For a full reference see the Wikipedia article: https://en.wikipedia.org/wiki/quaternions_and_spatial_rotation. The rotation is expressed by a rotation scalar and a vector in 3-dimensional space, the notation used in the rest of this document is the following: Q = q r + q i i + q j j + q k k Q is the quaternion. i, j and k are orthogonal axis in 3D space. The exact direction of i, j and k doesn't matter since they are relative to the position of the headset when it was powered up and therefore have no absolute meaning. q r is the rotation angle, and q i, q j and q k the coefficients along each axis. Note that the vector [q r, q i, q j, q k ] is normalized: q r + q i + q j + q k = 1 The unit quaternion, when there is no rotation, is q r = 1, q i = 0, q j = 0 and q k = 0. Using this notation, the rotation of the headset is expressed as follow: Revision #1 Page 4/8
Left and right (the head rotates on the horizontal plane): q r + q i = 1 and q j = 0, q k = 0. q i becomes positive when the head turns to the right. Up and down (the head rotates on the vertical plane, facing ahead): q r + q k = 1 and q i = 0, q j = 0. q k becomes positive when the head looks down. Rolling (the head is leaning to left and right): q r + q j = 1 and q i = 0, q k = 0. q j becomes positive when the head leans to the left.. Euler angles For a full reference see the Wikipedia article: https://en.wikipedia.org/wiki/euler_angles The VR Audio Kit uses the terms Yaw, Pitch and Roll to describe the rotation of the head following the x-y'-z'' convention, also called intrinsic Tait-Bryan angles. Yaw is the rotation around the vertical axis. The value 0 corresponds to facing the screen (or any reference point), positive values correspond to the head turning to the left, and negative values to the head turning to the right. Pitch is the rotation around the horizontal axis from one ear to the other. The value 0 corresponds to facing the screen (or any reference point), positive values correspond to the head looking down, and negative values to the head looking up. Roll is the rotation around the horizontal axis from the nape to the nose. The value 0 corresponds to the head positioned vertically, positive values correspond to the head leaning to the left, and negative values to the head leaning to the right. The unit triplet when there is no rotation is Yaw = 0, Pitch = 0 and Roll = 0. Euler angles are less suited than quaternions to express all 3D rotations because this coordinate system has singularities, an issue known as gimbal lock. Note that Yaw and q i have different signs: when the head turns to the left, Yaw is positive and q i is negative. 3 Conversion from quaternions to Euler angles Given the choice of axis in the quaternion definition, Euler angles can be obtained using the following formula: Yaw = atan ( (q r q k - q i q j ), 1 (q i + q k )) Pitch = arcsin( (q r q i + q j q k )) Roll = atan ( (q r q j - q i q k ), 1 (q i + q j )) (π / ) The atan (y, x) function computes the arctangent of y/x in such a way that the sign and value of the result are well defined everywhere and follow the quadrant in which (x,y) is located. The complete definition of atan can be found at https://en.wikipedia.org/wiki/atan. Revision #1 Page 5/8
4 Algebra of rotation coordinates and reset heading The rotation of the headset, regardless of the coordinate system used, is relative to the position of the headset when it was powered up. To obtain a meaningful position, applications allow the user to indicate when the headset is in the desired base position (usually when facing a monitor). This operation is called reset heading, and the rotation of the headset when in that position should be the unit rotation: the [1,0,0,0] quaternion or the [0,0,0] Euler angle. For this purpose, an application should record the value of the rotation when the headset is the base position, and substract it from any subsequent measured rotation: R current = R measured R base Calculating the opposite of a rotation, and composing rotations are the two operations we need. 4.1 Quaternions The opposite of the quaternion Q = [q r,q i,q j,q k ] is simply Q' = [q r,-q i,-q j,-q k ]. Composing quaternions Q'' = Q Q' is more complex but simply consists in multiplying the q r + q i i + q j j + q k k and q r ' + q i 'i + q j 'j + q k 'k expressions. Then, equaling coefficients and assuming the products ii, ij obey the following anti-commutation rules: i = j = k = -1 ij = -ji = k jk = -kj = i ki = -ik = j The final result Q'' has the following coefficients: Q r '' = Q r Q r ' Q i Q i ' Q j Q j ' Q k Q k ' Q i '' = Q r Q i ' + Q i Q r ' + Q j Q k ' Q k Q j ' Q j '' = Q r Q j ' Q i Q k ' + Q j Q r ' + Q k Q i ' Q k '' = Q r Q k ' + Q i Q j ' Q j Q i ' + Q k Q r ' 4. Euler angles The opposite of the (Yaw, Pitch, Roll) triplet is (-Yaw,-Pitch, -Roll), and composing Euler angles simply consists in adding their components: Yaw'' = Yaw + Yaw' Pitch'' = Pitch + Pitch' Revision #1 Page 6/8
Roll'' = Roll + Roll' 5 Application to the HOA library The HOA library can be used either with quaternions or Euler angles. We recommend using quaternions when possible because they don't lead to instabilities in calculations, and don't use trigonometric functions. If Q base is the quaternion recorded as the base quaternion when the user chose to reset heading, and if Q last is the last measured quaternion value from the headset, an application should pass the following values to the HOA library mixersetquaternion function: Q r = Q base r Q last r + Q base i Q last i + Q base j Q last j + Q base k Q last k Q i = Q base r Q last i Q base i Q last r Q base j Q last k + Q base k Q last j Q j = Q base r Q last j + Q base i Q last k Q base j Q last r Q base k Q last i Q k = Q base r Q last k Q base i Q last j + Q base j Q last i Q base k Q last r 6 Display in OpenGL Rotations measured from the headset can also be used to display a head (or headset) 3D-model on the screen. In this case it is assumed that the application will use the OpenGL library. This section provides instructions to apply rotations in the OpenGL environment so that the 3D-model display follows the movements of the user's head. 6.1 Quaternions Quaternions are used by converting them into an OpenGL rotation matrix, then multiplying this matrix with the current matrix using the glmultmatrix function. First, a switch of axis and orientation is necessary to make quaternions use the same axis reference as OpenGL: If a, b, c, d are the coefficients to use to create the rotation matrix, we define them as a = q r b = q k c = -q i d = -q j The rotation matrix is then calculated as follow, using the formula found in Wikipedia at https://en.wikipedia.org/wiki/quaternions_and_spatial_rotation#conversion_to_and_from_the_mat rix_representation. The OpenGL matrix is a 4x4 matrix, we therefore extend the formula in Wikipedia from a 3x3 matrix to 4x4 by adding the identity coefficients (they correspond to a null translation), and considering that it is expressed as an array M of 16 values in column-order, it Revision #1 Page 7/8
gives: m 0 = a a + b b - c c d d m 1 = b c + a d m = b d a c m 3 = 0 m 4 = b c a d m 5 = a a - b b + c c d d m 6 = c d + a b m 7 = 0 m 8 = b d + a c m 9 = c d a b m 10 = a a - b b - c c + d d m 11 = 0 m 1 = 0 m 13 = 0 m 14 = 0 m 15 = 1 6. Euler angles These are applied to the OpenGL environment using the glrotate function. To apply a rotation of angle (Yaw, Pitch, Roll) do the following: glrotate(yaw, 0, 1.0, 0) glrotate(pitch, 1.0, 0, 0) glrotate(-roll, 0, 0, 1.0) To apply the opposite of that rotation, calls to glrotate must be reversed: glrotate(roll, 0, 0, 1.0) glrotate(-pitch, 1.0, 0, 0) glrotate(-yaw, 0, 1.0, 0) Revision #1 Page 8/8