Vous êtes sur la page 1sur 10

Quaternions

Table of Contents
Quaternions...........................................................................................................................................1
Definition..............................................................................................................................................1
Product of Two Quaternions.................................................................................................................1
Product of a Quaternion and a Vector...................................................................................................1
Conjugate..............................................................................................................................................2
Useful Formulas...................................................................................................................................2
Quaternions vs Matrix Multiplication..................................................................................................3
Combining Rotations............................................................................................................................3
General Derivation for any Rotation Order..........................................................................................4
Calculating 3..................................................................................................................................5
Calculating 2..................................................................................................................................6
Calculating 1..................................................................................................................................7
Singularity at 2 = +90........................................................................................................................8
Singularity at 2 = -90.........................................................................................................................9
Summary...............................................................................................................................................9

Definition
q = q w i q x j q y k q z

(1)

Product of Two Quaternions


q p = q w i qx j q y k q z p w i p x j p y k p z
= q w pw
j q y p w
k q z p w

i q w px j q w p y k q w p z i q x p w ii qx px ij q x p y ik q x p z
ji q y p x jj q y p y jk q y p z
i q z p x k
j q z p y k
k q z p z
k

= q w pw i q w px j q w p y k q w p z i q x p w qx p x k q x p y j q x pz
j q y p w k q y px q y p y i q y p z k q z pw j q z p x i q z p y q z p z

So,
q p = q w pw q x p x q y p y q z p z i q w p x q x p w q y p z q z p y
q p q p q p q p
j q w p y q x p z q y pw q z px k
w z
x y
y x
z w

(2)

Product of a Quaternion and a Vector

d q = v x qx v y q y v z q z i v x q w v y q z v z q y
j v x q z v y q w v z q x k v x q y v y q x v z q w

(3)

q
d = q x v x q y v y q z v z i qw v x q y v z q z v y
j q w v y q x v z qz v x k q w v z q x v y q y v x

(4)

q d = q x v x q y v y qz v z i qw v x q y v z q z v y
j q w v y qx v z qz v x k q w v z qx v y q y v x

(5)

q = q w i q x j q y k q z

(6)

Conjugate

q q = qw q w q x q x q y q y q z q z i qw qx q x q w q y qz qz q y

j q w q y q x qz q y q w q z q x k
q w q z q x q y q y q x q z qw
2

(7)

= qw q x q y q z

For a vector (i.e., a quaternion with no scalar part),


v = v
v1 v2 = v 1x v 2x v 1y v 2y v 1z v 2z i v1y v 2z v 1z v 2y j v 1x v2z v 1z v 2x k v 1x v 2y v 1y v 2x
v2
v 1 = v 2x v 1x v 2y v 1y v 2z v 1z i v 2y v1z v 2z v1y j v 2x v1z v 2z v 1x k v 2x v1y v 2y v1x

(8)

(9)

v2v1 = v1v2

(10)

p q = p w qw p x qx p y q y p z q z i p w q x p x q w p y q z pz q y
j pw q y p x qz p y q w p z q x k p w q z p x q y p y qx pz qw

(11)

Useful Formulas

p q = pw q w p x q x p y q y p z q z i pw qx p x q w p y qz p z q y
j p w q y p x q z p y qw pz q x k pw q z p x q y p y qx p z qw

= q w p w q x p x q y p y q z pz i q x pw q w px q z p y q y pz
j q y p w q z p x qw p y q x p z k q z pw q y p x q x p y qw pz

(12)

= q w p w q x p x q y p y q z pz i qw px q x pw q y p z q z p y
j q w p y qx p z q y pw qz p x k qw p z q x p y q y px q z pw

q p = q w p w q x p x q y p y qz p z i qw px q x pw q y pz qz p y
j q w p y q x pz q y pw qz p x k q w p z q x p y q y px q z p w

So,

(13)

p q = q p

(14)

Quaternions vs Matrix Multiplication


We can show that a quaternion transformation is equivalent to a matrix multiplication.

v' = q

v q = qw i q x j q y k qz i v x j v y k v z q w i q x j q y k q z

