# Quaternions (was Re: moving foward in 3d with x y and z rotation)

Ben Burns wrote:

I suspect that a Quaternoin library might be what your after here,
assuming I’ve got the right end of the stick. Gamasutra have an
article on it, somewhere

Quaternions are really strange mathematical objects, just like complex
numbers except that instead of just a real and imaginary part, you have
three imaginary components, so every quaternion is of the form a + bi +
cj + dk, where i, j, and k when squared equal -1. The odd thing about
these numbers is that they don’t obey the commutative law of
multiplication pq != qp if p and q are quaternions. They’re multiplied
by the distributive law, and by the rules: i^2 = j^2 = k^2 = -1, ij = k
= -j
i, jk = i = -kj, and ki = j = -ik. For rotations in graphics,
you’re going to be interested in the unit quaternions, quaternions for
which sqrt(a^2 + b^2 + c^2 + d^2) = 1, as in this form:

cos(phi/2) + b*sin(phi/2)i + csin(phi/2)j + dsin(phi/2)*k

This corresponds to a rotation of an angle phi about the axis [ b c d ]
(which is a unit vector, of course). A unit quaternion can also be
thought of as a point on the surface of a four-dimensional hypersphere,
so if you try to interpolate between two unit quaternions, you can get
an intermediate rotation. Gamasutra describes Shoemake’s spherical
linear interpolation method, but I think getting the logarithm of the
quaternions and performing linear interpolation is easier. The
logarithm of a quaternion is given by

ln(a + bi + cj + dk) = log(sqrt(a^2 + b^2 + c^2 + d^2))

• ibarctan(r/a)/r
• jcarctan(r/a)/r
• kdarctan(r/a)/r

where r = sqrt(b^2 + c^2 + d^2)

Note that the first component will always be zero for unit quaternions.
Linearly interpolate each of the components of the logarithm of both
quaternions then perform the quaternion exponential on the result, given
by

exp(a + bi + cj + dk) = exp(a)cos® +
i
exp(a)bsin®/r +
jexp(a)csin®/r +
k
exp(a)dsin®/r

where r is the same factor as above. This finds an intermediate
rotation. Now, to actually use the quaternion rotation q on a point
p=[x y z], you compute the quaternion product s’ = qsq^-1 where q is the
unit quaternion a + bi + cj + dk for the rotation you want, q^-1 is a -
bi - cj - dk, and s = xi + yj + zk. s’ will be of the form x’i + y’j +
z’k, so the rotated point is p’=[x’ y’ z’]. The quaternion triple
product above is equivalent to a rotation matrix, as a lot of tedious
algebra can show. The proof is left as an exercise for the reader :-).

## For more information, you can check the Gamasutra article referred to above, and Section 21.1 and Exercise 21.7 in Foley, et. al.'s “Computer Graphics: Principles and Practice”. I had to implement a lot of this for my software renderer library…–

| Rafael R. Sevilla @Rafael_R_Sevilla |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman

Im sorry but this goes WAY over my head …
My level of maths is quite a lot smaller than what is even nescescary to
understand this …
these things look very very complicated - Ill try and find that article in
GameS***a and see if that helps …

ehehe - I finally figured out some wierd equations after about 2 more hours
looking at it by simultaneous equations and substitution.
they sorta work but very limited in workablity (eg they only work between
angles 0 and 90 and always seem to point to the origin) LOL

thanks anyway,
Oliver Batchelor> Quaternions are really strange mathematical objects, just like complex

numbers except that instead of just a real and imaginary part, you have
three imaginary components, so every quaternion is of the form a + bi +
cj + dk, where i, j, and k when squared equal -1. The odd thing about
these numbers is that they don’t obey the commutative law of
multiplication pq != qp if p and q are quaternions. They’re multiplied
by the distributive law, and by the rules: i^2 = j^2 = k^2 = -1, ij = k
= -j
i, jk = i = -kj, and ki = j = -ik. For rotations in graphics,
you’re going to be interested in the unit quaternions, quaternions for
which sqrt(a^2 + b^2 + c^2 + d^2) = 1, as in this form:

cos(phi/2) + b*sin(phi/2)i + csin(phi/2)j + dsin(phi/2)*k

