You are on page 1of 37

Rotations in 3D Graphics and the Gimbal Lock

Valentin Koch

Autodesk Inc.

January 27, 2016

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 1 / 37


Presentation Road Map

1 Introduction

2 Rotation Matrices in R2

3 Rotation Matrices in R3

4 Gimbal Lock

5 Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 2 / 37


About me

MSc. Mathematics with thesis


in Mathematical Optimization
Principal Research Engineer at
Autodesk, Inc.
Infrastructure Optimization
3D environment, InfraWorks
(similar to Sim City).
Encountered Gimbal Lock using
numerical optimization
algorithms.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 3 / 37


Presentation Road Map

1 Introduction

2 Rotation Matrices in R2

3 Rotation Matrices in R3

4 Gimbal Lock

5 Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 4 / 37


Rotation Matrix in R2
Rotation on the unit circle

A vector (x, y ) of magnitude r , is rotated by an angle t about the origin.


We recall that

x⇤
cos t = ,
r
y⇤
sin t = ,
r
and if r = 1, we obtain

x ⇤ = cos t,
y ⇤ = sin t.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 5 / 37


Rotation Matrix in R2
Rotating an arbitrary point

Given a point (x, y ) at an angle ↵ on the unit circle. We want to rotate it


by angle . Hence
x ⇤ = cos(↵ + ).
Using the trig identities, we see that

x ⇤ = cos(↵ + ) = cos ↵ cos sin ↵ sin


= x cos y sin .

Similarly, we have
y ⇤ = y cos + x sin .

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 6 / 37


Rotation Matrix in R2
Previous result in Matrix notation

Since

x ⇤ = x cos y sin ,

y = y cos + x sin ,

we write ✓ ◆ ✓ ◆✓ ◆
x⇤ cos sin x
=
y⇤ sin cos y
and our rotation matrix in R2 is
✓ ◆
cos sin
R= .
sin cos

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 7 / 37


Presentation Road Map

1 Introduction

2 Rotation Matrices in R2

3 Rotation Matrices in R3

4 Gimbal Lock

5 Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 8 / 37


Rotation Matrices in R3
Major axis approach

How to extend previous result to R3 ? General idea:


R2 rotation around major axis x, y , and z
Extend the 2x2 matrix to 3x3

Question
Why selecting 3 axis to rotate an object in R3 ?

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 9 / 37


Rotation Matrices in R3
Example rotation about z-axis

Example
Step 1 Take 2x2 rotation matrix
✓ ◆
cos sin
R=
sin cos
.
Step 2 Extend to 3x3 by adding z-axis, and keep z value unchanged
0 1
cos sin 0
Rz = @ sin cos 0A .
0 0 1

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 10 / 37


Rotation Matrices in R3
Basic rotation matrices

Given angles ↵, , and , we obtain the basic rotations about the x-axis
0 1
1 0 0
Rx = @0 cos ↵ sin↵A ,
0 sin ↵ cos ↵

the y-axis 0 1
cos 0 sin
Ry = @ 0 1 0 A,
sin 0 cos
and the z-axis 0 1
cos sin 0
Rz = @ sin cos 0A .
0 0 1

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 11 / 37


Rotation Matrices in R3
Disadvantages

Why are basic rotation matrices bad?


Expensive to correct matrix drifting.
Hard to interpolate nicely between two rotations.
Gimbal Lock!

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 12 / 37


Rotation Matrices in R3
Matrix drift

Matrix drift happens when multiple matrices are concatenated.


Round o↵ errors happens on some of the matrix elements, resulting in
sheared rotations.
Need to orthonormalize the matrix.
Gram-Schmidt process is computationally expensive!

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 13 / 37


Rotation Matrices in R3
Linear Interpolation (LERP))

Example

Linearly interpolate between identity I and rotation A, which is 2 around
x-axis.
0 1 0 1 0 1
1 0 0 1 0 0 1 0 0
R = 0.5 @0 1 0A + 0.5 @0 0 1 A = @0 0.5 0.5A
0 0 1 0 1 0 0 0.5 0.5

Let u = (0, 1, 0)T . Then kIuk = kAuk = 1.


p
But kRuk = k(0, 0.5, 0.5)T k = 0.5.

So R is not a rotation matrix!


