<<

Working With 3D

Stan Melax Graphics Software Engineer, Intel Human Brain is wired for Spatial Computation

Which 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 ● and Axis ● Challenges of deep (of Rotations) ● ● Applications

Terminology Clarification

Preferred usages of various terms:

Linear Angular Object Pose Position () Orientation A change in Pose (vector) Rate of change Linear Velocity

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 푥 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 θ 풙′, 풚′ : 풙, 풚 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 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 let: R = C B A R is a single rotation that is the same as rotating by A, then by B then C. Multiplication : 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 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 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 U P Initial R 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. • (R)==1 푹풛 • Inverse(R) == (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 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 for animation, gameplay, or code. 푅푥푥 푅푦푥 푅푧푥 푹 = 푅푥푦 푅푦푦 푅푧푦 ● Non-compact (9 floats for only 3DOF) 푅푥푧 푅푦푧 푅푧푧 ● Numerical Drift, non-orthonormal over time ● Getting meaningful information non-trivial? ● Extracting an axis of rotation by eigenvector ● Interpolation between orientations (keyframes)

Is there a better way to be working with rotations/orientations? Yaw-Pitch-Roll (Euler )

y,p,r = 0,0,0 45,0,0 45,0,45

• Ordered sequence of rotations on 3 fixed main axes. • Ideal representation for many game systems: • Standing NPC (yaw==heading) • Camera AI, • Helicopter flight. • Convert to Matrix on the fly as necessary. Yaw-Pitch-Roll – not ideal for general 3D

● Concatenating rotations: Done by matrix Consider pitch multiplication. Converting back to YPR?  upward to 90: ● Smooth interpolation and comparing rotations. What’s the angle between:

and

Could be: [20 80 -20] [-60 80 60] [0 90 0] or [45 90 -45] or [n 90 –n] (any n) Numerically distant, but Orientations similar! Angle Axis Axis Angle has Potential: ● General 3D ● Compact (drift averse) ● Inversion and Interpolation easy (just modify angle)

Issues: ● Specifics of the encoding (angle as separate or axis length?). ● Transforming points shouldn’t be clumbsy. ● Need a better/cleaner conversion to matrix. ● How can we “multiply” (combine) two Axis Angle rotations??? … Combining Angle Axis Rotations It’s Tricky ...

Small Angles:

Tilt, , No Roll. “Result axis/angle is almost like vector addition on [1 0 0],10° then [0 1 0],10° ~= [1 1 0],14 ° the xy

Larger Angles: “Hmmm, Combining = X and Y somehow make Z” [100],180° then [010],180° [0 0 1],180° Combining Angle Axis Rotations It’s Tricky Because…

[100],90 and [010],90 = ...

[010],90 and [100],90 = ...

Order of rotations makes a difference... Combining Angle Axis Rotations It’s Tricky Because Rotations are Tricky!

[100],90 and [010],90 = [1 1 -1],120

[010],90 and [100],90 = [1 1 1],120

Yikes. Is there any mathematics wizardry that can deal with this? Quaternions – Mathematics of Rotations Quaternions – Mathematics of Rotations

● Practical and Efficient (get the job done). Provides the machinery your program uses for rotational operations. ● Industry-wide standard algebraic system for dealing with rotations in 3D. (existing code, popular engines). You’ll need this. ● Geometric encompass (and surpass) quaternions. ● Still worth studying quats (stepping stone) ● A bit abstract (4D and complex ). Best to think visually/spatially.

Quaternions – not too complex 

● Like complex numbers a+bi, but with 3 ⊥ sqrts of -1: i,j,k ● ii=jj=kk=ijk=-1 , so ij=k , ji=-k , jk=i, ki=j ● Numbers of the form: q= a+bi+cj+dk (math text notation)

● Isomorphic to R3+: q= a+be23+ce31+de12 ● In Practice: q= xi+yj+zk+w (graphics/gamedev convention) ● multiplication: +푎푥푏푤 + 푎푦푏푧 − 푎푧푏푦 +푎푤 푏푖 풊

+ −푎푥푏푧 + 푎푦푏푤 + 푎푧푏푥 +푎푤 푏푗 풋 풂풃 = + +푎푥푏푦 − 푎푦푏푥 + 푎푧푏푤 +푎푤 푏풌 풌

+ −푎푥푏푥 − 푎푦푏푦 − 푎푧푏푧 +푎푤 푏푤

Connection to Rotations may not be obvious yet… Quaternions as , [v,w]

Equivalent to write quaternion as a bivector,scalar pair:

the xyz elements into a 3D bivector v alongside w.

Instead of: 푞푥, 푞푦, 푞푧, 푞푤 , its now: 풒풗, 푞푤

● Quaternion multiplication equivalent to: 풂풃 = 풂풗, 푎푤 ∗ 풃풗, 푏푤 = 풂풗 × 풃풗 + 풂풗푏푤 + 푎푤풃풗 , −풂풗 ∙ 풃풗 + 푎푤푏푤

Cross

some familiar operations Unit Quaternions and Rotations

Use Quaternions on unit 4D hypersphere (푥2+푦2+푧2+푤2 == 1): ● rotation/orientation with axis a and angle θ:

휃 휃 풒 = 풂 sin , cos 2 2

● Length of bivector part proportional to sin of half of angle. ● Value of scalar part w keeps quaternion at unit length (or cos of same half angle).

May be easier to visualize just using the (3D) bivector v component. But its not a regular (Euclidean) 3-space.

Unit Quaternions and Rotations

Double Coverage: Rotation around axis a and angle θ would produce the same result as rotation around axis -a and angle –θ. Therefore, q and –q represent the same rotation.

Inverse: Rotation around axis -a and angle θ (or around a by - θ) would give the opposite rotation. Since q is of unit length just use conjugate: 휃 휃 풒−ퟏ = 풄풐풏풋 풒 = −푥, −푦, −푧, 푤 = [−푣, 푤] = −풂 sin , cos 2 2

Examples Revisited with Quaternions:

Small Angles: approx 15° approx 10° on X on [1 1 -.1] then 10° on Y [0 0.1 0 0.99] * [0.1 0 0 0.99] ~= [0.1 0.1 -0.01 0.99]

Cross and dot Product near zero: 풂 풃 = 풂풗 × 풃풗 + 풂풗푏푤 + 푎푤풃풗 , −풂풗 ∙ 풃풗 + 푎푤푏푤 Larger Angles: 180 on X 180 on Y 180 on -Z =

[1 0 0 0] [0 1 0 0] [0 1 0 0]*[1 0 0 0]=[0 0 -1 0]

풂풗 × 풃풗 + 풂풗푏푤 + 푎푤풃풗 , −풂풗 ∙ 풃풗 + 푎푤푏푤 = 0 1 0 × 1 0 0 + 0 + 0 , 0 + 0 = 0 0 − 1 0 Only Matters here Examples Revisited now with Quaternions:

90˚ on X 90˚ on Y 120˚ on [1 1 -1]

.7 0 0 .7 0 .7 0 .7 0 .7 0 .7 ∗ .7 0 0 .7 = [0.5 0.5 − 0.5 0.5]

90˚ on Y 90˚ on X 120˚ on [1 1 1]

0 .7 0 .7 .7 0 0 .7 0.7 0 0 .7 ∗ 0 .7 0 .7 = [.5 .5 .5 .5]

Numerical values added just to see that the quaternion math indeed matches expectations. Rotating Points/Vectors with Quaternions

Representation Combine Rotate points or Rotations a,b vectors (v)

Matrix: Mb Ma M v -1 Quaternion: qb qa q v q

applies to both rotating points/vectors and other matrices. ● Rotate a point or vector v by treating it as a quaternion [v,0] and multiply by rotation and conjugate on the left and right sides respectively. Or use quaternion-to-matrix conversion. 휃 휃 휃 휃 ab = 풂풗 × 풃풗 + 풂풗푏푤 + 푎푤풃풗 , −풂풗 ∙ 풃풗 + 푎푤푏푤 -1 풒풗풒−ퟏ = 퐚 sin , cos 풗, 0 −퐚 sin , cos quaternion multiplication (bivector-scalar style) qvq 2 2 2 2

휃 휃 휃 휃 휃 = sin 퐚 × 풗 + cos 풗, − sin 풂 ∙ 풗 −퐚 sin , cos 2 2 2 2 2

휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 휃 = sin 퐚 × 풗 × −퐚 sin + cos 풗 × −퐚 sin + cos sin 퐚 × 풗 + cos cos 풗 + −퐚 sin sin −풂 ∙ 풗 , −풂 ∙ 풗 sin cos + sin 퐚 × 풗 ∙ 퐚 sin + cos 풗 ∙ 퐚 sin 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 cancel out 휃 휃 휃 휃 휃 = − sin2 퐚 × 풗 × 퐚 + 2 cos sin 퐚 × 풗 + cos2 풗 + sin2 풂 ∙ 풗 퐚, 0 2 2 2 2 2 휃 휃 휃 = − sin2 퐚 × 풗 × 퐚 + sin 휃 퐚 × 풗 + cos2 풗 + sin2 풂 ∙ 풗 퐚, 0 2 2 2 휃 휃 휃 휃 = cos2 퐚 × 풗 × 퐚 − sin2 퐚 × 풗 × 퐚 + sin 휃 퐚 × 풗 + cos2 풂 ∙ 풗 퐚 + sin2 풂 ∙ 풗 퐚, 0 2 휃 휃 2 2 2 cos2 − sin2 = cos 휃 2 2 2 2 cos + sin = 1

= cos 휃 퐚 × 풗 × 퐚 + sin 휃 퐚 × 풗 + 풂 ∙ 풗 퐚 , 0 after simplifying 휃 휃 -1 푞 = 퐚 sin , cos qvq : Rotating Points/Vectors 2 2 Portion along a Three Orthogonal Vectors stays the same 푞푣푞−1 = cos 휃 퐚 × 풗 × 퐚 + sin 휃 퐚 × 풗 + 풂 ∙ 풗 퐚 , 0

Quaternion sum weighted 풂 푎푥𝑖푠 multiplication by sin and cos qvq-1 transforms v by rotation q v lies in plane of 2 of these basis vectors: 풗 푣 = 퐚 × 풗 × 퐚 + 풂 ∙ 풗 퐚

Applications Quaternions can replace most Rotation Matrices

● Cameras or any general objects with position and orientation. ● Rigid Bodies - physics engines mostly use vec/quat pairs ● Vertex buffers instead of tangent,bitangent, can use: struct Vertex { float3 position; // location in mesh reference frame float4 orientation; // quaternion tangent space basis float2 texcoord; // uv’s ... Orientation Map ● Extention of normalmap ● rgba encodes orientation. Tangents T Normals N Orientations

Disclaimer: just curiosity research, Disc with specular (푇 ∙ 퐿) and diffuse (푁 ∙ 퐿) not sure how useful. SLERP – Spherical Linear Interpolation Key 0 Key 1 ● Smooth transition between orientations 푞0, 푞1 ● Double Coverage Issue: Use −푞1 instead of 푞1 if closer to 푞0 ● Normalized Lerp (nlerp) often sufficient 푞푡 = 푛표푟푚푎푙𝑖푧푒(푞0 1 − 푡 + 푞1 푡 ) ● Used by animation systems (blend keyframes)

Resulting Skinned Animation

t=0 t=0.5 t=1 NLERP 0.5 Quats – they do Addition too… Proof it’s the same: 휔 lim 푠 → 푑푡, 1 Updating state to the next time step. ( 휔 푑푡)→0 2 휔 푠 ∗ 푞 = 0001 ∗ 푞 + 푑푡, 0 ∗ 푞 ● Position: 푝푡+푑푡 = 푝푡 + 푣푒푙표푐𝑖푡푦 ∗ 푑푡 푡 푡 2 푡 휔 ● Orientation (spin 휔): 푠 ∗ 푞 = 푞 + , 0 ∗ 푞 푑푡 푡 푡 2 푡

Could Build a Quat for Multiplication Or Add Derivative

휔 휔 푑푡 휔 푑푡 푑푞 푠 = sin ( ), cos ( ) 푞푡+푑푡 = 푞푡 + 푑푡 휔 2 2 푑푡 휔 푞 = 푠 ∗ 푞 푞 = 푞 + 푞 푑푡 푡+푑푡 푡 푡+푑푡 푡 2 푡

Ok but why?... Quat Application: Time Integration (no drift)

more quat 휔(푞푡) additions Spin 휔 is not constant!! 푘 = ∗ 푞 푡 1 2 푡 휔(푞 + 푘 ∗ 푑푡/2) 푑푡 푘 = 푡 1 ∗ (푞 + 푘 ∗ ) 2 2 푡 1 2 휔(푞 + 푘 ∗ 푑푡/2) 푑푡 푘 = 푡 2 ∗ (푞 + 푘 ∗ ) 3 2 푡 2 2 휔(푞 + 푘 ∗ 푑푡) 푘 = 푡 3 ∗ (푞 + 푘 ∗ 푑푡) 4 2 푡 3 휔(푞푡) 푑푡 푑푡 푑푡 푑푡 푞푡+푑푡 = 푞푡 + ∗ 푞푡 ∗ 푑푡 푞 = 푞 + 푘 ∗ + 푘 ∗ + 푘 ∗ + 푘 ∗ 2 푡+푑푡 푡 1 6 2 3 3 3 4 6 Forward Euler Runge Kutta only looks at starting spin Takes samples over the timestep Orientation Updates (Euler vs RK4)

Forward Euler: • Spin drifts toward principle axis • Energy gained

Runge Kutta • Spin orbits as expected • Energy stays constant

Watch GDC 2013 Math Tutorial for full explanation of inertia , time integration, angular momentum, rk4, … Rotation that takes one direction 풗ퟎ to another 풗ퟏ

Cross product to find axis What if Less stable 풂 풂 was 0 when d ~ 1

풗ퟎ × 풗ퟏ 풂 acos (푑) acos (푑) 풒 = sin , cos 풂 2 2 풗ퟎ × 풗ퟏ 풗 ퟏ using half 풂 2(1 + 푑) 풒 = , 풗ퟎ angle 풗ퟎ formulas 2(1 + 푑) 2

When 풗 and 풗 get close, GA style – ퟎ ퟏ 풗ퟎ+풗ퟏ geometric Let: 풗풎풊풅 = 풂 = 풗ퟎ × 풗ퟏ becomes small product 풗ퟎ+풗ퟏ 풅 = 풗 ∙ 풗 goes to 1. produces ퟎ ퟏ rotation 풒 = 풗ퟎ × 풗풎풊풅, 풗ퟎ ∙ 풗풎풊풅 Ignore 풗ퟎ = −풗ퟏ case for now Diagonalization of Symmetric Matrices 푎 푓 푒 푓 푏 푑 For S find D,R: 푒 푑 푐 푫 = 푹 푺 푹−1 ● Iterative approach [Jacobi 1800s]. Rotate Off ● can accumulate directly into Largest Matrix or a Quaternion (3D).

diagonal? Eigenvalues are entries of diagonal part. No If not all equal, this may be interpreted as an orientation for the matrix in some contexts. Yes 푎′ 0 0 “Orientations may show up 푥 푦 푧 푤 , 0 푏′ 0 in new interesting places” 0 0 푐′ Orientation of a Point Cloud

● Compute covariance 2 푣푥 푣푥푣푦 푣푥푣푧 ● 푻 2 Diagonalize to get orientation. 풄풐풗 = 풗풗 = 푣푦푣푥 푣푦 푣푦푣푧 2 ● Permute by eigenvalues for 푣푧푣푥 푣푧푣푦 푣푧 long,med,short axes.

y Principle Axes

x UI: Data from AI: Optimal Depth Sensor bombing run. Visualize Inertia Properties To debug physics behavior of : ● Diagonalize Inertia Tensor (symmetric matrix) ● Draw box over object with resulting orientation ● Eigenvalues are box

Irregular Shape Inertia Overlay Dual Quat Screw Motion Dual Quaternions distant axis S 2 ● Add a 0 or 휀, 휀 =0 L ● 푞 = 푥풊, 푦풋 , 푧풌, 푤 , 푥′풊휺, 푦′풋휺, 푧′풌휺, 푤′휺 E R ● Put half translation t in dual part P ′′ 풕 = 0,0,0,1, 푡푥/2, 푡푦/2, 푡푧/2,0 ● Extend rotation r to dual quat ′′ 풓 = 풓, 0,0,0,0 S ● Multiply trans and rot dual quaternions K 풒′′ = 풕′′풓′′ I N Rotation and Trans in a single 8D number 풒′′ N I N G To be continued (in Gino’s IK session) ... Matrix Dual Quat Working with Rotations - Conclusion ● Rotations can be tricky (don’t blame math) ● Matrices work ● Quaternions work, more concise, more uses ● Be Aware, Be Precise: ● who to multiply ● what order to use ● when to invert Now go and do cool 3D stuff  Q & A

Raise your hand if you have any questions now!