Quaternions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Quaternions

Description:

Quaternion Multiplication ... We need this when we want to apply a quaternion rotation to a vector ... vector, V with the quaternion, Q, we take the following ... – PowerPoint PPT presentation

Number of Views:264
Avg rating:3.0/5.0
Slides: 13
Provided by: dataBo
Category:

less

Transcript and Presenter's Notes

Title: Quaternions


1
Quaternions
  • Andrew Williams

2
What is a Quaternion?
  • Its easier to state what we use them for
  • To represent rotations in 3D space
  • Strictly speaking, a quaternion can represent a
    point in 4D space
  • Looks like this
  • w, v, where v is a vector (note the boldface).
  • This might be written w, (x, y, z)

3
What is a Quaternion?
  • Invented by Irish mathematician William Hamilton
    in 1843
  • Complex numbers can be used to rotate vectors in
    2D
  • Hamilton conceived of quaternions as a way of
    rotating vectors in 3D
  • He thought they would be generally useful but
    theyre actually not used for much else

4
Representing Quaternions
  • class Quaternion
  • private
  • float w // rotation
  • float x, y, z // Vector
  • public
  • blah blah blah
  • Remember, all quaternion operations presented
    here depend on us using normalized quaternions
  • aka unit quaternions

5
Unit Quaternions
  • We use only a subset of quaternions
  • Unit quaternions
  • This avoids a lot of hairy maths
  • Complex and imaginary numbers
  • Create a unit quaternion by normalizing
  • normalize()
  • length sqrt(ww xx yy zz)
  • w w/length
  • x x/length
  • y y/length
  • z z/length

6
Quaternion Multiplication
  • Involves dot products and cross products, but
    expanding these gives us (using C-style
    pseudocode)
  • Quaternion multiply(quaternion A,
  • quaternion B)
  • quaternion C
  • C.x A.wB.x A.xB.w A.yB.z - A.zB.y
  • C.y A.wB.y - A.xB.z A.yB.w A.zB.x
  • C.z A.wB.z A.xB.y - A.yB.x A.zB.w
  • C.w A.wB.w - A.xB.x - A.yB.y - A.zB.z
  • return C

7
Quaternion Inversion
  • The inverse of w, v is w, -v
  • ie the inverse of w, (x, y, z) is w, (-x, -y,
    -z)
  • We need this when we want to apply a quaternion
    rotation to a vector
  • Quaternion invert(Quaternion q)
  • Quaternion invertedQ
  • invertedQ.w q.w
  • invertedQ.x -q.x
  • invertedQ.y -q.y
  • invertedQ.z -q.z
  • return invertedQ

8
Multiplying a Vector by a Quaternion
  • To multiply the vector, V with the quaternion, Q,
    we take the following steps
  • Convert the vector (V) into a quaternion (VQ)
  • resultQ VQ invert(Q)
  • resultQ Q resultQ
  • Return resultQs vector - ie the (x, y, z) bit

9
Multiplying a Vector by a Quaternion
  • Vector multplyQtimesV(Q, V)
  • V.normalise()
  • VQ.x V.x
  • VQ.y V.y
  • VQ.z V.z
  • VQ.w 0.0f
  • resultQ multiply (VQ, invert(Q))
  • resultQ multiply(Q, resultQ)
  • Return resultQ

10
A Quaternion from Pitch, Yaw and Roll
  • Quaternion from_euler(pitch, yaw, roll)
  • // Create a Q for each of pitch, yaw and roll
  • // then multiply those together.
  • // the calculation below does the same, just
    shorter
  • Quaternion result
  • float p pitch PIOVER180 / 2.0
  • float y yaw PIOVER180 / 2.0
  • float r roll PIOVER180 / 2.0  
  • float sinp sin(p)
  • float siny sin(y)
  • float sinr sin(r)
  • float cosp cos(p)
  • float cosy cos(y)
  • float cosr cos(r)  
  • result.x sinr cosp cosy - cosr sinp
    siny
  • result.y cosr sinp cosy sinr cosp
    siny
  • result.z cosr cosp siny - sinr sinp
    cosy
  • result.w cosr cosp cosy sinr sinp
    siny    
  • normalise()
  • From http//gpwiki.org/index.php/OpenGLTutorials
    Using_Quaternions_to_represent_rotation

11
Creating a Matrix from a Quaternion
  • // Convert to Matrix
  • Matrix4 QuaterniongetMatrix() const
  • float x2 x x
  • float y2 y y
  • float z2 z z
  • float xy x y
  • float xz x z
  • float yz y z
  • float wx w x
  • float wy w y
  • float wz w z
  • // This calculation would be a lot more
    complicated for non-unit length quaternions
  • // Note Matrix4f doesnt have a constructor
    like this, but you should be able
  • // to see the idea
  • return Matrix4( 1.0f - 2.0f (y2 z2), 2.0f
    (xy - wz), 2.0f (xz wy), 0.0f,
  • 2.0f (xy wz), 1.0f - 2.0f (x2 z2),
    2.0f (yz - wx), 0.0f,
  • 2.0f (xz - wy), 2.0f (yz wx), 1.0f -
    2.0f (x2 y2), 0.0f,
  • From http//gpwiki.org/index.php/OpenGLTutorials
    Using_Quaternions_to_represent_rotation

12
Using Quaternions for a Camera
  • Five steps
  • Use a quaternion to represent the rotation.
  • Generate a temporary quaternion for the change
    from the current orientation to the new
    orientation.
  • PostMultiply the temp quaternion with the
    original quaternion. This results in a new
    orientation that combines both rotations.
  • Convert the quaternion to a matrix and use matrix
    multiplication as normal.
  • From http//www.gamedev.net/reference/programming
    /features/qpowers/page7.asp
Write a Comment
User Comments (0)
About PowerShow.com