This corresponds to a rotation of an angle phi about the axis [ b c d ]
(which is a unit vector, of course). A unit quaternion can also be
thought of as a point on the surface of a four-dimensional hypersphere,
so if you try to interpolate between two unit quaternions, you can get
an intermediate rotation. Gamasutra describes Shoemake’s spherical
linear interpolation method, but I think getting the logarithm of the
quaternions and performing linear interpolation is easier. The
logarithm of a quaternion is given by

ln(a + bi + cj + dk) = log(sqrt(a^2 + b^2 + c^2 + d^2))

• ibarctan(r/a)/r
• jcarctan(r/a)/r
• kdarctan(r/a)/r

where r = sqrt(b^2 + c^2 + d^2)

Note that the first component will always be zero for unit quaternions.
Linearly interpolate each of the components of the logarithm of both
quaternions then perform the quaternion exponential on the result, given
by

exp(a + bi + cj + dk) = exp(a)cos® +
i
exp(a)bsin®/r +
jexp(a)csin®/r +
k
exp(a)dsin®/r

where r is the same factor as above. This finds an intermediate
rotation. Now, to actually use the quaternion rotation q on a point
p=[x y z], you compute the quaternion product s’ = qsq^-1 where q is the
unit quaternion a + bi + cj + dk for the rotation you want, q^-1 is a -
bi - cj - dk, and s = xi + yj + zk. s’ will be of the form x’i + y’j +
z’k, so the rotated point is p’=[x’ y’ z’]. The quaternion triple
product above is equivalent to a rotation matrix, as a lot of tedious
algebra can show. The proof is left as an exercise for the reader :-).

above, and Section 21.1 and Exercise 21.7 in Foley, et. al.'s “Computer
Graphics: Principles and Practice”. I had to implement a lot of this
for my software renderer library…

## –

| Rafael R. Sevilla dido at pacific.net.ph |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something much simpler like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in math knowledge

OliverOn Fri, 31 Mar 2000, you wrote:

Ben Burns wrote:

I suspect that a Quaternoin library might be what your after here,
assuming I’ve got the right end of the stick. Gamasutra have an
article on it, somewhere

Quaternions are really strange mathematical objects, just like complex
numbers except that instead of just a real and imaginary part, you have
three imaginary components, so every quaternion is of the form a + bi +
cj + dk, where i, j, and k when squared equal -1. The odd thing about
these numbers is that they don’t obey the commutative law of
multiplication pq != qp if p and q are quaternions. They’re multiplied
by the distributive law, and by the rules: i^2 = j^2 = k^2 = -1, ij = k
= -j
i, jk = i = -kj, and ki = j = -ik. For rotations in graphics,
you’re going to be interested in the unit quaternions, quaternions for
which sqrt(a^2 + b^2 + c^2 + d^2) = 1, as in this form:

cos(phi/2) + b*sin(phi/2)i + csin(phi/2)j + dsin(phi/2)*k

This corresponds to a rotation of an angle phi about the axis [ b c d ]
(which is a unit vector, of course). A unit quaternion can also be
thought of as a point on the surface of a four-dimensional hypersphere,
so if you try to interpolate between two unit quaternions, you can get
an intermediate rotation. Gamasutra describes Shoemake’s spherical
linear interpolation method, but I think getting the logarithm of the
quaternions and performing linear interpolation is easier. The
logarithm of a quaternion is given by

ln(a + bi + cj + dk) = log(sqrt(a^2 + b^2 + c^2 + d^2))

• ibarctan(r/a)/r
• jcarctan(r/a)/r
• kdarctan(r/a)/r

where r = sqrt(b^2 + c^2 + d^2)

Note that the first component will always be zero for unit quaternions.
Linearly interpolate each of the components of the logarithm of both
quaternions then perform the quaternion exponential on the result, given
by

exp(a + bi + cj + dk) = exp(a)cos® +
i
exp(a)bsin®/r +
jexp(a)csin®/r +
k
exp(a)dsin®/r