= ( i q w v x j q w v y k qw v z qx v x k q x v y j q x v z k q y v x q y v y i q y v z
j q z v x i qz v y qz v z )q w i q x j q y k q z
= [ q x v x q y v y q z v z i q w v x q y v z q z v y j q w v y qx v z q z v x
k q w v z q x v y q y v x ] q w i qx j q y k q z
= q x v x q y v y q z v z qw i q x j q y k q z
i q w v x q y v z q z v y qw i q x j q y k q z
j q w v y qx v z q z v x q w i q x j q y k q z
k q w v z q x v y q y v x qw i q x j q y k q z
2
= q w q x v x qw q y v y qw q z v z i q x v x q x q y v y q x q z v z
j q x q y v x q2y v y q y q z v z k qx q z v x q y q z v y q 2z v z
2
i q w v x qw q y v z q w q z v y q w q x v x q x q y v z q x q z v y
2
2
k q w q y v x q y v z q y q z v y j q w qz v x q y q z v z q z v y
j q 2w v y qw q x v z qw q z v x k q w q x v y q 2x v z q x q z v x
qw q y v y q x q y v z q y q z v x i q w q z v y q x qz v z q 2z v x
2
2
k q w v z qw q x v y q w q y v x j q w qx v z q x v y q x q y v x
2
i q w q y v z q x q y v y q y v x q w q z v z q x q z v y q y q z v x
2
2
2
2
= i q w v x q x v x q y v x q z v x 2 q x q y v y 2 qw q z v y 2 q x q z v z 2 qw q y v z
2
2
2
2
j 2 q w qz v x 2 qx q y v x qw v y q x v y q y v y q z v y 2 q y qz v z 2 q w q x v z
k 2q x qz v x 2q w q y v x 2 q y q z v y 2 qw q x v y q 2w v z q 2x v z q2y v z q 2z v z

Because we're working with normalised quaternions,


v ' = i [1 2 q2y 2 q2z v x 2 q x q y qw q z v y 2 q x q zqw q y v z ]

j [2 qw q z q x q y v x 1 2 q2x 2q 2z v y 2 q y q z qw q x v z ]
k [2q x q z qw q y v x 2 q y q z q w q x v y 1 2 q 2x 2 q 2y v z ]
Which is the same as multiplying by the matrix:

1 2 q 2y 2 q2z
2q x q y q w q z 2q x q z q w q y
v ' = 2q w q z q x q y 1 2 q 2x 2q 2z
2q y q z q w q x
2q x q z qw q y 2q y q zq w q x 1 2 q 2x 2 q 2y


vx
vy
vz

Combining Rotations
We can easily derive the formula for a quaternion that represents a general rotation. Let 1, 2, and
3 to represent three mutually perpendicular unit vectors, where we rotate first by around 1, then
2, and finally 3. The rotation is:

R3 R2 R 1 = cos

R3 R 2 R 1 = cos

e3 sin 3 cos 2 e2 sin 2 cos 1 e1 sin 1


2
2
2
2
2
2

cos 2 e2 cos 3 sin 2 e3 sin 3 cos 2 e3e2 sin 3 sin 2 cos 1 e1 sin 1
2
2
2
2
2
2
2
2
2
2

cos 2 cos 1 e1 cos 3 cos 2 sin 1 e2 cos 3 sin 2 cos 1 e2e1 cos 3 sin 2 sin 1
2
2
2
2
2
2
2
2
2
2
2
2

e3 sin 3 cos 2 cos 1 e3e1 sin 3 cos 2 sin 1 e3e2 sin 3 sin 2 cos 1 e3e2e1 sin 3 sin 2 sin 1
2
2
2
2
2
2
2
2
2
2
2
2

R3 R 2 R 1 = cos

(15)

It will be convenient to define the variable e


e3e2 e e1

(16)

where e = 1 depending on the order of rotation. We also know


e3e3 = e2e2 = e1e1 = 1

(17)

Then
e3e2e1 = e e1 e1 = e
1
1
1
e
e2e1 = e2 e3e2 = e2e2e3 = e3 = 2 e3 = e e3
e
e
e
e
1
1
1
e
e3e1 = e3 e3e2 = e3 e3 e2 = e2 = 2 e2 = e e2
e
e
e
e

(18)

