Vous êtes sur la page 1sur 211

Chapter 8

Rotation in Three Dimensions


Fletcher Dunn
Valve Software

Ian Parberry
University of North Texas

3D Math Primer for Graphics & Game Development

What Youll See in This Chapter


This chapter tackles the di icult problem o describin! the orientation o an ob"ect in #D$ and the closely related concepts o rotation and ang lar !isplacement% It is di&ided into se&en sections% Section 8%' discusses the subtle di erences bet(een terms like orientation$ !irection$ and ang lar !isplacement% Section 8%) describes ho( to e*press orientation usin! a matri* Section 8%# describes ho( to e*press an!ular displacement usin! +uler an!les% Section 8%, describes the a*is-an!le and e*ponential map orms% Section 8%. describes ho( to e*press an!ular displacement usin! a /uaternion% Section 8%0 compares and contrasts the di erent methods% Section 8%1 e*plains ho( to con&ert an orientation rom one orm to another%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

Word Cloud

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

Section 8%'6

What +*actly is 78rientation9:

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

8rientation
What is orientation: 3ore than direction% ; &ector speci ies direction but it can also be t(isted%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

This is Important <ecause


T(istin! an ob"ect chan!es its orientation%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

Some People 4et Con used


<ecause you can speci y a direction usin! t(o an!les =polar coordinates> Speci yin! an orientation re/uires # an!les% 8r at least # numbers (hiche&er (ay you represent it% To add to the con usion$ there are # popular (ays to represent orientation%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 1

What is ;n!ular Displacement:


8rientation cant be !i&en in absolute terms% ?ust as a position is a translation rom some kno(n point$ an orientation is a rotation rom some kno(n re erence orientation =o ten called the i!entity or home orientation>% The amount o rotation is kno(n as an ang lar !isplacement%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

@o( to Represent 8rientation


'% 3atrices )% +uler an!les #% Auaternions

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

Section 8%)6

3atri* Form

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'C

3atri* Form
Dist the relati&e orientation o t(o coordinate spaces by listin! the trans ormation matri* that takes one space to another% For e*ample6 rom ob"ect space to upri!ht space% Trans orm back by usin! the in&erse matri*%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ''

+*ample
WeE&e seen ho( a matri* can be used to trans orm points rom one coordinate space to another% In the i!ure on the ne*t slide$ the matri* in the upper ri!ht hand corner can be used to rotate points rom the ob"ect space o the "et into upri!ht space% WeE&e pulled out the ro(s o this matri* to emphasiFe their direct relationship to the coordinates or the "etEs body a*es% The rotation matri* contains the ob"ect a*es e*pressed in upri!ht space% Simultaneously$ it is a rotation matri*% We can multiply ro( &ectors by this matri* to trans orm those &ectors rom ob"ect space coordinates to upri!ht space coordinates%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ')

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'#

To Transpose or 2ot to Transpose: That is the Auestion


; le!itimate /uestion to ask is$ (hy does the matri* contain the ob"ect space a*es e*pressed usin! upri!ht space coordinates: Why not the upri!ht space a*es e*pressed in ob"ect space coordinates: ;nother (ay to phrase this is6 (hy did (e choose to !i&e a rotation matri* that trans ormed &ectors rom ob"ect space to upri!ht space: Why not rom upri!ht space to ob"ect space: From a math perspecti&e$ this /uestion is redundant% <ecause rotation matrices are ortho!onal$ their in&erse is the same as their transpose% =Recall rom Chapter 0%> Thus the decision is entirely a cosmetic one%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ',

; Coders Dament
<ut practically speakin!$ in our opinion$ it is /uite important% ;t issue is (hether you can (rite code that is intuiti&e to read and (orks the irst time$ or i it re/uires a lot o (ork to decipher$ or a kno(led!e o con&entions (hich are not stated since they are ob&ious to e&eryone but you% 8ur opinions come rom (atchin! pro!rammers !rapple (ith rotation matrices% We donEt e*pect that e&eryone (ill a!ree (ith us$ but (e hope that e&ery reader (ill at least appreciate the &alue in considerin! these issues%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.

Its ?ust a 3atri*$ Ri!ht:


Certainly e&ery !ood math library (ill ha&e a # * # matri* class that can represent any arbitrary trans ormation$ (hich is to say that it makes no assumptions about the &alue o the matri* elements% =8r perhaps it is a , * , matri* that can do pro"ection$ or a , * # that can do translation but not pro"ection G those distinctions are not important here%> For a matri* like this$ the operations inherently are in terms o some input coordinate space and an output coordinate space% This is "ust implicit in the idea o matri* multiplication% <ut i you need to !o rom output to input$ then you need to obtain the in&erse o the matri*%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'0

<utH
It is a common practice to use the !eneric trans ormation matri* class to describe the orientation o an ob"ect% In this case$ rotation is treated "ust like any other trans ormation% The inter ace remains in terms o a source and destination space% It is our e*perience that the ollo(in! t(o matri* operations are by ar the most commonly used6
'% Take an ob"ect space &ector and e*press it in upri!ht coordinates% )% Take an upri!ht space &ector and e*press it in ob"ect coordinates%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'1

Which 3atri* Should We Ise:


2otice that (e need to be able to !o in both directions% We ha&e no reason to belie&e that either direction is si!ni icantly more common that the other% <ut more importantly$ the &ery nature o the operations and the (ay pro!rammers naturally think about the operations is in terms o o"#ect space and pright space =or some other e/ui&alent terminolo!y$ such as parent space and chil! space>% We do not think o them in terms o a source space and a destination space% It is in this conte*t that (e (ish to consider the /uestion posed at the be!innin! o this section6 (hich matri* should (e use:

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'8

8rientation in a State Jariable


First$ (e should back up a bit and remind oursel&es o the mathematically moot but yet conceptually important distinction bet(een orientation and ang lar !isplacement% I your purpose is to create a matri* that does a speci ic an!ular displacement =such as 7rotate #CK about the x-a*is>$ then the t(o operations listed ) slides a!o are not really the ones you probably ha&e in your head$ and usin! a !eneric trans orm matri* (ith its implied direction o trans ormation is no problem$ and so this discussion does not apply% Instead$ suppose that the orientation o some ob"ect is stored as a state &ariable%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B

Were ;ll Coders$ Ri!ht:


DetEs assume that (e adopt the common policy and store orientation usin! the !eneric trans ormation matri*% WeEll arbitrarily pick a con&ention that multiplication by this matri* (ill trans orm rom ob"ect to upri!ht space% I (e ha&e a &ector in upri!ht space and (e need to e*press it in ob"ect space coordinates$ (e must multiply this &ector by the in&erse o the matri*% 2o( letEs see ho( our policy a ects the code that is (ritten and read hundreds o times by a&era!e !ame pro!rammers%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C

In Code
Rotate some &ector rom ob"ect space to upri!ht space is translated into code as multiplication by the matri*% Rotate a &ector rom upri!ht space to ob"ect space is translated into code as multiplication by the in&erse o the matri*% =;ctually$ multiplication by the transpose$ since rotation matrices are ortho!onal$ but that is not the point here%>
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& )'

8n Codin! Style
2otice that the code does not match one-to-one (ith the hi!h-le&el intentions o the pro!rammer% Furthermore$ to read or (rite this code re/uires that you kno( (hat the con&entions are% It is our opinion that this codin! style is a contributin! actor to the di iculty that be!innin! pro!rammers ha&e in learnin! ho( to use matrices%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

))

?ust a 3atri*:
;n e/ui&alent practice is to not use a class at all$ and "ust declare a matri* (ith somethin! like float R L#ML#M % This style o code (ill orce e&ery user to remember (hat the con&entions are e&ery time they use the matri*% From our e*perience$ they are usually not documented$ since it (as o"vio s to the author o this code ho( it (as supposed to (ork% This in&ariably results in iddlin! and random transposin! by the uninitiated$ (ho are orced to re&erse en!ineer the arbitrary con&entions throu!h trial and error%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)#

; Rotation 3atri* Class


;n alternati&e (ay to code is to ha&e a special # * # matri* class that is used e*clusi&ely or rotations% It assumes$ as an in&ariant$ that the matri* is ortho!onal$ meanin! it only contains rotation% =We also (ould probably assume that the matri* does not contain a re lection$ e&en thou!h that is possible in an ortho!onal matri*%> With these assumptions in place$ (e are no( ree to per orm rotations usin! the matri* at a hi!her le&el o abstraction% 8ur inter ace unctions match e*actly the hi!h-le&el intentions o the pro!rammer%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

),

Some ?usti ication


Furthermore$ (e ha&e remo&ed the con usin! linear al!ebra details ha&in! to do (ith ro( &ectors &ersus column &ectors$ (hich space is on the le t or ri!ht$ and (hich (ay is the re!ular (ay and (hich is the in&erse$ etc% 8r rather$ (e ha&e con ined such details to the class internals$ the person implementin! the class certainly needs to pick a con&ention =and hope ully document it>% In act$ in this specialiFed matri* class$ the operations o m ltiply a vector and invert this matrix really are not that use ul% We ad&ocate keepin! this dedicated matri* class con ined to operations that couch thin!s in terms o pright space and o"#ect space$ rather than m ltiply a vector%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

).

I Frank Sinatra Was a Coder$ Would @e Do It @is Way:


8 course$ there are those (ho ind it per ectly ob&ious (hich (ay the re!ular multiplication should !o and (hich should be the transpose$ and they donEt understand (hy others canEt keep it strai!ht% They mi!ht say$ 7You "ust use ro( &ectors and re!ular multiplication rotates rom ob"ect to upri!ht$ because thatEs the rotation thatEs needed the most re/uently% What could be more ob&ious:9 The problem is that there are those (ho eel column &ectors are better$ or that the or(ard direction should !o rom upri!ht to ob"ect space% I these con&entions are so ob&ious$ then (hy doesnEt e&erybody a!ree on (hat they should be: In other (ords$ codin! or di&ersity is a !ood thin!%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)0

It Shouldnt 3atter
So$ back to the /uestion posed earlier6 Which matri* should (e use: 8ur ans(er is 7It shouldnEt matter%9 <y that (e mean there is a (ay to desi!n your matri* code in such a (ay that it can be used (ithout kno(in! (hat choice (as made%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& )1

Whats in a =Function> 2ame:


;s ar as (ritin! real CNN code !oes$ this is purely a cosmetic chan!e% For e*ample$ perhaps (e "ust replace the unction name multiply => (ith ob"ectToIpri!ht=>$ and like(ise (e replace multiply<yTranspose=> (ith upri!htTo8b"ect=>% 8ur ar!ument is that the &ersion o the code (ith descripti&e$ named coordinate spaces is easier to read and (rite% The unction names multiply=> and multiply<yTranspose=> ha&e no descripti&e &alue =they could be replaced (ith doTheThin!=> and doThe8therThin!=> and no in ormation is lost>% To use these unctions re/uires you to kno( (hat 7the thin!9 is and (hat 7the other thin!9 is% <ut ob"ectToIpri!ht=> and upri!htTo8b"ect=> are descripti&e and sel -contained%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)8

<ack to the 3ath


+nou!h on ho( our con&entions encoura!e a robust codin! style% You (ill probably "ust i!nore us and !et into trouble$ but (e tried%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)B

Direction Cosines 3atri*


; !irection cosines matrix is the same thin! a a rotation matri*$ but the term re ers to a special (ay to interpret =or construct> the matri*% The term !irection cosines re ers to the act that each element in a rotation matri* is the dot product o a cardinal a*is in one space (ith a cardinal a*is in the other space% For e*ample$ the center element m)) in a # * # matri* !i&es the dot product that the y-a*is in one space makes (ith the y-a*is in the other space%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

#C

Direction Cosines 3atri*


3ore !enerally$ letEs say that the basis &ectors o a coordinate space are the mutually ortho!onal unit &ectors $ (hile a second coordinate space (ith the same ori!in has as its basis a di erent =but also orthonormal> basis The rotation matri* that rotates ro( &ectors rom the irst space to the second is the matri* o direction cosines =dot products> o each pair o basis &ectors$ as ollo(s6
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& #'

Direction Cosines 3atri*

These a*es can be interpreted as !eometric rather than al!ebraic entities$ so it really does not matter (hat coordinates are used to describe the a*es =pro&ided (e use the same coordinate space to describe all o them>$ the rotation matri* (ill be the same%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& #)

+*ample
For e*ample$ letEs say that our a*es are described usin! the irst coordinate space% Then ha&e the tri&ial orms L'$ C$ CM$ LC$ '$ CM and LC$ C$ 'M$ respecti&ely% The basis &ectors o the second space$ are not e*pressed in their o(n space$ and thus they ha&e arbitrary coordinates% When (e substitute the tri&ial &ectors into the matri* on the pre&ious slide and e*pand the dot products$ (e !et6
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ##

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

#,

Conclusion
In other (ords$ the ro(s o the rotation matri* are the basis &ectors o the output coordinate space$ e*pressed usin! the coordinates o the input coordinate space% 8 course$ this act is not "ust true or rotation matrices$ itEs true or all trans ormation matrices% This is the central idea o (hy a trans ormation matri* (orks$ (hich (as de&eloped in Chapter ,%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

#.

The 8ther Case


2o( letEs look at the other case% Instead o e*pressin! all the basis &ectors usin! the irst coordinate space$ measure them usin! the second coordinate space =the output space>% This time$ are arbitrary% ha&e tri&ial orms$ and

Puttin! these into the direction cosines matri* produces6


Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& #0

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

#1

Conclusion
This says that the columns o the rotation matri* are ormed rom the basis &ectors o the input space$ e*pressed usin! the coordinates o the output space% This is not true o trans ormation matrices in !eneralO it applies only to ortho!onal matrices such as rotation matrices%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

#8

;d&anta!es o 3atri* Form '


Rotation of vectors is immediately available% You can use a matri* to rotate &ectors bet(een ob"ect and upri!ht space% 2o other representation o orientation allo(s this in order to rotate &ectors$ you must con&ert the orientation to matri* orm%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

#B

;d&anta!es o 3atri* Form )


Format used by graphics APIs% 4raphics ;PIs use matrices to e*press orientation% ;PI stands or ;pplication Pro!rammin! Inter ace% <asically this is the code that you use to communicate (ith the !raphics hard(are% When you are communicatin! (ith the ;PI$ you are !oin! to ha&e to e*press your trans ormations as matrices e&entually% @o( you store trans ormations internally in your pro!ram is up to you$ but i you choose another representation you are !oin! to ha&e to con&ert them into matrices at some point in the !raphics pipeline%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,C

;d&anta!es o 3atri* Form #


Concatenation of multiple angular displacements. It is possible to collapse nested coordinate space relationships% For e*ample$ i (e kno( the orientation o ob"ect ; relati&e to ob"ect <$ and (e kno( the orientation o ob"ect < relati&e to ob"ect C$ then usin! matrices$ (e can determine the orientation o ob"ect ; relati&e to ob"ect C% We ha&e encountered these concepts be ore$ (hen (e learned about nested coordinate spaces in Chapter #$ and then discussed ho( matrices could be concatenated in Chapter .%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,'

;d&anta!es o 3atri* Form ,


Matrix inversion. When an an!ular displacement is represented in matri* orm$ it is possible to compute the opposite an!ular displacement usin! matri* in&ersion% WhatEs more$ since rotation matrices are ortho!onal$ this computation is a tri&ial matter o transposin! the matri*%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ,)

Disad&anta!es o 3atri* Form '


Matrices take more memory. I (e need to store many orientations = or e*ample$ key rames in an animation se/uence>$ that e*tra space or nine numbers instead o three can really add up% DetEs take a modest e*ample% DetEs say (e are animatin! a model o a human that is broken up into '. pieces or di erent body parts% ;nimation is accomplished strictly by controllin! the orientation o each part relati&e to its parent part% ;ssume (e are storin! one orientation or each part$ per rame$ and our animation data is stored at a reasonable rate$ say$ '.hF% This means (e (ill ha&e )). orientations per second% Isin! matrices and #)-bit loatin! point numbers$ each rame (ill take 8$'CC bytes% Isin! +uler an!les =(hich (e (ill meet ne*t>$ the same data (ould only take )1CC bytes% For a mere #C seconds o animation data$ matrices (ould take '0)P more than the same data stored usin! +uler an!lesQ

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,#

Disad&anta!es o 3atri* Form )


ifficult for humans to use. 3atrices are not intuiti&e or humans to (ork (ith directly% There are "ust too many numbers$ and they are all rom G' to '% WhatEs more$ humans naturally think about orientation in terms o an!les$ but a matri* is e*pressed usin! &ectors% With practice$ (e can learn ho( to decipher the orientation rom a !i&en matri*% =;lthou!h the techni/ues rom Chapter , or &isualiFin! a matri* help a lot%> <ut still this is much more di icult than +uler an!les$ and !oin! the other (ay is much more di icult

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,,

Disad&anta!es o 3atri* Form #


Matrices can be malformed. ; matri* uses nine numbers$ (hen only three are necessary% In other (ords$ a matri* contains si* de!rees o redundancy% There are si* constraints that must be satis ied in order or a matri* to be &alid or representin! an orientation% The ro(s must be unit &ectors$ and they must be mutually perpendicular%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ,.

@o( Can 3atrices 4et 3al ormed:


We may ha&e a matri* that contains scale$ ske($ re lection$ or pro"ection% ;ny non-ortho!onal matri* is not a (ell-de ined rotation matri*% Re lection matrices =(hich are ortho!onal> are not &alid rotation matrices either% We may "ust !et bad data rom an e*ternal source% For e*ample$ i (e are usin! a physical data ac/uisition system$ such as motion capture$ there could be errors due to the capturin! process% 3any modelin! packa!es are notorious or producin! mal ormed matrices% We can actually create bad data due to loatin! point roundo error% For e*ample$ suppose (e apply a lar!e number o incremental chan!es to an orientation$ (hich could routinely happen in a !ame or simulation that allo(s a human to interacti&ely control the orientation o an ob"ect% The lar!e number o matri* multiplications$ (hich are sub"ect to limited loatin! point precision$ can result in an ill- ormed matri*% Recall our discussion o matrix creep in Chapter 0%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,0

Summary o 3atri* Form '


3atrices are a brute orce method o e*pressin! orientation6 (e e*plicitly list the basis &ectors o one space in the coordinates o some di erent space% The matri* orm o representin! orientation is use ul primarily because it allo(s us to rotate &ectors bet(een coordinate spaces% 3odern !raphics ;PIs e*press orientation usin! matrices% We can use matri* multiplication to collapse matrices or nested coordinate spaces into a sin!le matri*%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,1

Summary o 3atri* Form )


3atri* in&ersion pro&ides a mechanism or determinin! the opposite an!ular displacement% 3atrices take t(o to three times as much memory as the other techni/ues (e (ill learn% This can become si!ni icant (hen storin! lar!e numbers o orientations$ such as animation data% The numbers in a matri* arenEt intuiti&e or humans to (ork (ith% 2ot all matrices are &alid or describin! an orientation% Some matrices contain mirrorin! or ske(% We can end up (ith a mal ormed matri* either by !ettin! bad data rom an e*ternal source$ or throu!h matri* creep%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,8

Section 8%#6

+uler ;n!les

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

,B

+uler ;n!les
+uler an!les are another common method o representin! orientation% +uler is pronounced 7oiler$9 not 7yoolur%9 They are named or the amous mathematician (ho de&eloped them$ Deonhard +uler ='1C1 G'18#>% =Ima!e rom Wikimedia Commons%>

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

.C

+uler ;n!les
Speci y orientation as a series o # an!ular displacements rom upri!ht space to ob"ect space% Which a*es: Which order: 2eed a con&ention% @eadin!-pitch-bank
@eadin!6 rotation about y a*is =aka 7ya(9> Pitch6 rotation about x a*is <ank6 rotation about $ a*is =aka 7roll9>

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

.'

Implementin! +uler ;n!les


+ach !ame ob"ect keeps track o its current headin!$ pitch$ and bank an!les =the 7+uler an!les9>% It also keeps track o its headin!$ pitch$ and bank chan!e rate% In each rame$ calculate ho( much the ob"ect has chan!ed its headin!$ pitch$ and bank based on the amount o time since the last rame$ and add this to the +uler an!les%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& .)

@eadin!

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

.#

Pitch

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

.,

<ank

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

..

The Si!n 3atters


Ise the hand rule a!ain% Thumb points alon! positi&e a*is o rotation% Fin!ers curl in direction o positi&e rotation%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

.0

The 8rder 3atters


@eadin! is irst6 it is relati&e to the upri!ht rame o re erence G that is$ &ertical% Pitch is ne*t because it is relati&e to the horiFon% <ut the x-a*is may ha&e been mo&ed by the headin! chan!e% =8b"ect x is no lon!er the same as upri!ht x%> <ank is last% The $-a*is may ha&e been mo&ed by the headin! and pitch chan!e% =8b"ect $ is no lon!er the same as upri!ht $%>
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& .1

Whats in a 2ame:
@eadin!-pitch-bank is o ten called yaw%pitch%roll =at the time o (ritin!$ the latter has a Wikipedia pa!e but the ormer doesnt> @eadin! is ya($ bank is roll% This came rom the aerospace industry$ (here ya( in act !oesn&t mean headin! the (ay (e interpret it% 8ther less common terms are o ten used%
'ea!ing also !oes by the name a$im th% Pitch is also called attit !e or elevation% (an) is sometimes called tilt$ or twist%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& .8

<ack to Ya(-pitch-roll
Perhaps more interestin! is that act that you (ill o ten hear these same three (ords listed in the opposite order6 rollpitch-ya(% ;s it turns out$ there is a per ectly reasonable e*planation or this back(ards con&ention6 itEs the order that (e actually do the rotations inside a computerQ
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& .B

The Fi*ed-;*is System


In an +uler an!le system$ the a*es o rotation are the body a*es$ (hich chan!e a ter each rotation% In a fixe!%axis system$ the a*es o rotation are the upri!ht space a*es%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 0C

+*ample
The i*ed-a*is system and the +uler an!le system are e/ui&alent$ pro&ided that you take the rotations in the opposite order% DetEs say (e ha&e a headin! =ya(> o h and a pitch o p% ;ccordin! to the +uler an!le con&ention6
We irst do the headin! and rotate about the ob"ect space y-a*is by h% Then (e rotate about the ob"ect space x-a*is by p%

Isin! a i*ed-a*is scheme (e !et to this same endin! orientation by doin! the rotations in the opposite order%
First$ (e do the pitch$ rotatin! about the upri!ht x-a*is by p% Then$ (e per orm the headin!$ rotatin! about the upri!ht y-a*is by h%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

0'

3ore Dater
;lthou!h (e mi!ht &isualiFe +uler an!les$ inside a computer (hen rotatin! &ectors rom upri!ht space to ob"ect space$ (e (ill actually use a i*ed-a*is system% WeEll discuss this in !reater detail (hen (e learn ho( to con&ert +uler an!les to a rotation matri*%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

0)

;d&anta!es o +uler ;n!les


+asy or humans to use% Really the only option i you (ant to enter an orientation by hand% 3inimal space G # numbers per orientation% <ottom line6 i you need to store a lot o #D rotational data in as little memory as possible$ as is &ery common (hen handlin! animation data$ +uler an!les =or e*ponential map ormat G to be discussed later> are the best choices% ;nother reason to choose +uler an!les (hen you need to sa&e space is that the numbers you are storin! are more easily compressed% +&ery set o # numbers makes sense G unlike matrices and /uaternions%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 0#

Disad&anta!es o +uler ;n!les


;liasin! 4imbal =or !ymbal> lock Interpolation problems

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

0,

;liasin!
There are many (ays to represent a sin!le orientation$ e!% Pitch do(n '#. is the same as headin! '8C $ pitch do(n ,. $ then bank '8C % This is called the aliasing problem% 3akes it hard to con&ert orientations rom ob"ect to (orld space$ e!% 7;m I acin! +ast:9
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 0.

Canonical +uler ;n!les


Dimit headin! =y> to '8C Dimit pitch =x> to BC Dimit bank =$> to '8C 2o( each orientation has a uni/ue canonical +uler an!le% +*cept or one more irritatin! thin!%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

00

4imbal Dock
Chan!e headin! =y a*is> by ,. Pitch do(n BC 2o( i you bank$ your ob"ect space $ a*is is pointin! in (orld space (here your ob"ect space y a*is (as be ore you started your rotations% So any rotation about $ could ha&e been done as a headin! chan!e%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 01

Canonical +uler ;n!les


Fi* this =i%e% make canonical +uler an!les uni/ue> by insistin! that i pitch is BC $ then bank must be Fero% Put the bank rotation in the headin! instead%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

08

@int or Pro!rammers
When (ritin! CNN that accepts +uler an!le ar!uments$ itEs usually best to ensure that they (ork !i&en +uler an!les in any ran!e% Duckily this is usually pretty easy$ and re/uently "ust automatically (orks (ithout takin! any e*tra precaution$ especially i the an!les are ed into tri! unctions% @o(e&er$ (hen (ritin! code that computes or returns +uler an!les$ itEs a !ood practice to try to return the canonical +uler an!le triple%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 0B

3ore on 4imbal Dock


It is a common misconception that$ because o 4imbal lock$ certain orientations cannot be described usin! +uler an!les% ;ctually$ or the purposes o describin! an orientation$ aliasin! doesnEt pose any problems% To be clear$ any orientation in #D can be described usin! +uler an!les$ and that representation is uni/ue (ithin the canonical set% ;lso$ as (e mentioned earlier$ there is no such thin! as an in&alid set o +uler an!les% +&en i the an!les are outside the usual ran!e$ (e can al(ays a!ree (hat orientation is described by the +uler an!les%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

1C

;liasin! and 4imbal Dock


WhatEs the uss about aliasin! and 4imbal lock: DetEs say (e (ish to interpolate bet(een t(o orientations RC and R'% That is$ or a !i&en parameter t$ C R t R '$ (e (ish to compute an intermediate orientation R=t> that interpolates smoothly rom RC to R' as t &aries rom C to '% This is e*tremely use ul or character animation and camera control$ or e*ample%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

1'

The Dan!ers o Derpin!


The nai&e approach to this problem is to apply the standard linear interpolation ormula =lerp or short> to each o the three an!les independently%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

1)

Problems
2ot !ood (ith noncanonical an!les% For e*ample$ to interpolate rom 1)C to ,. in ' increments (ould mean turnin! around t(ice%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 1#

What ;bout Canonical +uler ;n!les:


This can e&en happen (ith canonical +uler an!les$ or e*ample$ rom G '1C to '1C % It !oes the 7lon! (ay round9%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 1,

Function wrapPi
De ine the ollo(in! unction6 Isin! (rapPi=> makes it easy to take the shortest arc (hen interpolatin! bet(een t(o an!les%

@eres the code or (rapPi=> =ne*t slide>6


Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 1.

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

10

;nother Problem
+&en (ith these t(o band-aid solutions$ you are le t (ith !imbal lock% Solution6 dont use +uler an!les or interpolation% Ise somethin! else$ like /uaternions%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

11

JisualiFin! 4imbal Dock


I you ha&e ne&er e*perienced (hat 4imbal lock looks like$ you may be (onderin! (hat all the uss is about% Fortunately$ itEs easy to ind an animation demonstratin! the problem6 "ust do a YouTube search or 7!imbal lock%9 For e*ample$ http6SS(((%youtube%comS(atch:&TFc8b)?o 1mno
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 18

Summary o +uler ;n!les '


+uler an!les store orientation usin! three an!les% These an!les are ordered rotations about the three ob"ect-space a*es% The most common system o +uler an!les is the headin!-pitch-bank system% @eadin! and pitch tell (hich (ay the ob"ect is acin!$ (ith headin! !i&in! a compass readin! and pitch measurin! the an!le o declination% <ank measures the amount o t(ist% In a i*ed-a*is system$ the rotations occur about the upri!ht a*es rather than the mo&in! body a*es% This system is e/ui&alent to +uler an!les pro&ided that (e per orm the rotations in the opposite order% Dots o smart people use lots o di erent terms or +uler an!les$ and they can ha&e !ood reasons or usin! di erent con&entions% ItEs best not to rely on terminolo!y (hen usin! +uler an!les% ;l(ays make sure you !et a precise (orkin! de inition$ or youEre likely to !et &ery con used% In most situations$ +uler an!les are more intuiti&e or humans to (ork (ith compared to other methods o representin! orientation%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

1B

Summary o +uler ;n!les )


When memory is at a premium$ +uler an!les use the minimum amount o data possible or storin! an orientation in #D$ and +uler an!les are more easily compressed than /uaternions% There is no such thin! as an in&alid set o +uler an!les% ;ny three numbers ha&e a meanin! ul interpretation% +uler an!les su er rom aliasin! problems$ due to the cyclic nature o rotation an!les$ and because the rotations are not completely independent o one another% Isin! canonical +uler an!les can simpli y many basic /ueries on +uler an!les% ;n +uler an!le triple is in the canonical set i headin! and bank are in ran!e G '8C to '8C and pitch is in ran!e GBC to BC % WhatEs more$ i pitch is UBC $ then bank is Fero% 4imbal lock occurs (hen pitch is UBC % In this case$ one de!ree o reedom is lost because headin! and bank both rotate about the &ertical a*is%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

8C

Summary o +uler ;n!les #


Contrary to popular myth$ any orientation in #D can be represented usin! +uler an!les$ and (e can a!ree on a uni/ue representation or that orientation (ithin the canonical set% The (rapPi unction is a &ery handy tool that simpli ies situations (here (e ha&e to deal (ith the cyclic nature o an!les% Such situation arise re/uently in practice$ especially in the conte*t o +uler an!lers$ but at other times as (ell% Simple orms o aliasin! are irritatin!$ but there are (orkarounds% 4imbal lock is a more undamental problem and no easy solution e*ists% 4imbal lock is a problem because the parameter space o orientation has a discontinuity% This means small chan!es in orientation can result in lar!e chan!es in the indi&idual an!les% Interpolation bet(een orientations usin! +uler an!les can reak out or take a (obbly path%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

8'

Section 8%,6

;*is-;n!le and +*ponential 3ap

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

8)

+ulerEs Rotation Theorem


+ulerEs Rotation Theorem6 any #D an!ular displacement can be accomplished &ia a sin!le rotation about a care ully chosen a*is% That is$ or e&ery pair o orientations R' and R) there e*ists an a*is n such that that (e can !et rom R' to R) by per ormin! a rotation about n% +ulerEs Rotation Theorem leads to t(o closely-related methods or describin! orientation% We be!in (ith some notation% DetEs say (e ha&e chosen a rotation an!le V$ and an a*is o rotation that passes throu!h the ori!in and is parallel to the unit &ector n%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 8#

+*ponential 3aps
The t(o &alues n and V describe an an!ular displacement in axis% angle form% ;lternati&ely$ since n has unit len!th$ (ithout loss o in ormation (e can multiply it by V$ yieldin! the sin!le &ector e T Vn% This scheme or describin! rotation !oes by the rather intimidatin! and obscure name o exponential map% The rotation an!le can be deduced rom the len!th o e$ i%e% V T WWeWW$ and the a*is is obtained by normaliFin! e% The e*ponential map is not only more compact than a*is-an!le =# numbers instead o ,>$ it ele!antly a&oids certain sin!ularities and has better interpolation and di erentiation properties%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

8,

3ultiples o ;n!ular Displacements


We can directly obtain a multiple o an an!ular displacement% For e*ample$ !i&en a rotation in a*is-an!le orm$ (e can !et 'S#rd o the rotation$ or )%0. times the rotation$ simply by multiplyin! V by this amount% 8 course$ (e can do this same operation (ith the e*ponential map "ust as easily =by multiplyin! c>% Auaternions can do this throu!h e*ponentiation$ but an inspection o the math re&eals that itEs really usin! the a*is-an!le ormat under the hood% Auaternions can also do a similar operation usin! slerp$ but in a more roundabout (ay and (ithout the ability or intermediate results to store rotations beyond '8CK% WeEll look at /uaternions later%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 8.

+*ponential 3ap &s ;*is-;n!le


The e*ponential map !ets more use than a*is-an!le% Its interpolation properties are nicer than +uler an!les% ;lthou!h it does ha&e sin!ularities =to be discussed belo(>$ they are not as troublesome as +uler an!les% Isually (hen one thinks o interpolatin! rotations one immediately thinks o /uaternions$ but or some applications such as stora!e o animation data$ the under-appreciated e*ponential map is a &iable alternati&e% The most important and re/uent use o the e*ponential map is not or an!ular displacement$ but rather an!ular velocity$ because the e*ponential map di erentiates nicely =(hich is some(hat related to its nicer interpolation properties> and can represent multiple rotations easily%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 80

;liasin! and Sin!ularities


Dike +uler an!les$ the a*is-an!le and e*ponential map orms e*hibit aliasin! and sin!ularities$ althou!h o a sli!htly more restricted and beni!n manner% There is an ob&ious sin!ularity at the identity orientation$ (hen V T C and any a*is may be used% 2otice$ ho(e&er$ that the e*ponential map nicely tucks this sin!ularity a(ay$ since the multiplication by V causes e to &anish$% ;nother tri&ial orm o aliasin! in a*is-an!le space can be produced by ne!atin! both V and n% @o(e&er$ the e*ponential map dod!es this issue as (ell$ since ne!atin! both V and n lea&es e T Vn unchan!edQ
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 81

8ther ;liases
The other aliases cannot be dispatched so easily% ;s (ith +uler an!les$ addin! a multiple o #0CK to V produces an an!ular displacement that results in the same orientation$ in both the a*is-an!le and the e*ponential map% @o(e&er$ this is not al(ays a shortcomin! G or describin! an!ular displacement$ this ability to represent such e*tra rotation is an important and use ul property% For e*ample$ itEs /uite important to be able to distin!uish bet(een rotation about the xa*is at a rate o 1)CK per second$ &ersus rotation about the same a*is at a rate o 'C8CK per second$ e&en thou!h these displacements result in the same endin! orientation i applied or an inte!ral number o seconds% It is not possible to capture this distinction in /uaternion ormat%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

88

3ore on the +uler Rotation Theorem%


For any an!ular displacement described as a rotation matri*$ there is a uni/ue e*ponential map representation% ;lthou!h more than one e*ponential map may produce the same rotation matri*$ it is possible to take a subset o the e*ponential maps =those or (hich WW eWW X )Y> and orm a one-to-one correspondence (ith the rotation matrices% This is the essence o the +uler Rotation Theorem%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

8B

Concatenatin! Rotations
Suppose e' and e) are rotations in e*ponential map ormat% The result o per ormin! the rotations in se/uence$ or e*ample e' then e)$ is not the same as per ormin! the rotation e' N e)$ because &ector addition is commutati&e$ but three-space rotations are not% For e*ample$ suppose that e' is a BCK do(n(ard pitch rotation$ and e) is a BCK +ast(ard headin! rotation$ that is$ e' T LBCK$ C$ CM$ and e) T LC$ BCK$ CM% Per ormin! e' ollo(ed by e)$ (e (ould end up lookin! do(n(ard (ith our head pointin! +ast$ but doin! them in the opposite order$ (e end up on our ear acin! +ast%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

BC

Concatenatin! Small Rotations


@o(e&er$ i the an!les (ere much smaller$ say )K instead o BCK then the endin! orientations (ould be closer% ;s (e reduce the ma!nitude o the rotation an!les$ the importance o the order decreases$ and at the e*treme$ or in initesimal rotations$ the order is completely irrele&ant% @ence or in initesimal rotations$ (hich are use ul or describin! an!ular &elocity$ e*ponential maps can be added &ectorially$

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

B'

Terminolo!y Disclaimer
;lternati&e names or these concepts abound% We ha&e tried to choose the most standard names (e could$ but it (as di icult to ind any consensus in the literature% Some authors use the term axis%angle to describe both a*is-an!le and e*ponential map and donEt really distin!uish bet(een them% +&en more con usin! is the use o the term * ler axis to re er to either orm =but not to +uler an!lesQ>% +otation vector is another term you mi!ht see attached to (hat (e are callin! exponential map% Finally$ the term exponential map$ in the broader conte*t o Die al!ebra$ rom (hence the term ori!inates$ actually re ers to an operation =a map> rather than a /uantity%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& B)

Section 8%.6

Auaternions

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

B#

Auaternions
Auaternions (ere in&ented by the Irish mathematician Sir William Ro(an @amilton in '8,#% They ha&e been the cause o much puFFlement to students since then% =Ima!e rom Wikimedia Commons%>

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

B,

When Auaternions Were In&ented


8n the '0th o 8ctober$ '8,#$ @amilton le t the Dunsink 8bser&atory in Dublin =(here he li&ed and (as ser&in! as Director>$ and (alked alon! the Royal Canal% @is destination (as the Royal Irish ;cademy at 'B Da(son Street in the center o Dublin% =Ima!e rom Wikimedia Commons%>

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

B.

2erd ;lertQ
8n his (ay$ as he (as passin! o&er <room =<rou!hamS<roome> <rid!e$ he disco&ered the undamental /uaternion ormula6 i) T #) T )) T i#) T G'% @e (as so e*cited about this that he scratched the ormula onto the brid!e (ith his penkni e% 2o trace can be ound today$ but in 'B.8 a pla/ue (as erected on the site%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& B0

=Ima!e rom Wikimedia Commons%>


Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& B1

Auaternions Jersion '6

Comple* 2umber 2otation


This notation is "ust like comple* numbers$ but (ith three ima!inary parts$ i, #, )% / T w - xi - y# - $). @eres ho( the ima!inary parts interact6 i) T #) T )) T i#) T G' i# / ), #) / i, )i / # #i / G), )# / Gi, i) / G# This is (hat @amilton scratched on the brid!e%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& B8

Auaternions Jersion )6

Jector-Scalar 2otation
Instead o the ima!inary number notation6 w - xi - y# - $) use &ector-scalar notation6 ! / L w v M$ (here w is a scalar and v a &ector% ;lternati&ely6 !/Lw=xy$>M =its the same w, x, y, $>.
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& BB

Auaternions Jersion #6

,D Space
@amilton himsel thou!ht o /uaternions as ,D &ectors Lw, x, y, $M% @o(e&er$ /uaternions are not the same as &ectors in homo!enous ,D space Lx, y, $, wM% In particular$ his w is not the same as the w in homo!enous ,D space%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 'CC

What (as @amilton Thinkin!:


@e (as lookin! or a (ay to e*tend comple* numbers rom )D into #D% Thats the normal comple* numbers you&e probably met be ore$ c T x N yi (here i) T G' Whats the link bet(een comple* numbers and )D !eometry:
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 'C'

)D and Comple* 2umbers


Instead o thinkin! o a point =x, y> in )D space as t(o real numbers$ think o it as a sin!le comple* number x - yi% ; rotation by an!le can be also be represented as a comple* number cos N i sin Comple* numbers are doin! double duty here as both points and rotations%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 'C)

Comple* 3ultiplication

; rotation =represented as a comple* number> can be applied to a point =also represented as a comple* number> usin! multiplication o comple* numbers% =x - yi> =cos N i sin > T =x cos G y sin > N i =x sin
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De&

N y cos >
'C#

Whats 4oin! 8n @ere:


Remember the )D rotation matri*% The ima!inary part captures the ne!ati&e si!n on the sine =so to speak>%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'C,

What Do Ima!inary 2umbers <uy Is:


The ima!inary part seems to ha&e t(o roles% '% It allo(s us to 7"am to!ether9 the x and y part rom a &ector Lx, yM into a sin!le number x N yi% The ima!inary part i keeps the x and y parts separate% )% It !ets us the si!n chan!e on the sin that (e sa( in rotation matrices%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 'C.

So What Was @e Thinkin!:


@amilton (anted to e*tend this to #D rotations% @is intuition told him that he needed ) ima!inary parts i and #% That makes sense$ ' ima!inary part or )D$ and ) ima!inary parts or #D% @e !ot i*ated on that% For some reason his mental block cleared on the brid!e6 # ima!inary parts is the (ay to !o%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'C0

In the <ack o @is 3ind


It (as kno(n as ar back as +uler that a*ial rotations are closed under composition% What does this mean: I an ob"ect rotates around t(o or more a*es simultaneously$ the result is a rotation about a sin!le a*is%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'C1

Points as Auaternions
Represent #D point = x y $ > as the /uaternion p T L C = x y $ > M% 8r e/ui&alently in comple* number notation6 xi - y# - $). 4ames !enerally dont implement the C &alue or w%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& 'C8

Rotations as Auaternions
The scalar part 7kind o 9 represents the an!le% The &ector part 7kind o 9 represents the a*is% Rotation by an an!le around unit a*is n is represented by the /uaternion6 " T L cos S) T L cos S)
Chapter 8 2otes

sin S) n M

sin S) =nx ny n$>M


'CB

#D 3ath Primer or 4raphics 5 4ame De&

Auaternion 2e!ation
I ! T L( =* y F>M T L( vM is a /uaternion$ de ine its ne!ation to be6 G! T GL( =* y F>M T LG( =G* Gy GF>M T GL( vM T LG( GvM% Surprisin!ly$ ! T G!% The /uaternions ! and G! describe the same an!ular displacement% ;ny an!ular displacement in #D has e*actly t(o distinct representations in /uaternion ormat$ and they are ne!ati&es o each other% ItEs not too di icult to see (hy% I (e add #0CK to $ it doesnEt chan!e the an!ular displacement represented by !$ but it ne!ates all our components o !%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

''C

Auaternion 3a!nitude
The ma!nitude o a /uaternion is

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'''

Rotation Auaternion 3a!nitude


The ma!nitude o a rotation /uaternion is '%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'')

Con"u!ate and In&erse


The con# gate o a /uaternion is obtained by re&ersin! the &ector part% !Z T L w v MZ T Lw Gv M% The in&erse o a /uaternion is its con"u!ate di&ided by its ma!nitude% !-' T !ZSWW ! WW% For unit /uaternions$ in particular rotation /uaternions$ con"u!ate is the same as in&erse L w v MZ T L w v M-'% The in&erse o a rotation /uaternion is a rotation in the opposite direction%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ''#

Auaternion 3ultiplication

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'',

Facts ;bout Auaternion 3ultiplication


It is associati&e but not commutati&e% !=rs> T =!r>s !r r! 3ultiplicati&e identity is L' #M T L' = C C C >M WW!rWW T WW!WW WWrWW$ so unit /uaternions are closed under multiplication% =ab>-' T b-'a-'$ and in !eneral ="ust like matrices> =!'!)H!n-'!n>G' T !nG'!n-'G'H!)G'!'G'

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

''.

Auaternion In&erse ;!ain


;ssume L w v M is a unit /uaternion =same kind o ar!ument holds or !eneral /uaternions>% Then$ L w v M L w v M-' T L w v M L w 0v M T L w) 0 v.=0v> v x =0v> N wv 0 wv M #M T Lw) - x) - y) - $)

T L ' # M =because its a unit /uaternion>

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

''0

;pplyin! Rotations to Points


To apply a rotation /uaternion ! to a #D point p$ use /uaternion multiplication =thinkin! o p or a moment as the /uaternion L' pM>6 !p!-'% +*amine (hat happens (hen multiple rotations are applied to a &ector% Rotate the &ector p by the /uaternion a$ and then rotate that result by another /uaternion b% pE T b=apa-'>b-' T =ba>p=a-'b-'> T =ba>p=ba>-'%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ''1

Concatenatin! Rotations
2otice that rotatin! by a and then by b is e/ui&alent to per ormin! a sin!le rotation by the /uaternion product ba% Thus$ /uaternion multiplication can be used to concatenate multiple rotations$ "ust like matri* multiplication% We say 7"ust like matri* multiplication$9 but in act there is a sli!htly irritatin! di erence%
With matri* multiplication$ our pre erence to use ro( &ectors puts the &ectors on the le t$ resultin! in the nice property that concatenated rotations read le tto-ri!ht in the order o trans ormation% With /uaternions$ (e donEt ha&e this le*ibility6 concatenation o multiple rotations (ill al(ays read rom ri!ht to le t%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

''8

8rder o 3ultiplication
This means that to apply a rotation /uaternion ! ollo(ed by a rotation /uaternion r$ (e apply the product /uaternion !r6 r-'=!-'p !>r T =!r>-'p =!r> This is cool because /uaternion multiplication is done the same order as the trans ormations% <ooks that use column &ectors o ten de ine /uaternion product back(ards G re&ersin! the order o the cross product G to make the order natural or them%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ''B

Ro( &s% Column Jectors


Auaternion multiplication or ro( &ectors6 L 1wrw 0 !v.rv !v x rv N rw !v N 1w rv M Auaternion multiplication or column &ectors6 L 1wrw 0 !v.rv rv x !v N rw !v N 1w rv M Some books do it in the (ron! order and "ust put up (ith the incon&enience% 3oral6 2aveat *mptor
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ')C

Auaternion Di erence
Det a and b be /uaternions representin! t(o orientations% The /uaternion d that takes orientation a to orientation b is called the 1 aternion !ifference bet(een a and b% Want ad T b% What is d: d T a-'b Why: ad T a=a-'b> T =aa-'>b T L ' # M b T b
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ')'

Auaternion Dot Product


Similar to &ector dot product6 !'%!) T L w' v' M % L w) v) M T w' w)- v'%v) I v' T L x' y' $' M and v) T L x) y) $) M$ then6 !'%!) T w' w) - x' x) - y' y) - $' $) 4eometric interpretation6 the lar!er the absolute &alue o !'%!)$ the more similar their orientations are%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '))

Auaternion Do!
;s a shorthand$ let [ T 34. Det n be a unit &ector% Suppose /uaternion ! T Lcos [ n sin [M The logarithm o !$ denoted lo! !$ is de ined to be LC$ [nM% 2ote that lo! ! is not necessarily a unit /uaternion% ;lso note the similarity bet(een the logarithm of a 1 aternion and the exponential map format%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

')#

Auaternion +*ponential
The e*ponential unction or /uaternions is de ined in the e*act opposite manner% Suppose p T LC$ [nM$ (here n is a unit &ector% The e*ponential o p$ denoted e*p p$ is de ined to be Lcos [ n sin [M% 2ote that e*p p is al(ays a unit /uaternion% Auaternion lo! and e*ponential are in&erses$ that is$ or all /uaternions !$ e*p=lo! !> T lo!=e*p !> T !%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '),

3ultiplyin! a Auaternion by a Scalar


4i&en a scalar ) and a /uaternion ! T Lw vM$ )! T )Lw vM T L)w )vM% This (ill not usually result in a unit /uaternion$ (hich is (hy multiplication by a scalar is not or representin! an!ular displacement%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ').

Auaternion +*ponentiation
We used the term 7e*ponential9 be ore$ no( its time or e*ponentiation% I ! is a /uaternion and t is a scalar$ de ine !t T e*p =t lo! !>% ;s t &aries rom C to '$ the /uaternion !t &aries rom L'$ #M to !% Auaternion e*ponentiation is use ul because it allo(s us to e*tract a raction o an an!ular displacement% For e*ample$ !'S# is a /uaternion that represents 'S#rd o the an!ular displacement represented by the /uaternion !%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

')0

Facts ;bout Auaternion +*ponentiation


+*ponents t outside the ran!e C R t R ' beha&e mostly as e*pected$ (ith one ma"or ca&eat% For e*ample$ !) represents t(ice the an!ular displacement as !% I ! represents a clock(ise rotation o #CK about the x-a*is$ then !) represents a clock(ise rotation o 0CK about the x-a*is$ and !0'S# represents a counterclock(ise rotation o 'CK about the x-a*is% 2otice that !0' yields the /uaternion in&erse%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ')1

The Ca&eat
The ca&eat (e mentioned is this6 a /uaternion represents an!ular displacements usin! the shortest arc% 3ultiple spins cannot be represented% For e*ample$ i ! represents a clock(ise rotation o #CK about the xa*is$ then !8 is not a ),CK clock(ise rotation about the *-a*is as e*pectedO it is a ')CK counterclock(ise rotation% 8 course$ rotatin! ),CK in one direction produces the same end result as rotatin! ')CK in the opposite direction$ and this is the point6 1 aternions only really capt re the en! res lt.

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

')8

In Conse/uence
I urther operations on this /uaternion (ere per ormed$ thin!s (ill not beha&e as e*pected% For e*ample$ =!8>'S) is not !,$ as (e (ould intuiti&ely e*pect% In !eneral$ many o the al!ebraic identities concernin! e*ponentiation o scalars$ such as =as>t T ast$ do not apply to /uaternions% In some situations$ (e do care about the total amount o rotation$ not "ust the end result% The most important e*ample is that o an!ular &elocity% Auaternions are not the correct tool or this "obO use the e*ponential map =or its cousin$ the a*is-an!le ormat> instead%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

')B

Implementation 2otes
@eres (hy !t interpolates rom the identity /uaternion to ! as t &aries rom C to '%
The lo! operation essentially con&erts the /uaternion to e*ponential map ormat% =+*cept or a actor o )%> Then$ (hen (e per orm the scalar multiplication by the e*ponent t$ the e ect is to multiply the an!le by t% Finally$ the e*p undoes (hat the lo! operation did$ recalculatin! the ne( w and v rom the e*ponential &ector%

Direct implementation o the ormula !t T e*p =t lo! !> as an al!orithm or computin! !t can !i&e rise to code that is more complicated than necessary% Instead o (orkin! (ith a sin!le e*ponential-map-like /uantity like the ormula tells us to$ (e (ill break out the a*is and hal -an!le separately%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '#C

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'#'

2otes ;bout Code '


The check or the identity /uaternion is necessary since a &alue o ( T ' (ould cause the computation o mult to di&ide by Fero%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '#)

2otes ;bout Code )


Raisin! an identity /uaternion to any po(er results in the identity /uaternion$ so i (e detect an identity /uaternion on input$ (e simply i!nore the e*ponent and return the ori!inal /uaternion%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'##

2otes ;bout Code #


We use the arccos unction$ (hich al(ays returns a positi&e an!le$ to compute alpha% ;ny /uaternion can be interpreted as ha&in! a positi&e an!le o rotation$ since ne!ati&e rotation is the same as positi&e rotation about the opposite a*is%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'#,

Auaternion Interpolation
The raison !5etre o /uaternions in !ames and !raphics today is an operation kno(n as slerp$ (hich stands or spherical linear interpolation% Slerp allo(s us to smoothly interpolate bet(een t(o orientations (hile a&oidin! all the problems that pla!ued interpolation o +uler an!les% Slerp is a ternary operator$ meanin! it accepts three operands$ ) /uaternions and a scalar%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'#.

The Slerp Function


The irst t(o operands the t(o /uaternions !C and !' bet(een (hich (e (ish to interpolate% The third operand is the interpolation parameter$ a real number t such that C R t R '% ;s t &aries rom C to '$ the slerp unction slerp= !C$ !'$ t> (ill return an orientation that interpolates rom !C to !' by raction t%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'#0

Scalar Dinear Interpolation


To interpolate bet(een t(o scalar &alues aC and a'$ use the standard linear interpolation ormula6 \a T a' G aC lerp=aC$ a'$ t> T aC N t%\a This re/uires three basic steps6 '% Compute the di erence bet(een the t(o &alues )% Take a raction o this di erence #% Take the irst &alue and ad"ust it by this raction o the di erence%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '#1

Slerp <y ;nalo!y to Derp


We use the same idea to interpolate bet(een orientations% Recall that /uaternion multiplication reads ri!ht-to-le t% '% Compute the di erence bet(een the t(o &alues% The an!ular displacement rom !C to !' is !i&en by \! T !'!C-'% #% Take a raction o this di erence usin! /uaternion e*ponentiation% The raction o the di erence is !i&en by =\!>t% .% Take the ori!inal &alue and ad"ust it by this raction o the di erence by composin! the an!ular displacements usin! /uaternion multiplication$ !i&in! =\!>t !C%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '#8

The Slerp +/uation


Puttin! these steps to!ether$ slerp=!C$ !'$ t> T =!'!C-'>t !C% This is ho( slerp is computed in theory% In practice$ a more e icient techni/ue is used% We start by interpretin! the /uaternions as e*istin! in a ,D space% We are only interested in unit /uaternions$ (hich li&e on the sur ace o a ,D hypersphere% We interpolate around the arc that connects the t(o /uaternions$ alon! the sur ace o the ,D hypersphere% @ence the name spherical linear interpolation%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '#B

JisualiFe It in the Plane


4i&en unit len!th )D &ectors vC and v'$ compute vt$ the result o smoothly interpolatin! around the arc by a raction t o the distance rom vC to v'$ or some real number t such that C R t R '% I (e let ] be the an!le intercepted by the arc rom vC to v'$ then vt is the result o rotatin! vC around this arc by an an!le o t]%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',C

JisualiFe It in the Plane


+*press vt as a linear combination o vC and v'% That is$ ind nonne!ati&e constants )C and )' such that vt T )C vC N )' v'% Ise elementary !eometry to determine the &alues o )C and )' as in this dia!ram%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

','

Sol&in! or )'
;pplyin! some tri! to the ri!ht trian!le (ith hypotenuse )' v' =recallin! that v' is a unit &ector>$ (e see that6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',)

Continuin!
Similarly$ ;nd there ore$

4eneraliFin! to /uaternion space6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',#

Computin! ]
We need a (ay to compute ]$ the an!le bet(een the t(o /uaternions% ;s it turns out$ the analo!y rom )D &ector math can be carried into /uaternion spaceO the /uaternion dot product is cos ]%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',,

Complication '
There are t(o sli!ht complications% First$ the t(o /uaternions ! and G! represent the same orientation$ but may produce di erent results (hen used as an ar!ument to slerp% This problem doesnEt happen in )D or #D because the sur ace o a ,D hypersphere has a di erent topolo!y than +uclidian space% The solution is to choose the si!ns o !C and !' such that the dot product !C^!'is nonne!ati&e% This has the e ect o al(ays selectin! the shortest rotational arc rom !C to !' %
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ',.

Complication )
The second consideration is that i !C and !' are &ery close$ then ] is &ery small$ and thus sin ] is also &ery small$ (hich (ill cause problems (ith the di&ision%

To a&oid this$ i sin ] is &ery small$ then use simple linear interpolation instead% 2e*t6 some C codeH
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& ',0

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',1

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',8

;d&anta!es o Auaternions
$mooth interpolation% The interpolation pro&ided by slerp pro&ides smooth interpolation bet(een orientations% 2o other representation method pro&ides or smooth interpolation% Fast concatenation and inversion of angular displacements. We can concatenate a se/uence o an!ular displacements into a sin!le an!ular displacement usin! the /uaternion cross product% The same operation usin! matrices in&ol&es more scalar operations$ thou!h (hich one is actually aster on a !i&en architectures is not so clean-cut6 SI3D &ector operations can make &ery /uick (ork o matri* multiplication% Auaternion con"u!ate pro&ides a (ay to compute the opposite an!ular displacement &ery e iciently% This can be done by transposin! a rotation matri*$ but is not easy (ith +uler an!les% Fast conversion to and from matrix form % ;s (e (ill see later$ /uaternions can be con&erted to and rom matri* orm a bit aster than +uler an!les% %nly four numbers% Since a /uaternion contains our scalar &alues$ it is considerably more economical than a matri*$ (hich uses nine numbers% =@o(e&er$ it still is ##_ lar!er than +uler an!les%>

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

',B

Disad&anta!es o Auaternions
@o(e&er$ these ad&anta!es do come at some cost% Auaternions su er rom a e( o the problems that a ect matrices$ only to a lesser de!ree6 $lightly bigger than &uler angles% That one additional number may not seem like much$ but an e*tra ##_ can make a di erence (hen lar!e amounts o an!ular displacements are needed$ or e*ample$ (hen storin! animation data% ;nd the &alues inside a /uaternion are not e&enly spaced rom G' to ' so the component &alues do not interpolate smoothly e&en i the orientation does% This makes /uaternions more di icult to pack into a i*ed point number than +uler an!les or an e*ponential map% Can become invalid% This can happen either throu!h bad input data$ or rom accumulated loatin! point round o error% =We can o course normaliFe the /uaternion to ensure that it has unit ma!nitude%> ifficult for humans to 'ork 'ith% 8 the three representation methods$ /uaternions are the most di icult or humans to (ork (ith directly%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.C

Section 8%06

Comparison o 3ethods

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.'

Comparison o 3ethods '


Rotatin! points bet(een coordinate spaces =ob"ect and upri!ht> Matrix6 Possible$ and o ten hi!hly optimiFed by &ector instructions% &uler Angles6 Impossible =must con&ert to rotation matri*> &xponential Map6 Impossible =must con&ert to rotation matri*> "uaternion6 8n a chalkboard$ yes% Practically$ in a computer$ not really% You mi!ht as (ell con&ert to rotation matri*% Concatenation o multiple rotations Matrix6 Possible$ and can o ten be hi!hly optimiFed by &ector instructions% Watch out or matri* creep% &uler Angles6 Impossible% &xponential Map6 Impossible% "uaternion6 Possible% Fe(er scalar operations than matri* multiplication$ but mi!ht not as easy to take ad&anta!e o SI3D instructions% Watch out or error creep%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.)

Comparison o 3ethods )
In&ersion o rotations Matrix6 +asy and ast$ usin! matri* transpose &uler Angles6 2ot easy% &xponential Map6 +asy and ast$ usin! &ector ne!ation "uaternion6 +asy and ast$ usin! /uaternion con"u!ate Interpolation Matrix6 +*tremely problematic &uler Angles6 Possible$ but 4imbal lock causes /uirkiness &xponential Map6 Possible$ (ith some sin!ularities$ but not as troublesome as +uler an!les% "uaternion6 Slerp pro&ides smooth interpolation
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '.#

Comparison o 3ethods #
Direct human interpretation Matrix6 Di icult &uler Angles6 +asiest &xponential Map6 Jery di icult "uaternion6 Jery di icult Storin! in a memory or in a ile Matrix6 2ine numbers &uler Angles6 Three numbers that can be easily /uantiFed &xponential Map6 Three numbers that can be easily /uantiFed "uaternion6 , numbers that dont /uantiFe (ellO can be reduced to # by assumin! unit len!th and that ,th component is nonne!ati&e%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '.,

Comparison o 3ethods ,
Ini/ue representation or a !i&en rotation Matrix6 Yes &uler Angles6 2o$ due to aliasin! &xponential Map6 2o$ due to aliasin!O less complicated than +uler an!les% "uaternion6 +*actly t(o distinct representations or any an!ular displacement$ and they are ne!ati&es o each other Possible to become in&alid Matrix6 Si* de!rees o redundancy inherent in ortho!onal matri*% 3atri* creep can occur% &uler Angles6 ;ny three numbers can be interpreted unambi!uously &xponential Map6 ;ny three numbers can be interpreted unambi!uously "uaternion6 +rror creep can occur

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'..

@o( to Choose Representation '


+uler an!les are easiest or humans to (ork (ith% Isin! +uler an!les !reatly simpli ies human interaction (hen speci yin! the orientation o ob"ects in the (orld% This includes direct keyboard entry o an orientation$ speci yin! orientations directly in the code =e%!% positionin! the camera or renderin!>$ and e*amination in the debu!!er% This ad&anta!e should not be underestimated% Certainly donEt sacri ice ease o use in the name o optimiFation until you are certain that your optimiFation (ill make a di erence in practice%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.0

@o( to Choose Representation )


3atri* orm must e&entually be used i &ector coordinate space trans ormations are needed% @o(e&er$ this doesnEt mean you canEt store the orientation usin! another ormat and then !enerate a rotation matri* (hen you need it% ;n alternati&e solution is to store the main copy o the orientation usin! +uler an!les or a /uaternion$ but also maintain a matri* or rotations$ recomputin! this matri* anytime the main copy chan!es%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '.1

@o( to Choose Representation #


For stora!e o lar!e numbers o orientations =e%!% animation data>$ +uler an!les$ e*ponential maps$ and /uaternions o er &arious tradeo s% In !eneral$ the components o +uler an!les and e*ponential maps /uantiFe better than /uaternions% It is possible to store a rotation /uaternion in only three numbersO by assumin! the ourth component is nonne!ati&e and the /uaternion has unit len!th it can be computed rom the three that are stored%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.8

@o( to Choose Representation #


Reliable /uality interpolation can only be accomplished usin! /uaternions% @o(e&er$ e&en i you are usin! a di erent orm you can al(ays con&ert to /uaternions$ per orm the interpolation$ and then con&ert back to the ori!inal orm% Direct interpolation usin! e*ponential maps mi!ht be a &iable alternati&e in some cases$ since the points o sin!ularity are at &ery e*treme orientations and are o ten easily a&oided in practice%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'.B

@o( to Choose Representation ,


For an!ular &elocity or any other situation (here e*tra spins o&er and abo&e #0CK need to be represented$ use the e*ponential map or a*is-an!le%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'0C

Section 8%16

Con&ertin! <et(een Representations

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'0'

Con&ersion ;l!orithms
'% +uler an!les to rotation matri*% )% Rotation matri* to +uler an!les% #% Auaternion to matri*% ,% 3atri* to /uaternion% .% +uler an!les to /uaternion% 0% Auaternion to +uler an!les%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '0)

+uler ;n!les to 3atri* '


+uler an!les represent a trans ormation o inertial space a*es to ob"ect space a*es% We (ant a matri* to trans orm points% 8 course$ it matters (hether (e (ant to !o rom inertial to ob"ect space or the other (ay around% Dets start (ith ob"ect to upri!ht space$ since thats ho( +uler an!les are de ined% Since trans ormin! a*es is the opposite o trans ormin! points$ (e need to ne!ate the an!les%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '0#

+uler ;n!les to 3atri* )


Create rotation matrices or headin!$ pitch$ and bank an!les$ then multiply the matrices% Mo"#ect pright T (P) ($ P$ and ) are the rotation matrices or bank$ headin!$ and pitch$ (hich rotate about the $$ x$ and y-a*is$ respecti&ely%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '0,

+uler ;n!les to 3atri* #

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'0.

+uler ;n!les to 3atri* ,

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'00

+uler ;n!les to 3atri* .


To trans orm points rom ob"ect to upri!ht space$ its "ust the in&erse o the matri* (e "ust sa(% ;nd the in&erse o a rotation matri* is its transpose% M pright o"#ect T =Mo"#ect pright >-' =Mo"#ect pright >T
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De&

T
'01

3atri* to +uler ;n!les '


We must kno( (hich rotation the matri* per orms6 either ob"ectto-upri!ht or upri!ht-to-ob"ect% @ere$ (e (ill de&elop a techni/ue usin! the upri!ht-to-ob"ect matri*% The process o con&ertin! an ob"ect-to-upri!ht matri* to +uler an!les is supposedly &ery similar% For any !i&en an!ular displacement$ there are an in inite number o +uler an!le representations due to +uler an!le aliasin!% The techni/ue presented here (ill al(ays return canonical +uler an!les$ (ith headin! and bank in ran!e '8CK and pitch in ran!e BCK%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'08

3atri* to +uler ;n!les )


Some matrices may be mal ormed$ and so (e must be tolerant o loatin! point precision errors% Some matrices contain trans ormations other than rotation$ such as scale$ mirrorin!$ or ske(% The techni/ue described here only (orks on proper rotation matrices$ perhaps (ith the usual loatin! point imprecision$ but nothin! !rossly out o ortho!onality% I itEs !i&en a non-ortho!onal matri*$ the results are unpredictable% With those considerations in mind$ (e set out to sol&e or the +uler an!les rom the rotation matri* e/uation directly%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '0B

3atri* to +uler ;n!les #

Sol&e or p rom m#) m#) T Gsin p p T asin =Gm#)>


Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1C

3atri* to +uler ;n!les ,


The C standard library unction asin()returns a &alue in the ran!e G S) to S) radians$ (hich is GBC to NBC $ e*actly the ran!e o &alues (e (ish to return or pitch% 2o( that (e kno( p$ (e also kno( cos p% Det us irst assume that cos p C% We can determine sin h and cos h by di&idin! m#' and m## by cos p$ as ollo(s6 m#' T sin h cos p sin h T m#' S cos p and m## T cos h cos p cos h T m## S cos p
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1'

3atri* to +uler ;n!les .


8nce (e kno( the sine and cosine o an an!le$ (e can compute the &alue o the an!le usin! the SDP unction atan) =>% tan h T sin h S cos h h T atan =sin h S cos h> T atan)=sin h $ cos h> This unction returns an an!le rom G to radians$ (hich is G'8C to N'8C $ our desired output ran!e%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1)

3atri* to +uler ;n!les 0


atan2=y$ x> (orks by takin! the arctan!ent o y3x usin! the si!ns o the t(o ar!uments to determine the /uadrant o the returned an!le% Since cos p ` C$ the di&isions do not a ect the /uotient and are$ there ore$ unnecessary% Thus$ headin! can be computed more simply by6 h T atan2=sin h$ cos h> T atan2=m'# Scos p$ m## Scos p> T atan2=m'# $ m## >
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1#

3atri* to +uler ;n!les 1


2o( (e ha&e pitch and headin!% <ank is similar to headin!6 " T atan2=m)' $ m)) > We skipped the case cos p T C% I cos p T C$ then p T BCK =lookin! strai!ht up or strai!ht do(n>% This is the !imbal lock case%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1,

3atri* to +uler ;n!les 8


;rbitrarily assi!n all rotation about the &ertical a*is to headin! and set bank e/ual to Fero% So$ cos p T C "TC Which means sin " T C cos " T '
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1.

3atri* to +uler ;n!les B

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'10

3atri* to +uler ;n!les 'C

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'11

3atri* to +uler ;n!les ''

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'18

Auaternion to 3atri* '


Start by re&erse en!ineerin! the matri* or rotatin! around an arbitrary a*is n T =nx$ ny$ n$> rom Chapter .%

See i (e can rearran!e it so that the parts o a /uaternion leap out at us%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '1B

Auaternion to 3atri* )
Were tryin! to manipulate the matri* so that the ollo(in! &alues pop out at us6 w T cos= 3)> x T nx sin= 3)> y T ny sin= 3)> $ T ny sin= 3)> there are really only t(o ma"or cases to handle6 the dia!onal elements and the o -dia!onal elements%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '8C

Auaternion to 3atri* #
DetEs start (ith the dia!onal elements o the matri*% WeEll (ork throu!h m'' hereO m)) and m## can be sol&ed similarly%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'8'

Auaternion to 3atri* ,

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'8)

Auaternion to 3atri* .
2o( (e need to replace the cos term (ith somethin! that contains cos S) or sin S)$ since the components o a /uaternion contain those terms% Det [ T S)% WeEll (rite one o the double-an!le ormulas or cosine rom Section '%,%, in terms o [$ and then substitute in %

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'8#

Auaternion to 3atri* 0
Substitutin! or cos $ (e ha&e

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'8,

Auaternion to 3atri* 1
Since n is a unit &ector$ nx) N ny) N n$) T '$ and there ore ' G nx) T ny) N n$) $ so6

;s (e said$ elements m)) and m## are deri&ed in a similar ashion%


Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '8.

Auaternion to 3atri* 8
2o( letEs look at the non-dia!onal elements o the matri*O they are easier than the dia!onal elements% WeEll use m') as an e*ample%

WeEll need the re&erse o the double-an!le ormula or sine% sin )[ T ) sin [ cos [ sin T ) sin = S)> cos = S)>
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '80

Auaternion to 3atri* B

;s (e said$ the other nondia!onal elements are deri&ed in a similar ashion%


Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '81

Auaternion to 3atri* 'C


Finally$ (e present the complete rotation matri* constructed rom a /uaternion6

8ther &ariations can be ound in other sources% For e*ample m'' T G' N )w) N )$) also (orks$ since w) N x) N y) N $) T '%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& '88

3atri* to Auaternion '


To e*tract a /uaternion rom a rotation matri*$ re&erse en!ineer the matri* rom the last slide%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'8B

3atri* to Auaternion )

+*aminin! the sum o the dia!onal elements =kno(n as the trace o the matri*> (e !et6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'BC

3atri* to Auaternion #
There ore$ and similarly$

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B'

3atri* to Auaternion ,
In ortunately$ (e cannot use this trick or all our components$ since the s/uare root (ill al(ays yield positi&e results% =3ore accurately$ (e ha&e no basis or choosin! the positi&e or ne!ati&e root%> @o(e&er$ since ! and G! represent the same orientation$ (e can arbitrarily choose to use the nonne!ati&e root or one o the our components and still al(ays return a correct /uaternion% We "ust canEt use the abo&e techni/ue or all our &alues o the /uaternion% ;nother trick is to e*amine the sum and di erence o dia!onally opposite matri* elements%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B)

3atri* to Auaternion .

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B#

3atri* to Auaternion 0
;rmed (ith these ormulas$ (e use a t(o-step strate!y%
'% Sol&e or one o the components o the trace% )% Plu! that &alue into one o the e/uations rom the pre&ious slide to sol&e or the other three%

This strate!y boils do(n to selectin! a ro( rom the table on the ne*t slide$ then sol&in! the e/uations in that ro( rom le t to ri!ht%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B,

3atri* to Auaternion 1

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B.

3atri* to Auaternion 8
The only /uestions is$ (hich ro( should (e use: Which component should (e sol&e or irst: The simplest strate!y (ould be to "ust pick one arbitrarily and al(ays use the same procedure$ but this is rau!ht (ith problems%
DetEs say (e choose to al(ays use the top ro($ meanin! (e sol&e or w rom the trace$ and then or x$ y$ and $ (ith the e/uations on the ri!ht side o the arro(% <ut i w T C$ the di&isions to ollo( (ill be unde ined% +&en i w ` C$ a small w (ill produce numeric instability%

Shoemake su!!ests the strate!y o irst determinin! (hich o w$ x$ y$ and $ has the lar!est absolute &alue$ computin! that component usin! the dia!onal o the matri*$ and then usin! it to compute the other three accordin! to the table on the pre&ious slide%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B0

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B1

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'B8

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

'BB

+uler ;n!les to Auaternion '


Similar to ho( (e con&erted +uler an!les to a rotation matri*% Con&ert headin!$ pitch$ and bank to the correspondin! /uaternions$ then use /uaternion multiplication to !et the composite trans ormation% ?ust as (ith matrices$ there are t(o cases to consider6 one (hen (e (ish to !enerate an ob"ectaupri!ht /uaternion$ and a second (hen (e (ant the upri!htaob"ect /uaternion% Since the t(o are con"u!ates$ (e (ill only (alk throu!h the deri&ation or the ob"ectaupri!ht /uaternion%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)CC

+uler ;n!les to Auaternion )


Suppose (e start (ith +uler an!les h$ p$ and " or headin!$ pitch$ and bank% Det h$ p$ and b be /uaternions (hich per orm the rotations about the y$ x$ and $a*es$ respecti&ely%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C'

+uler ;n!les to Auaternion #


2o( to concatenate these in the correct order% We ha&e t(o sources o back(ardness that cancel each other out% We (ill use i*ed-a*is rotations$ so the order o rotations (ill actually be bank$ then pitch$ then headin!% @o(e&er$ /uaternion multiplication per orms the rotations rom ri!ht-to-le t%
Chapter 8 2otes #D 3ath Primer or 4raphics 5 4ame De& )C)

+uler ;n!les to Auaternion ,

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C#

+uler ;n!les to Auaternion .

The upri!htaob"ect /uaternion is simply the con"u!ate6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C,

Auaternion to +uler ;n!les '


Similar to ho( (e con&erted a rotation matri* to +uler an!les% Pitch6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C.

Auaternion to +uler ;n!les )


@eadin!6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C0

Auaternion to +uler ;n!les #


<ank6

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C1

Auaternion to +uler ;n!les ,


Some code ne*t% '% Con&ert an ob"ectaupri!ht /uaternion into +uler an!les% )% Con&ert an upri!htaob"ect /uaternion into +uler an!les%

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)C8

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)CB

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)'C

That concludes Chapter 8% 2e*t$ Chapter B6

4eometric Primiti&es

Chapter 8 2otes

#D 3ath Primer or 4raphics 5 4ame De&

)''

Vous aimerez peut-être aussi