where r is the same factor as above. This finds an intermediate
rotation. Now, to actually use the quaternion rotation q on a point
p=[x y z], you compute the quaternion product s’ = qsq^-1 where q is the
unit quaternion a + bi + cj + dk for the rotation you want, q^-1 is a -
bi - cj - dk, and s = xi + yj + zk. s’ will be of the form x’i + y’j +
z’k, so the rotated point is p’=[x’ y’ z’]. The quaternion triple
product above is equivalent to a rotation matrix, as a lot of tedious
algebra can show. The proof is left as an exercise for the reader :-).

above, and Section 21.1 and Exercise 21.7 in Foley, et. al.'s “Computer
Graphics: Principles and Practice”. I had to implement a lot of this
for my software renderer library…

## –

| Rafael R. Sevilla dido at pacific.net.ph |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman

My flatmate and myself are working on a space sim, which requires all sorts
of rotations, and quaternions were the best way to do it as far as we can
tell. Problem is, all the pages about them have huge explanations about how
they work, when in practice, they arent that difficult, if only you could
find destructions on how to use the things, and not only theory.
Unfortunately, I cant help you because my flatmate did all that stuff. If
you get hold of a library, SOLID has one inbuilt
(http://www.win.tue.nl/~gino/solid/), and only requires a few lines here and
there to use.

Ben> ----- Original Message -----

Sent: 31 March 2000 12:43
To: sdl at lokigames.com
Subject: Re: Quaternions (was Re: [SDL] moving foward in 3d with x y and
z rotation)

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something much simpler
like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in math
knowledge

Oliver

On Fri, 31 Mar 2000, you wrote:

Ben Burns wrote:

I suspect that a Quaternoin library might be what your after here,
assuming I’ve got the right end of the stick. Gamasutra have an
article on it, somewhere

Quaternions are really strange mathematical objects, just like complex
numbers except that instead of just a real and imaginary part, you have
three imaginary components, so every quaternion is of the form a + bi +
cj + dk, where i, j, and k when squared equal -1. The odd thing about
these numbers is that they don’t obey the commutative law of
multiplication pq != qp if p and q are quaternions. They’re multiplied
by the distributive law, and by the rules: i^2 = j^2 = k^2 = -1, ij = k
= -j
i, jk = i = -kj, and ki = j = -ik. For rotations in graphics,
you’re going to be interested in the unit quaternions, quaternions for
which sqrt(a^2 + b^2 + c^2 + d^2) = 1, as in this form:

cos(phi/2) + b*sin(phi/2)i + csin(phi/2)j + dsin(phi/2)*k

This corresponds to a rotation of an angle phi about the axis [ b c d ]
(which is a unit vector, of course). A unit quaternion can also be
thought of as a point on the surface of a four-dimensional hypersphere,
so if you try to interpolate between two unit quaternions, you can get
an intermediate rotation. Gamasutra describes Shoemake’s spherical
linear interpolation method, but I think getting the logarithm of the
quaternions and performing linear interpolation is easier. The
logarithm of a quaternion is given by

ln(a + bi + cj + dk) = log(sqrt(a^2 + b^2 + c^2 + d^2))

• ibarctan(r/a)/r
• jcarctan(r/a)/r
• kdarctan(r/a)/r

where r = sqrt(b^2 + c^2 + d^2)

Note that the first component will always be zero for unit quaternions.
Linearly interpolate each of the components of the logarithm of both
quaternions then perform the quaternion exponential on the result, given
by

exp(a + bi + cj + dk) = exp(a)cos® +
i
exp(a)bsin®/r +
jexp(a)csin®/r +
k
exp(a)dsin®/r

where r is the same factor as above. This finds an intermediate
rotation. Now, to actually use the quaternion rotation q on a point
p=[x y z], you compute the quaternion product s’ = qsq^-1 where q is the
unit quaternion a + bi + cj + dk for the rotation you want, q^-1 is a -
bi - cj - dk, and s = xi + yj + zk. s’ will be of the form x’i + y’j +
z’k, so the rotated point is p’=[x’ y’ z’]. The quaternion triple
product above is equivalent to a rotation matrix, as a lot of tedious
algebra can show. The proof is left as an exercise for the reader :-).