My logic in that step may be sloppy because I've taken cross products of scalars and vectors, and
I'm not sure that's meaningful. However, it works in this case. See the end of this document for a
more detailed proof.
Substituting into (15),
3

cos 2 cos 1 e1 cos 3 cos 2 sin 1 e2 cos 3 sin 2 cos 1 e e3 cos 3 sin 2 sin 1
2
2
2
2
2
2
2
2
2
2
2
2
3
2
1
3
2
1
3
2
1
3
2

e3 sin cos cos


e e2 sin cos sin
e e 1 sin sin cos
e sin sin sin 1
2
2
2
2
2
2
2
2
2
2
2
2

R3 R 2 R 1 = cos

And rearranging terms gives


3

cos 2 cos 1 esin 3 sin 2 sin 1


2
2
2
2
2
2
3
2
1
3
2

e1 cos cos sin


e sin sin cos 1
2
2
2
2
2
2
3
2
1
3
2

e2 cos sin cos


e sin cos sin 1
2
2
2
2
2
2
3
2
1
3
2

e3 sin cos cos


e cos sin sin 1
2
2
2
2
2
2

R3 R2 R 1 = cos

(19)

In this derivation we assumed that the rotations around three mutually perpendicular axes. So while
this equation can be adapted to rotations in any order (by setting the value of e appropriately), it is
not valid if two of the rotation axes are the same. (E.g., if we rotate around the y-axis, then around
the x-axis, then around the y-axis again, the equation is not valid... or is it???? Need to review this.)

General Derivation for any Rotation Order


From (19), the components of the resulting quaternion are given by

cos 2 cos 1
2
2
2
3
2
1
p1 = cos cos sin
2
2
2
3
2

p2 = cos sin cos 1


2
2
2
3
2

p3 = sin cos cos 1


2
2
2
p0 = cos

sin 2 sin 1
2
2
2
3
2
1
e sin sin cos
2
2
2
3
2
1
e sin cos sin
2
2
2
3
2
1
e cos sin sin
2
2
2
e sin

(20)

Note: Although p0 is the scalar component of the quaternion, and p1, p2, p3 are the remaining
components, the subscripts indicate the order of rotation. So for example, if the rotation is around
the y-axis, then around the x-axis, then around the z-axis, the quaternion representing the combined
rotation is ( p0, p2, p1, p3), not (p0, p1, p2, p3)!
To make the derivations easier, we'll use the notation:
1

s1 = sin 1
2
2
Similarly for c 2, s 2, c 3 and s 3

c 1 = cos

(21)

The half-angle formulas can be written:


s1 c 1 =

1
sin 1
2

c12 s 21 = cos 1

(22)

So (20) can be written


p0
p1
p2
p3

=
=
=
=

c3 c2 c1
c3 c2 s 1
c3 s 2 c1
s 3 c 2 c1

e s3 s2 s1
e s 3 s2 c1
e s3c2 s1
e c3 s 2 s1

(23)

The squares of the components will be useful in our derivation. Note that because the variable e
only takes on the values 1, we know e2 = 1.
2

p0
2
p1
p22
2
p3

=
=
=
=

c3 c 2 c 1
2 2 2
c3 c 2 s1
c23 s22 c12
2 2 2
s 3 c2 c1

2e c3 s3 c2 s 2 c1 s 1
2 e c3 s 3 c 2 s 2 c1 s1
2 e c3 s 3 s 2 c 2 c 1 s1
2 e c3 s 3 c 2 s 2 c1 s1

2 2

s 3 s 2 s1
2 2 2
s3 s2 c1
s23 c 22 s21
2 2 2
c 3 s 2 s1

(24)

One more useful expression is


cos a cos b =
=
=
=
=
=
s 2a c 2bc 2a s 2b =

Calculating 3
From (23),

c a s a cb sb
c 2a cb2c 2a s 2bs 2a c 2bs 2a sb2
1s 2a c2b c 2a sb2 s 2a cb2 1c 2a s 2b
cb2 s2a cb2 c 2a sb2 s 2a c 2b s 2b c 2a s 2b
c 2bs 2b 2 s 2a c 2b 2 c2a s 2b
1 2 s 2a c 2bc 2a s 2b
1
1cos a cos b
2

(25)

