Animating Rotations and Using Quaternions What We’ll Talk About • • • • • • Animating Translation Animating 2D Rotation Euler Angle representation 3D Angle problems Quaternions Animating with quaternions Translation 2D Rotation 3D Rotation Euler Problems Quaternions Q. Animation Review: Translation P=X Y Z 1 1 1 T= P’ = TP = tx ty 1 tz x + tx y + ty z + tz 1 1 Animation: interpolate over tx,ty,tz in T Move from (10,20,30) to (10,50,40), in time = 0 to 10 ∆x = (50-20)/10 ∆y = (40-30)/10 1 1 10 20 1 30 1 1 10 23 1 31 1 Time = 0 1 1 1 10 26 1 32 1 Time = 1 Time = 2 1 … 1 10 50 1 40 1 Time = 10 Review: 2D Rotation R= cos Ө -sin Ө sin Ө cos Ө P’ = RP 2D Rotation 3D Rotation Euler Problems Quaternions Q. Animation Ө 1 Animating 2D Rotations cos 0 -sin 0 sin 0 cos 0 time Rotation 0 0 3 90 cos 90 -sin 90 sin 90 cos 90 cos 60 -sin 60 sin 60 cos 60 cos 30 -sin 30 sin 30 cos 30 1 Number of frames: 3 ∆R = (90-0)/3 = 30 1 1 1 Review: 3D Rotations 1 Rx = 3D Rotation Euler Problems Quaternions Q. Animation cos Ө -sin Ө sin Ө cos Ө 1 cos Ө Ry = sin Ө 1 -sin Ө cos Ө 1 Rz = cos Ө -sin Ө sin Ө cos Ө 1 1 Orientation specified by a combination of rotations in a predetermined order: RxRyRz Euler Angles Euler Quaternions Q. Animation • Euler’s Theorem: any orientation can be expressed as a single rotation about an axis • Can lead to Gimbal lock • Gives you the basic idea • Euler representation is good basis for calculating quaternions, which work well • Ideas: – Orientation represented by an angle and an (x,y,z) vector, e.g. (A1, Ө1) – Axes represent local coordinate system, not global • Rotation order is reverse of global, i.e. RzRyRx Euler Angles Euler Quaternions Q. Animation • Determine axis of rotation from 1st line a to second line b: cross product a x b • Determine angle between lines dot product = |a| |b| cos Ø Ø = acos ( a b/ (|a||b|) ) note: normalized angle Ө1 • Animation with k = 0..1 : Ø Ө2 axisk = rotate(k Ø ) a anglek = (1-k)Ө1 +kӨ2 Can you do it? • Line 1: P1 = (0,1,0) P2 = (1,0,1) • Line 2: Q1 = (1,1,1) Q2 = (3,3,3) • Cross product: x y z 1 -1 1 2 2 2 (-4,0,-4) • acos a•b/|a||b| acos(2/(√3 * 2√3) = acos(1/3) = 70.5 degrees Motivation • We would like to represent 3D rotations about an arbitrary axis • We would like to be able to apply a series of arbitrary rotations and have it actually work.. – Direct interpolation of matrices leads to nonsense – Gimbal lock occurs when the axes of two of the three gimbals needed to compensate for rotations in 3D space are driven to the same direction, e.g. (0,90,0) Problems Euler Quaternions Q. Animation What is a quaternion? Quaternions Q. Animation • Alternative to Euler angles for specifying orientation • 4-tuple: use 3 numbers for axis of rotation + 1 for angle of rotation • Let q be a quaternion: q = s,v = s, vx, vy, vz = s + vxi + vyj + Vzk Quaternion algebra Quaternions Q. Animation Quaternions are like complex numbers, with one normal component and 3 imaginary components, i,j,k. A. i2 = j2 = k2 = -1 = i-j-k B. ij = k = -ji jk = i = -kj ki = j = -ik (a+bi)(c+di) = C. q1 + q2 = (s1 + s2, v1 + v2) (ac-bd) +(cb-ad)i D. q1*q2 = q3 if s1 = s2 = 0, then q3 = (v1•v2 ,v1 x v2) general: q3 = (s1s2- v1•v2, s1v2 +s2v1 + v1x v2 ) E. q1•q2 = (s1s2 + v1•v2) Not all Quaternions Represent Rotations • • • • • Quaternions Q. Animation Only unit-length quaternions are rotations || q || = sqrt( s2 + v v) = 1 q = 1/||q|| [s,v] q-1 = 1/||q||2 [s,-v] The inverse rotation is rotation by the same amount by the negative axis • Unit quaternion defined by q = (cos Ө/2, sin Ө/2[x,y,z]) Suppose I had an Euler Rotation… q = (cos Ө/2, sin Ө/2[x,y,z]) where Ө and (x,y,z) are the Euler angle and axis respectively 2. Normalize using quaternion normalization rules: q 1. Note: q = -q = (-s,-v) = cos (-Ө/2), sin(-Ө/2)[-x,-y,-z] = cos (Ө/2), -sin(Ө/2)[-x,-y,-z] =( cos (Ө/2), sin(Ө/2)[x,y,z] ) Quaternions Q. Animation Convert Q to matrix form M = 1-2y2-2z2 2xy + 2sz 2xy -2sy 2xy + 2sz 1-2x2-2z2 2yz – 2sx 2xz – 2sy 2yz – 2sx 1-2x2-2y2 Quaternions Q. Animation Rotating a point p by q • Rq(p) = q p q where q = [s,-v] (conjugate of q) where p = [0,(px, py, pz)] Rq(p) = [s,v][0,p][s,-v] =[0,(s2-v v)p + 2v(v p) +2s(v x p)] Quaternions Q. Animation Consecutive Rotations • • • • R2(R1(p)) q2(q1 p q1)q2 = (q2 q1) p (q2 q1) =R21(p) Very convenient… Note about conjugates • q = [s,-v] • q-1= 1/|q|2 q (normalize!!) • For unit quaternions, q-1= q Animating Rotations Using Quaternions • A. How to find intermediate rotations? Linear interpolation: lerp( q1, q2, u) = q1(1-u) + uq2 • Advantage: easier to find q’ vs. animating rotation matrix • Problem: will speed up in the midst of rotation q1 q: point on a 4D unit sphere Ө q2 Animating Rotations Using Quaternions B. Spherical Interpolation slerp( q1, q2, u) = [sin(1-u) Ө/sin Ө ] q1 + [sin uӨ/sin Ө ] q2 Where q1•q2 = cos Ө (note: use smaller Ө ) Why? Spherical Trigonometry Plane trigonometry a = b = c Same formula, but a,b,c are arc lengths sin sin ß sin b a ß b a ß c c