above, and Section 21.1 and Exercise 21.7 in Foley, et. al.'s “Computer
Graphics: Principles and Practice”. I had to implement a lot of this
for my software renderer library…

## –

| Rafael R. Sevilla dido at pacific.net.ph |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman

Yes, via the use of a transformation then a rotation.

The transformation of course to move each point, and the roatation
to turn the point.

— Oliver Batchelor wrote:> I just read that article on gam***a and Im 350 % confused.

Are you sure that this cant successfully be done in something much
simpler like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in math
knowledge

Oliver

=====
Jason Platt.

“In theory: theory and practice are the same.
In practice: they arn’t.”

ICQ# 1546328

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something much simpler like
matrices ?

Matrices are preferable to quaternions in most respects (since the latter
aren’t unique for rotations), but quaternions are easier to interpolate,
and require less storage.

(If someone has a good matrix-based interpolation scheme, go ahead and
post it and I will happily admit my error.)

I think I could probably use one of these things as long as I dont have to
understand it …(Im off to look at SOLID)
I was totally lost trying to read
anything about them … we havent even done complex numbers in math, let alone
3 of them in some wierd arrangement …

thanks,
Oliver BatchelorOn Sat, 01 Apr 2000, you wrote:

My flatmate and myself are working on a space sim, which requires all sorts
of rotations, and quaternions were the best way to do it as far as we can
tell. Problem is, all the pages about them have huge explanations about how
they work, when in practice, they arent that difficult, if only you could
find destructions on how to use the things, and not only theory.
Unfortunately, I cant help you because my flatmate did all that stuff. If
you get hold of a library, SOLID has one inbuilt
(http://www.win.tue.nl/~gino/solid/), and only requires a few lines here and
there to use.

Ben

-----Original Message-----
From: Oliver Batchelor [mailto:@Oliver_Batchelor]
Sent: 31 March 2000 12:43
To: sdl at lokigames.com
Subject: Re: Quaternions (was Re: [SDL] moving foward in 3d with x y and
z rotation)

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something much simpler
like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in math
knowledge

Oliver

On Fri, 31 Mar 2000, you wrote:

Ben Burns wrote:

I suspect that a Quaternoin library might be what your after here,
assuming I’ve got the right end of the stick. Gamasutra have an
article on it, somewhere

Quaternions are really strange mathematical objects, just like complex
numbers except that instead of just a real and imaginary part, you have
three imaginary components, so every quaternion is of the form a + bi +
cj + dk, where i, j, and k when squared equal -1. The odd thing about
these numbers is that they don’t obey the commutative law of
multiplication pq != qp if p and q are quaternions. They’re multiplied
by the distributive law, and by the rules: i^2 = j^2 = k^2 = -1, ij = k
= -j
i, jk = i = -kj, and ki = j = -ik. For rotations in graphics,
you’re going to be interested in the unit quaternions, quaternions for
which sqrt(a^2 + b^2 + c^2 + d^2) = 1, as in this form:

cos(phi/2) + b*sin(phi/2)i + csin(phi/2)j + dsin(phi/2)*k

This corresponds to a rotation of an angle phi about the axis [ b c d ]
(which is a unit vector, of course). A unit quaternion can also be
thought of as a point on the surface of a four-dimensional hypersphere,
so if you try to interpolate between two unit quaternions, you can get
an intermediate rotation. Gamasutra describes Shoemake’s spherical
linear interpolation method, but I think getting the logarithm of the
quaternions and performing linear interpolation is easier. The
logarithm of a quaternion is given by

ln(a + bi + cj + dk) = log(sqrt(a^2 + b^2 + c^2 + d^2))

• ibarctan(r/a)/r
• jcarctan(r/a)/r
• kdarctan(r/a)/r

where r = sqrt(b^2 + c^2 + d^2)

Note that the first component will always be zero for unit quaternions.
Linearly interpolate each of the components of the logarithm of both
quaternions then perform the quaternion exponential on the result, given
by

exp(a + bi + cj + dk) = exp(a)cos® +
i
exp(a)bsin®/r +
jexp(a)csin®/r +
k
exp(a)dsin®/r

where r is the same factor as above. This finds an intermediate
rotation. Now, to actually use the quaternion rotation q on a point
p=[x y z], you compute the quaternion product s’ = qsq^-1 where q is the
unit quaternion a + bi + cj + dk for the rotation you want, q^-1 is a -
bi - cj - dk, and s = xi + yj + zk. s’ will be of the form x’i + y’j +
z’k, so the rotated point is p’=[x’ y’ z’]. The quaternion triple
product above is equivalent to a rotation matrix, as a lot of tedious
algebra can show. The proof is left as an exercise for the reader :-).