p0 p3 = c3 c2 c1 e s3 s 2 s 1 s3 c 2 c 1 e c 3 s2 s1
= c3 s 3 c22 c21 e c 23 c 2 s 2 c 1 s1 e s 23 c2 s2 c1 s 1 c3 s 3 s22 s21

Also from (23),


p 1 p 2 = c3 c2 s1 e s 3 s 2 c 1c 3 s 2 c 1 e s3 c2 s1
= c23 c 2 s 2 c 1 s1 e c3 s 3 c 22 s 21 e c3 s 3 s 22 c 21 s 23 c 2 s 2 c 1 s1

Combining the two terms,


2

p0 p3 e p1 p2 = c 3 s 3 c 2 c 1 e c3 c 2 s2 c1 s 1 e s 3 c2 s 2 c1 s 1 c 3 s3 s 2 s 1
2
2 2
2 2
2
e c 3 c2 s 2 c1 s 1 c 3 s3 c 2 s1 c 3 s3 s 2 c1 e s 3 c2 s2 c1 s 1
= c 3 s3 c22 c12 c 3 s3 s 22 s 21 c3 s 3 c22 s 21 c3 s 3 s 22 c 21
2 2
2 2
2 2
2 2
= c 3 s3 c2 c1 c 3 s3 c 2 s1 c 3 s3 s 2 s 1 c3 s 3 s 2 c 1
2
2
2
2
2
2
= c 3 s3 c2 c1 s 1 c 3 s3 s2 s1 c 1
2
2
= c 3 s3 c2 c 3 s3 s 2
2
2
= c 3 s3 c 2 s 2

Using (22)
p0 p3 e p1 p 2 =

1
sin 3 cos2
2

(26)

sin 3 cos 2 = 2 p0 p3 e p1 p2

From (24),
2

p 2 p3 =
=
=
=
=

c3 s 2 c1 2e c 3 s 3 s2 c 2 c 1 s1 s 3 c2 s 1 s 3 c2 c1 2 e c3 s 3 c 2 s 2 c1 s 1 c 3 s 2 s1
2 2 2
2 2 2
2 2 2
2 2 2
c 3 s 2 c 1 s 3 c 2 s 1 s 3 c 2 c1 c3 s2 s 1
c 23 s 22 c 21 c 23 s 22 s 21 s 23 c 22 c21 s 23 c22 s 21
2 2
2
2
2 2
2
2
c 3 s 2 c1 s1 s 3 c 2 c1 s1
2 2
2 2
c 3 s 2 s3 c 2

Using (25),
1
1cos 3 cos 2
2
2
2
2 p2 p3 = 1cos 3 cos 2
2

p2 p3 =

cos 3 cos 2 = 12 p 2 p 3

(27)

Combining with (26),


tan 3 =

2 p 0 p 3 e p 1 p 2
12 p 22 p 23

(28)

This should be calculated using a two-argument arctangent function. There is a singularity where
both the numerator and the denominator are zero. Examining (26) and (27), we can see that this can
only occur if cos 2 is zero, which means 2 = 90. We'll deal with the singularities later.

Calculating 2
From (23),
p0 p2 = c 3 c 2 c 1 e s3 s 2 s 1c 3 s 2 c 1 e s3 c 2 s1
= c23 c 2 s 2 c 21 e c 3 s 3 c 22 c 1 s1 e c3 s 3 s 22 c 1 s1 s23 c 2 s2 s21

Also from (23),

p1 p3 = c3 c2 s 1 e s 3 s 2 c1 s3 c2 c1 e c3 s2 s 1
= c3 s 3 c22 c1 s 1 e c 23 c2 s 2 s 21 e s 23 c2 s 2 c21 c3 s 3 s 22 c 1 s1

Combining the two terms,


2

p 0 p2 e p1 p 3 = c3 c2 s 2 c1 e c 3 s3 c2 c1 s 1 e c3 s 3 s 2 c 1 s1 s 3 c2 s 2 s 1
2
2
2
2
2
2
e c 3 s3 c2 c1 s 1 c3 c2 s2 s 1 s 3 c2 s 2 c1 e c 3 s3 s 2 c1 s 1
= c 23 c 2 s 2 c 21 s 23 c2 s 2 s 21 c23 c2 s2 s 21 s 23 c2 s 2 c21
2
2
2
2
2
2
= c 3 c 2 s 2 c1 s1 s 3 c2 s 2 s1 c 1
2
2
= c3c 2 s2 s3c2 s2
2
2
= c 2 s 2 c3 s3
= c2 s2

