Working with 3D Rotations
Total Page:16
File Type:pdf, Size:1020Kb
Working With 3D Rotations Stan Melax Graphics Software Engineer, Intel Human Brain is wired for Spatial Computation Which shape is the same: a) b) c) “I don’t need to ask A childhood IQ test question for directions” Translations Rotations Agenda ● Rotations and Matrices (hopefully review) ● Combining Rotations ● Matrix and Axis Angle ● Challenges of deep Space (of Rotations) ● Quaternions ● Applications Terminology Clarification Preferred usages of various terms: Linear Angular Object Pose Position (point) Orientation A change in Pose Translation (vector) Rotation Rate of change Linear Velocity Spin also: Direction specifies 2 DOF, Orientation specifies all 3 angular DOF. Rotations Trickier than Translations Translations Rotations a then b == b then a x then y != y then x (non-commutative) ● Programming with rotations also more challenging! 2D Rotation θ Rotate [1 0] by θ about origin 1,1 [ cos(θ) sin(θ) ] θ θ sin cos θ 1,0 2D Rotation θ Rotate [0 1] by θ about origin -1,1 0,1 sin θ [-sin(θ) cos(θ)] θ θ cos 2D Rotation of an arbitrary point Rotate about origin by θ = cos θ + sin θ 2D Rotation of an arbitrary point 푥 Rotate 푦 about origin by θ 푥′, 푦′ 푥′ = 푥 cos θ − 푦 sin θ 푦′ = 푥 sin θ + 푦 cos θ 푦 푥 2D Rotation Matrix 푥 Rotate 푦 about origin by θ 푥′, 푦′ 푥′ = 푥 cos θ − 푦 sin θ 푦′ = 푥 sin θ + 푦 cos θ 푦 푥′ cos θ − sin θ 푥 푥 = 푦′ sin θ cos θ 푦 cos θ − sin θ Matrix is rotation by θ sin θ cos θ 2D Orientation 풚 Yellow grid placed over first grid but at angle of θ cos θ − sin θ sin θ cos θ 풙 Columns of the matrix are the directions of the axes. Matrix is yellow grid’s Orientation 2D Passive Transformation cos θ − sin θ 풙′, 풚′ Basis: 풙, 풚 sin θ cos θ 푥′ cos θ − sin θ 푥 = 푦′ sin θ cos θ 푦 (note: exact same math as before) 푥′ 푥 , both same point but 푦′ 푦 In different reference frames 3D Rotation around Z axis 푍 푎푥푠 푥′ cos θ − sin θ 0 푥 푥′, 푦′ 푦′ = sin θ cos θ 0 푦 푦 푧′ 0 0 1 푧 푥 Can Rotate around X and Y too 푧 푥′ 1 0 0 푥 푦 푦′ = 0 cos θ −sin θ 푦 X 푎푥푠 푥 푧′ 0 sin θ cos θ 푧 풙′, 풚′, 풛′ 푥′ cos θ 0 sin θ 푥 푦 푧 푥 풙′, 풚′, 풛′ 푦′ = 0 1 0 푦 푧′ − sin θ 0 cos θ 푧 Rotating Objects (changing orientation) 0 0 1 1 0 0 0 −1 0 0 1 0 0 0 −1 1 0 0 Rotations: −1 0 0 0 1 0 0 0 1 90˚ on Y 90˚ on X 90˚ on Z Orientations: 1 0 0 0 0 1 0 0 1 −1 0 0 0 1 0 0 1 0 1 0 0 0 0 1 0 0 1 −1 0 0 0 1 0 0 1 0 Matrices used for both rotations and orientations Quat rotations: 0 .7 0 .7 .7 0 0 .7 0 0 .7 .7 orientations: 0 0 0 1 0 0 .7 .7 [.5 .5 .5 .5] 0 .7 .7 0 Row vs Column Conventions OpenGL and most math books use column vectors: C O v’ = M v = B A v L U M N Some engines, APIs (DirectX) use row convention: v’ = v MT = v AT BT All the same. Combining Rotations Combine a sequence of Rotations A,B,… Rotate v by A, then B, then C... = C ( B ( Av )) Mathematically we know C ( B ( A v )) == ( C B A ) v So with matrix-matrix multiplication let: R = C B A R is a single rotation that is the same as rotating by A, then by B then C. Multiplication Order: top of page W Y 90˚ on 90˚ on O right side World Y “World” X X R of page L Z D 푨푤표푟푙푑 푩푤표푟푙푑 World Coordinate Frame Y L 푨푙표푐푎푙 푩푙표푐푎푙 O X 90˚ on 90˚ on C Local Y “Local” Z A Z (dice side 2) (dice side 3) L Dice Coordinate Frame Multiplication Order: Math Equations W 90˚ on 90˚ on O World Y “World” X R = * * L D 푨푤표푟푙푑 푩푤표푟푙푑 푑푐푒푛푒푤 = 푩푤표푟푙푑 ∗ 푨푤표푟푙푑 ∗ 푑푐푒 = * * L 푨푙표푐푎푙 푩푙표푐푎푙 O 90˚ on 90˚ on C 푑푐푒 = 푑푐푒 ∗ 푨 ∗ 푩 Local Y “Local” Z 푛푒푤 푙표푐푎푙 푙표푐푎푙 A (dice side 2) (dice side 3) L Both produce: 120˚ on [1 1 1] Example When to use Local frame ● Player “pulls up” on flight stick. ● Pitch upward about object wing (x) axis. ● World x irrelevant ● Multiply rotation (about x) on the right hand Math: 1 0 0 0 cos θ −sin θ side 0 sin θ cos θ = * Sidenote: a point doesn’t 푐푙푚푏푛푔 푐푟푢푠푒푛푔 푝푡푐ℎ_푢푝 have an orientation, so never = ∗ do this for points. 표푟푒푛푡푎푡표푛 표푟푒푛푡푎푡표푛 푟표푡푎푡표푛 Find Rotation R Between Orientations A and B need to be more specific ● Have an object with orientation A, what rotation R will change it to have orientation B? 푹 = 푩푨−ퟏ ● Given a direction v in reference frame A, what rotation R will show how v points according to B? 푹 = 푩−ퟏ푨 Be aware of all the details of the problem to be solved. Rotating (Reorienting) a Rotation 푟표푡 Machine that rotates an object by 푟표푡 : Apply 45˚ Tilt to the Machine: Rotating a Rotation – Its Different Neither of these 180 푟표푡 45 푡푙푡 multiplication sequences work 45 푡푙푡 180 푟표푡 Rotating a Rotation: Decompose Steps Tilted How to calculate Machine: what this new rotation will be? Rotate duck into and back out of the machine’s reference frame: Same Result! Rotating a Rotation: The Mathematics U P Initial R equation I : = * G H 풏풆풘 푑푢푐푘′ 푑푢푐푘 T = 푟표푡 ∗ 표푟푒푛푡푎푡표푛 표푟푒푛푡푎푡표푛 equation w tilt T I L : = * * * T E D 풏풆풘 푑푢푐푘′′ = 푡푙푡 ∗ 푟표푡 ∗ 푡푙푡−1 ∗ 푑푢푐푘 Rotating a Rotation: The Mathematics Now drop the duck… = * * −1 푟표푡푡푖푙푡푒푑 = 푡푙푡 ∗ 푟표푡 ∗ 푡푙푡 Matrix & Axis Angle 3D Orientation / Rotation Matrix R 풛 푅푥푥 푅푦푥 푅푧푥 푹 = 푅푥푦 푅푦푦 푅푧푦 푅푥푧 푅푦푧 푅푧푧 푹풚 풚 푹풙 General form of Rotation Matrix: • Orthonormal basis: 푹풙 푹풚 푹풛 풙 • 푹풛 = 푹풙 × 푹풚 etc. • Determinant(R)==1 푹풛 • Inverse(R) == Transpose(R) • Has a corresponding axis of rotation Rotation Matrix – Finding its Axis Angle 풛 풛 푹풚 푹풙 휽 풚 풚 푹풛 풙 풙 푅푥푥 푅푦푥 푅푧푥 푹 = 푅푥푦 푅푦푦 푅푧푦 풂풙풊풔, 휽 푅푥푧 푅푦푧 푅푧푧 풂풙풊풔 will be an eigenvector of R Example of corresponding Matrix and Axis Angle 풛 풛 푹풚 휽 푹풙 풚 풚 푹풛 풙 풙 0 0 1 푹 = 1 0 0 풂풙풊풔, 휽 = ퟏ ퟏ ퟏ , ퟏퟐퟎ° 0 1 0 To check, verify: 풂풙풊풔 == 푹 ∗ 풂풙풊풔 Matrix from general axis a, angle θ 풂 푎푥푠 Matrix for a,θ ? Matrix from general axis a, angle θ 풂 푎푥푠 ? [푥, 푦, 푧] How would axis/angle rotate a point [푥, 푦, 푧]? Matrix from general axis a, angle θ • Find b,c unit vecs a,b,c orthonormal 풂 푎푥푠 풂 = 풃 × 풄 , 풄 = 풂 × 풃, 풃 = 풄 × 풂 [푥, 푦, 푧] Matrix from general axis a, angle θ • Find b,c unit vecs a,b,c orthonormal 풂 푎푥푠 풂 = 풃 × 풄 , 풄 = 풂 × 풃, 풃 = 풄 × 풂 • Get [xyz] as weighted sum of a,b,c [푥, 푦, 푧] Matrix from general axis a, angle θ • Find b,c unit vecs a,b,c orthonormal 풂 푎푥푠 풂 = 풃 × 풄 , 풄 = 풂 × 풃, 풃 = 풄 × 풂 • Get [xyz] as weighted sum of a,b,c [푥, 푦, 푧] • Stuff along a stays the same, • Results along b & c based on sinθ and cosθ portions along b & c 푥′ 푥 푥 푥 푥 푥 푦′ = 풂 풂 ∙ 푦 + 풃 풃 ∙ 푦 cos θ − 풄 ∙ 푦 sin θ + 풄 풃 ∙ 푦 sin θ + 풄 ∙ 푦 cos θ 푧′ 푧 푧 푧 푧 푧 Matrix from general axis a, angle θ • Find b,c unit vecs a,b,c orthonormal 풂 푎푥푠 풂 = 풃 × 풄 , 풄 = 풂 × 풃, 풃 = 풄 × 풂 • Get [xyz] as weighted sum of a,b,c [푥, 푦, 푧] • Stuff along a stays the same, • Results along b & c based on sinθ and cosθ portions along b & c 푥′ 푥 푥 푥 푥 푥 푦′ = 풂 풂 ∙ 푦 + 풃 풃 ∙ 푦 cos θ − 풄 ∙ 푦 sin θ + 풄 풃 ∙ 푦 sin θ + 풄 ∙ 푦 cos θ 푧′ 푧 푧 푧 푧 푧 푥′ 푥 “still need method 푇 푇 푇 푇 푇 푦′ = 풂풂 + 풃풃 cos θ − 풃풄 sin θ + 풄풃 sin θ + 풄풄 cos θ 푦 for finding b,c” 푧′ 푧 Matrix from general axis a, angle θ Alternatively (Equivalently): 풂 푎푥푠 [푥, 푦, 푧] Think of [b,c,a] as a 3x3 basis. 풄 ● Move/rotate into abc’s 푥′, 푦′, 푧′ reference frame. 풃 ● Do spin on ‘local’ z axis ● Rotate back out 3x3 Rotation Matrix 푥′ cos θ − sin θ 0 풃 푥 푏푥 푐푥 푎푥 cos θ − sin θ 0 푏푥 푏푦 푏푧 푥 푏 푐 푎 푐 푐 푐 푦 푦′ = 풃 풄 풂 sin θ cos θ 0 풄 푦 = 푦 푦 푦 sin θ cos θ 0 푥 푦 푧 푏푧 푐푧 푎푧 0 0 1 푎푥 푎푦 푎푧 푧 푧′ 0 0 1 풂 푧 “ok, but this math is still not concise.” Challenges with the Space of Rotations Matrix Disadvantages Great for some systems (batch rendering), but not ideal for animation, gameplay, or physics code.