above, and Section 21.1 and Exercise 21.7 in Foley, et. al.'s “Computer
Graphics: Principles and Practice”. I had to implement a lot of this
for my software renderer library…

## –

| Rafael R. Sevilla dido at pacific.net.ph |
| Instrumentation, Robotics, and Control Laboratory |

College of Engineering, University of the Philippines, Diliman

## Content-Type: text/html; name="unnamed" Content-Transfer-Encoding: quoted-printable Content-Description:

I really wouldnt mind about interpolating stuff for now …
But how can quaternions or matrices be used to move an object foward when you
know its rotations ?>

Matrices are preferable to quaternions in most respects (since the latter
aren’t unique for rotations), but quaternions are easier to interpolate,
and require less storage.

(If someone has a good matrix-based interpolation scheme, go ahead and
post it and I will happily admit my error.)

does opengl do this ?
Or should I just make up my own matrix and use that …On Sat, 01 Apr 2000, you wrote:

Yes, via the use of a transformation then a rotation.

The transformation of course to move each point, and the roatation
to turn the point.

— Oliver Batchelor <@Oliver_Batchelor> wrote:

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something much
simpler like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in math
knowledge

Oliver

=====
Jason Platt.

“In theory: theory and practice are the same.
In practice: they arn’t.”

ICQ# 1546328

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

yes it does, in particular
glRotate*(angle,x,y,z) where angle is the angle of counterclockwise
rotation in degrees, and x,y,z, are the endpoint of the vector.
glTranslate*(x,y,z) where x,y,z are the values to move along that
particualr axis.

You may want to take a short trip to www.opengl.org and read a few of
the tutorials they have there.

— Oliver Batchelor wrote:> does opengl do this ?

Or should I just make up my own matrix and use that …

On Sat, 01 Apr 2000, you wrote:

Yes, via the use of a transformation then a rotation.

The transformation of course to move each point, and the
roatation
to turn the point.

— Oliver Batchelor wrote:

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something
much

simpler like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in
math

knowledge

Oliver

=====
Jason Platt.

“In theory: theory and practice are the same.
In practice: they arn’t.”

ICQ# 1546328

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

=====
Jason Platt.

“In theory: theory and practice are the same.
In practice: they arn’t.”

ICQ# 1546328

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.

yea but how can you then “get” the matrix that it makes ?
I can apply the glRotate stuff to vertices etc… but how to “take” the values
out…

thanks
OliverOn Sat, 01 Apr 2000, you wrote:

yes it does, in particular
glRotate*(angle,x,y,z) where angle is the angle of counterclockwise
rotation in degrees, and x,y,z, are the endpoint of the vector.
glTranslate*(x,y,z) where x,y,z are the values to move along that
particualr axis.

You may want to take a short trip to www.opengl.org and read a few of
the tutorials they have there.

— Oliver Batchelor <@Oliver_Batchelor> wrote:

does opengl do this ?
Or should I just make up my own matrix and use that …

On Sat, 01 Apr 2000, you wrote:

Yes, via the use of a transformation then a rotation.

The transformation of course to move each point, and the
roatation
to turn the point.

— Oliver Batchelor <@Oliver_Batchelor> wrote:

I just read that article on gam***a and Im 350 % confused.
Are you sure that this cant successfully be done in something
much

simpler like
matrices ?

I dont know much about them either but at least they seem
understandable … because quaternions are way out of my depth in
math

knowledge

Oliver

=====
Jason Platt.

“In theory: theory and practice are the same.
In practice: they arn’t.”

ICQ# 1546328

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

=====
Jason Platt.

“In theory: theory and practice are the same.
In practice: they arn’t.”

ICQ# 1546328

Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com