Using (22)
p 0 p2 e p1 p3 =

1
sin 2
2

(29)

sin 2 = 2 p0 p 2 e p1 p 3

Calculating 1
From (24),
2

p 1 p2 =
=
=
=
=

2 2

c3 c2 s1 2 e c3 s 3 c 2 s 2 s1 c1 s 3 s 2 c1 c3 s2 c1 2 e c3 s 3 s 2 c 2 c1 s 1 s3 c 2 s1
2 2 2
2 2 2
2 2 2
2 2 2
c 3 c 2 s 1 c 3 s 2 c1 s 3 s 2 c 1 s 3 c2 s 1
c 23 c22 s21 s22 c21 s 23 s22 c12 c 22 s 21
2
2
2 2
2 2
c3 s 3 c2 s1 s2 c1
2 2
2 2
c 2 s1 s2 c1

Using (25),
2

p1 p2 =

1
1cos 2 cos1
2

2 p21 p 22 = 1cos 2 cos 1

(30)

cos 2 cos 1 = 1 2 p1 p2

From (23),
p 0 p1 = c3 c2 c1 e s3 s 2 s 1c 3 c 2 s1 e s3 s 2 c1
= c 23 c 22 c1 s 1 e c 3 s3 c 2 s2 c12 e c 3 s3 c 2 s2 s21 s 23 s 22 c1 s 1

Also from (23),


p2 p3 = c3 s 2 c1 e s 3 c2 s 1 s3 c 2 c 1 e c 3 s 2 s1
= c3 s 3 c2 s 2 c21 e c32 s22 c1 s 1 e s 23 c 22 c1 s 1 c 3 s3 c 2 s2 s21

Combining the two terms,


2

2 2

p 0 p1 e p2 p3 = c 3 c2 c1 s 1 e c3 s 3 c 2 s 2 c 1 e c3 s 3 c 2 s 2 s1 s3 s2 c1 s1
2
2 2
2 2
2
e c3 s 3 c 2 s 2 c 1 c3 s 2 c1 s 1 s3 c 2 c 1 s1 e c 3 s3 c2 s2 s 1
= c 23 c 22 c1 s1 s23 s22 c1 s 1 c 23 s 22 c1 s 1 s23 c 22 c 1 s1
2 2
2 2
2 2
2 2
= c 3 c 2 c1 s1 s3 c 2 c 1 s1 s 3 s 2 c1 s 1 c 3 s 2 c 1 s1
2
2
2
2
2
2
= c 2 c 1 s1 c 3 s 3 s2 c1 s 1 s3 c 3
2
2
= c 2 c 1 s1 s 2 c1 s 1
= c1 s1 c 22 s 22

Using (22),
p0 p1 e p2 p 3 =

1
sin 1 cos2
2

(31)

sin 1 cos 2 = 2 p 0 p1 e p2 p 3

Combining with (30),


tan 1 =

2 p 0 p 1 e p 2 p 3
12 p21 p22

(32)

As before, there is a singularity where 2 = 90.

Singularity at 2 = +90
At 2 = +90,
2
1
= sin 45 =
2
2
2
1
c 2 cos
= cos 45 =
2
2
s2 sin

From (23),
p 0 = c3 c2 c1 e s 3 s 2 s 1 =
p 1 = c3 c2 s 1 e s 3 s2 c1 =
p 2 = c3 s 2 c1 e s 3 c 2 s 1 =
p 3 = s 3 c 2 c1 e c3 s 2 s1 =

2
1

2
1

2
1

c 3 c 1 e s3 s 1 =
c3 s1 e s 3 c 1 =
c 3 c 1 e s3 s 1 =
s3 c1 e c3 s 1 =

2
1

2
1

2
1

cos 1
2
2
3
1
cos sin
2
2
3

cos cos 1
2
2
3
1
sin cos
2
2
cos

sin 1
2
2
3