You need a Spherical Linear Interpolation (SLERP) for the rotational
parts, and LERP for the other parts. Complicated.
Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 14 / 37
Presentation Road Map

1 Introduction

2 Rotation Matrices in R2

3 Rotation Matrices in R3

4 Gimbal Lock

5 Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 15 / 37


Gimbal Lock
Sword movement

We want to rotate a sword by = ⇡ about the y -axis, and use rotations


about the z and y -axis to move the sword down and up again.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 16 / 37


Gimbal Lock
Compose rotation matrices

We first want to rotate to = ⇡2 . Let R = Rx Ry Rz , which is


0 10 10 1
1 0 0 cos 0 sin cos sin 0
@
R = 0 cos ↵ sin ↵ A @ 0 1 0 A @ sin cos 0A .
0 sin ↵ cos ↵ sin 0 cos 0 0 1
Since cos ⇡2 = 0, and sin ⇡2 = 1, the above becomes
0 10 10 1
1 0 0 0 0 1 cos sin 0
R = @0 cos ↵ sin ↵A @0 1 0A @ sin cos 0A ,
0 sin ↵ cos ↵ 1 0 0 0 0 1
which equals
0 1
0 0 1
@
R = sin ↵ cos + cos ↵ sin sin ↵ sin + cos ↵ cos 0A .
cos↵ cos + sin ↵ sin cos ↵ sin + sin ↵ cos 0

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 17 / 37


Gimbal Lock
Simplification of rotation matrix

Using the facts that

sin(↵ ± ) = sin ↵ cos ± cos ↵ sin


cos(↵ ± ) = cos ↵ cos ⌥ sin ↵ cos

we obtain
0 1
0 0 1
R = @ sin(↵ + ) cos(↵ + ) 0A
cos(↵ + ) sin(↵ + ) 0

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 18 / 37


Gimbal Lock
Loosing a degree of freedom

From = 0 to ⇡2 , we now want to rotate 6⇡ about the z-axis, and from


= ⇡2 to ⇡, we want to rotate 6⇡ about the x-axis. But since
0 1
0 0 1
R = @ sin(↵ + ) cos(↵ + ) 0A ,
cos(↵ + ) sin(↵ + ) 0

is a rotation matrix about the z-axis, changing ↵ or has the same


e↵ect! The angle ↵ + may change, but the rotation happens always
about the z-axis with unexpected results.

Watch this Video by PuppetMaster’s 3D experiences, CC-BY.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 19 / 37


Gimbal Lock
Euler angles

Using Euler angles to steer pitch, roll, and yaw.

Pictures by MathsPoetry, CC BY-SA

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 20 / 37


Rotation Alternatives

How to avoid Gimbal Lock?

There are alternative rotations:


Euler angles
Axis-angle representation
Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 21 / 37


Axis angle representation
Euler vector

A rotation vector

r = ✓ê,

where ê = (ex , ey , ez )T is an
arbitrary unit vector, and ✓ is the
angle of rotation about the axis ê.

No Gimbal Lock.
Combining two rotations defined by Euler vectors is not simple.
Cannot use LERP to interpolate.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 22 / 37


Presentation Road Map

1 Introduction

2 Rotation Matrices in R2

3 Rotation Matrices in R3

4 Gimbal Lock

5 Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 23 / 37


Quaternions
The holy grail of 3D rotations

Introduced by William Rowan


Hamilton in 1843
An extension to complex
numbers from two dimensions to
three
i 2 = j 2 = k 2 = ijk = 1

by JP, CC BY-SA 2.0

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 24 / 37


Quaternions
Representation

Definition
A quaternion is a quadruple formed by a scalar w , and a vector
v = (x, y , z). We write it as

q = (w , v).

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 25 / 37


Quaternions
Unit Quaternions

Any rotation in R3 can be represented by a unit quaternion.

Definition
A unit quaternion is a quaternion q, such that kqk = 1, where
kqk = w 2 + x 2 + y 2 + z 2 .

All unit quaternions form a hypersphere in R4 . Rotations happen on the


surface of this hypersphere.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 26 / 37


Quaternions
From axis-angle to quaternions

Given an axis defined by unit vector

u = (ux , uy , uz ),

and an angle
✓.

How do I obtain a rotation quaternion?

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 27 / 37