e sin cos 1
2
2
3

esin sin 1
2
2
3

ecos sin 1
2
2
esin

cos 1 e sin 3 sin 1


2
2
2
2
2
3
1
3

1
p1 = e p 3 =
cos sin
esin cos 1
2
2
2
2
2
p0 = p2 =

(33)

cos

(34)

If e = 1, applying the angle addition formulas gives


3

1
cos 1 sin 3 sin 1 =
cos31
2
2
2
2
2
2

1
1
p1 = p 3 =
cos 3 sin 1 sin 3 cos 1 =
sin 3 1
2
2
2
2
2
2
p 0 = p2 =

cos

Dividing the second equation by the first,


tan 3 1 =

p1
p0

We can arbitrarily choose one of the angles. It's convenient to set one of them to zero, so
3 = 0

tan 1 =

p1
p0

If e = -1, applying the angle addition formulas to (34) gives


3

1
1
cos 1 sin 3 sin 1 =
cos31 =
cos13
2
2
2
2
2
2

1
1
1
p1 = p 3 =
cos 3 sin 1 sin 3 cos 1 =
sin 31 =
sin 13
2
2
2
2
2
2
2
p0 = p2 =

cos

(35)

Dividing the second equation by the first,


tan 1 3 =

p1
p0

Again, we can arbitrarily choose one of the angles.


3 = 0

tan 1 =

p1
p0

(36)

This is the same result as (35), so it holds whether e is positive or negative.

Singularity at 2 = -90
At 2 = -90,
2
1
= sin 45 =
2
2
2
1
c 2 cos
= cos45 =
2
2
s2 sin

From (23),
3

cos 1
2
2
2
2
3
1
1
1
p1 = c3 c2 s 1 e s 3 s2 c1 =
c s e s 3 c 1 =
cos sin

2
2
2 3 1
2

1
1
p 2 = c3 s 2 c1 e s 3 c 2 s 1 =
c3 c1 e s 3 s 1 = cos 3 cos 1
2
2
2
2
3
1
1
1
p3 = s 3 c 2 c1 e c3 s 2 s1 =
s c e c3 s 1 =
sin cos

2
2
2 3 1
2
p 0 = c3 c2 c1 e s 3 s 2 s 1 =

c 3 c 1 e s3 s 1 =

cos

cos 1 e sin 3 sin 1


2
2
2
2
2

1
p 1 = e p3 =
cos 3 sin 1 esin 3 cos 1
2
2
2
2
2
p0 = p 2 =

sin 1
2
2
3

e sin cos 1
2
2
3

e sin sin 1
2
2
3
1
e cos sin
2
2
esin

(37)

cos

(38)

The formulas for p0 and p1 are the same as (34) in the previous section, so (36) holds for 2 = +90.

Summary
From (28), (29), (32) and (36),
e3e2 e e1
2 p 0 p1 e p2 p3
tan 1 =
2
2
12 p1 p2
sin 2 = 2 p0 p2 e p1 p3
tan 3 =

2 p0 p3 e p1 p2
2

12 p2 p3

(39)

Except : If 2 = 90 :
3 = 0
p
tan 1 = 1
p0

Note: Although p0 is the scalar component of the quaternion, and p1, p2, p3 are the remaining
components, the subscripts indicate the order of rotation. So for example, if the rotation is around
the y-axis, then around the x-axis, then around the z-axis, the quaternion is ( p0, p2, p1, p3), not (p0,

p1, p2, p3)!

Detailed Proof of (18)


The most straightforward way to prove this is to work out every possible combination of our three
mutually perpendicular unit vectors.
order
XYZ
YZX
ZXY
ZYX
XZY
YXZ

e3e2
j
k
k
i
ji
i j
j k
ki

e e1
i
j
k
k
i
j

In each case, (18) holds.

e
1
1
1
1
1
1

e2e1
ji
kj
i k
jk
i
k
i j

= e e1
k
i
j
i
j
k

e3e1
i
k
i j
jk
i k
ji
kj

= e e1
j
k
i
j
k
i

e3e2e1 = e
kji 1
ik
j 1
ji k 1
i jk 1
jk i 1
ki j 1

(40)

Vous aimerez peut-être aussi