Quaternions
Complex numbers and R2

Any point in R2 can be represented by a complex number, where the real


part lays on the x-axis, and the imaginary part on the y -axis.

Euler’s formula relates the trigonometric functions to the exponential


function

by gunther and Wereon, CC BY-SA 3.0

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 28 / 37


Quaternions
Extending Euler’s Formula

Let i = (1, 0, 0)T , j = (0, 1, 0) and k = (0, 0, 1). Given u and ✓, an


extension to Euler’s Formula says that
✓ ✓ ✓
ew v = e 2 (ux i+uy j+uz k) = cos + sin (ux i + uy j + uz k).
2 2

Conversion of an axis-angle to a rotation quaternion


Given a unit vector u = (ux , uy , uz ) 2 R3 and a rotation angle ✓ about u,
the corresponding unit quaternion q is
✓ ✓
q = (cos , sin u).
2 2

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 29 / 37


Quaternions
Axis-angle to quaternion example

Example
We want to rotate 90 about the y -axis using a quaternion.

We know q = (cos 2✓ , sin 2✓ u). The y -axis can be represented by


u = (0, 1, 0), and ✓ = ⇡2 . We obtain the unit quaternion
⇡ ⇡
q = (cos , 0, sin , 0).
4 4
Great. What do we do with this rotation quaternion now?

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 30 / 37


Quaternions
Rotation

Rotation
Any vector v 2 R3 can be rotated using a rotation quaternion q by
1
qpq ,

where p = (0, v), using the Hamilton product.

What is the Hamilton product and how do you take the inverse of a
quaternion?

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 31 / 37


Hamilton Product

Definition
Let q0 = (w0 , v0 ) and q1 = (w1 , v1 ). The Hamilton Product is defined as

q0 q1 = (w0 w1 v0 · v1 , w0 v1 + w1 v0 + v0 ⇥ v1 ).

Using transposes q0 = (w0 , x0 , y0 , z0 )T and q1 = (w1 , x1 , y1 , z1 )T , we can


write 0 1
w 0 w 1 x0 x 1 y 0 y1 z 0 z 1
Bw 0 x 1 + x 0 w 1 + y 0 z 1 z 0 y 1 C
q0 q1 = B C
@w 0 y 1 x 0 z 1 + y 0 w 1 + z 0 x 1 A .
w 0 z 1 + x0 y1 y0 x1 + z 0 w 1

Warning
Quaternion multiplication is not commutative, q0 q1 6= q1 q0

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 32 / 37


Quaternion Inverse

Definition
The inverse of a quaternion q = (w , v) is computed as

1 (w , v)
q = .
kqk2

Addition and subtraction is the same as with complex numbers.

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 33 / 37


Quaternions
Rotation matrix

Given a rotation quaternion q = (w , x, y , z), we can rotate any vector


v 2 R3 using the
Rotation matrix

0 1
2y 2 2z 2
1 2xy 2zw 2xz + 2yw
Rq = @ 2xy + 2zw 1 2x 2 2z 2 2yz 2xw A .
2xz 2yw 2yz + 2xw 1 2x 2 2y 2

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 34 / 37


Quaternions
Compose rotations

Given a rotation defined by q0 , followed by q1 . They can be composed as


q⇤ = q1 q0 .

Example
Sword movement We rotate = ⇡2 about the y -axis v0 = (0, 1, 0), followed
by ↵ = 6⇡ about the x-axis v1 = (1, 0, 0). We obtain the quaternions

⇡ ⇡ ⇡ ⇡
q0 = (cos , 0, sin , 0), q1 = (cos , sin , 0, 0).
4 4 12 12
Compose them q = q1 q0 = (w , x, y , z), we get
⇡ ⇡ ⇡ ⇡
w = cos cos , x = sin cos ,
12 4 12 4
⇡ ⇡ ⇡ ⇡
y = cos sin , z = sin sin .
12 4 12 4
Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 35 / 37
Quaternions
Interpolation

The same as with complex numbers.


LERP
qt = (1 t)q0 + tq1

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 36 / 37


References and Links

Links:
Gimbal Lock
Rotating Objects Using Quaternions
Understanding Quaternions

Valentin Koch (ADSK) IEEE Okanagan January 27, 2016 37 / 37

You might also like