quaternions design and creation of virtual environments cise 6930/4930 section 6589/3146

27
Quaternions Quaternions Design and Creation of Virtual Environments Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146 CISE 6930/4930 Section 6589/3146 Benjamin Lok Benjamin Lok Fall 2003 Fall 2003

Upload: kieran-bradshaw

Post on 31-Dec-2015

42 views

Category:

Documents


0 download

DESCRIPTION

Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146. Benjamin Lok Fall 2003. Euler Angles. Orientation of an object in computer graphics is often described using Euler Angles. R x R y R z Any axis order will work and could be used. Yaw, Pitch & Roll. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

QuaternionsQuaternions

Design and Creation of Virtual EnvironmentsDesign and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146CISE 6930/4930 Section 6589/3146

Benjamin LokBenjamin Lok

Fall 2003Fall 2003

Page 2: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Euler AnglesEuler Angles

Orientation of an object Orientation of an object in computer graphics is in computer graphics is often described using often described using Euler Angles.Euler Angles.

RRxxRRyyRRzz

Any axis order will work Any axis order will work and could be used.and could be used.

Yaw, Pitch & Roll Yaw, Pitch & Roll

Z

Y

X

Page 3: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Problems with Euler AnglesProblems with Euler Angles Rotations not uniquely definedRotations not uniquely defined

Ex: (z, x, y) [roll, yaw, pitch] = (90, 45, 45) = (45, 0, -45)Ex: (z, x, y) [roll, yaw, pitch] = (90, 45, 45) = (45, 0, -45)takes positive x-axis to (1, 1, 1)takes positive x-axis to (1, 1, 1)

Cartesian coordinates are independent of one another, but Cartesian coordinates are independent of one another, but Euler angles are notEuler angles are not

Remember, the axes stay in the same place during rotationsRemember, the axes stay in the same place during rotations Gimbal LockGimbal Lock

Term derived from mechanical problem that arises in gimbal Term derived from mechanical problem that arises in gimbal mechanism that supports a compass or a gyromechanism that supports a compass or a gyro

Second and third rotations have effect of transforming Second and third rotations have effect of transforming earlier rotations, we lose a degree of freedomearlier rotations, we lose a degree of freedom

ex: Rot x, Rot y, Rot zex: Rot x, Rot y, Rot z If Rot y = 90 degrees, If Rot y = 90 degrees,

Rot z is equivalent to -Rot xRot z is equivalent to -Rot x

Page 4: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

A Gimble is a A Gimble is a hardware implementation hardware implementation of Euler angles (used for mounting of Euler angles (used for mounting

gyroscopes and globes)gyroscopes and globes)

Page 5: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Another Problem with Euler Another Problem with Euler AnglesAngles

What if we want to produce a smooth What if we want to produce a smooth animation of a rotation from point P1 to animation of a rotation from point P1 to Point P2 around some axis.Point P2 around some axis.

The entire rotation is defined as RThe entire rotation is defined as RxxRRyyRRzz but but how do we get an intermediate point?how do we get an intermediate point?

Rotations are defined as rotations about Rotations are defined as rotations about the x, y and z axes. Rotations about any the x, y and z axes. Rotations about any other vector in space have to be broken other vector in space have to be broken down into equivalent rotations about the down into equivalent rotations about the major axes.major axes.

Page 6: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Smooth RotationSmooth Rotation

x

z

y

With Euler Angles, knowing the transformations for the entire rotation does not help us with the intermediate rotations. Each is a unique set of three rotations about the x,y and z axes

Page 7: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

QuaternionsQuaternions

Invented by Sir William Hamilton (1843)Invented by Sir William Hamilton (1843) Do not suffer from Gimbal LockDo not suffer from Gimbal Lock Provide a natural way to interpolate Provide a natural way to interpolate

intermediate steps in a rotation about intermediate steps in a rotation about an arbitrary axisan arbitrary axis

Are used in many position tracking Are used in many position tracking systems and VR software support systems and VR software support systemssystems

Page 8: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

QuaternionsQuaternions

Definitions & Basic OperationsDefinitions & Basic Operations

Page 9: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Remember Complex Remember Complex Numbers?Numbers?

z = a + z = a + i i b where b where ii = = (-1)(-1)

Can think of a complex number as having a real and an Can think of a complex number as having a real and an imaginary part or as a vector in two-dimensional spaceimaginary part or as a vector in two-dimensional space

z = [a, b]z = [a, b]

z1+z2 = (a1 + z1+z2 = (a1 + ii b1) + (a2 + b1) + (a2 + ii b2) = (a1+a2) + b2) = (a1+a2) + ii (b1+b2) (b1+b2)

z1*z2 = (a1+z1*z2 = (a1+ii b1)*(a2+ b1)*(a2+ii b2) = (a1a2–b1b2) + b2) = (a1a2–b1b2) + ii (a2b1+a1b2)(a2b1+a1b2)

Page 10: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

QuaternionsQuaternions You can think of quaternions as an extension You can think of quaternions as an extension

of complex numbers where there are three of complex numbers where there are three different square roots of -1.different square roots of -1.

q = w + q = w + i i xx + + j j yy + + k k zz where where ii = = (-1), (-1), j j = = (-1), (-1), kk = = (-1)(-1) i*j = k, j*k = i, k*i = ji*j = k, j*k = i, k*i = j j*i = -k, k*j = -i, i*k = -jj*i = -k, k*j = -i, i*k = -j

You could also think of q as a value in four-You could also think of q as a value in four-dimensional space, q = [w, x, y, z]dimensional space, q = [w, x, y, z]

Sometimes written as q = [w, Sometimes written as q = [w, vv] where w is a ] where w is a scalar and scalar and vv is a vector in 3-space is a vector in 3-space

Page 11: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Addition of QuaternionsAddition of Quaternionsqq1 1 + q+ q22

= (= (ww11++ii xx11++j j yy11++k k zz11) + () + (ww22++ii xx22++j j yy22++k k zz22))

= = ww11++ww22 + + i i ((xx11++xx22) + ) + jj ( (yy11++yy22) +) +k k ((zz11++zz22))

q1 + q2 q1 + q2

= [w1, = [w1, v1v1] + [w2, ] + [w2, v2v2] ]

= [w1+w2, = [w1+w2, v1v1++v2v2]]

Page 12: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Multiplication of Multiplication of QuaternionsQuaternions

qq1 1 * q* q22

= (= (ww11++ii xx11++j j yy11++k k zz11) * () * (ww22++ii xx22++j j yy22++k k zz22) )

= = ww11ww22 + + i i ww11xx22 + + j j ww11yy22 + + k k ww11zz22 + +

--xx11xx22 + + i i ww22xx11 – – j j xx11zz22 + + k k xx11yy22 + +

--yy11yy22 + + i i yy11zz22 + + j j ww22yy11 - - k k xx22yy11 + +

--zz11zz22 - - i i yy22zz11 + + j j xx22zz11 + + k k ww22zz11

q1 * q2 = [w1, q1 * q2 = [w1, v1v1] * [w2, ] * [w2, v2v2] = ] =

Page 13: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Multiplication of Multiplication of QuaternionsQuaternions

q1*q2 = (w1+ix1+jy1+kz1)*(w2+ix2+jy2+kz2)q1*q2 = (w1+ix1+jy1+kz1)*(w2+ix2+jy2+kz2)

= = ww11ww22 + + i i ww11xx22 + + j j ww11yy22 + + k k ww11zz22 + +

--xx11xx22 + + i i ww22xx11 – – j j xx11zz22 + + k k xx11yy22 + +

--yy11yy22 + + i i yy11zz22 + + j j ww22yy11 - - k k xx22yy11 + +

--zz11zz22 - - i i yy22zz11 + + j j xx22zz11 + + k k ww22zz11

q1*q2 = [w1, q1*q2 = [w1, v1v1] * [w2, ] * [w2, v2v2] = ] =

[[w1w2w1w2 – – v1°v2v1°v2, , w1w1v2 v2 ++ w2w2v1v1+ + v1v1v2v2]]

Page 14: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

PracticePractice q1 = [1.57 0 0 1], q2= [.78 0 1 0]q1 = [1.57 0 0 1], q2= [.78 0 1 0] q1 = [.25 3 4 4], q2= [.20 2 9 9]q1 = [.25 3 4 4], q2= [.20 2 9 9] q1 + q2?q1 + q2?qq1 1 + q+ q22 = = ww11++ww22 + + i i ((xx11++xx22) + ) + jj ( (yy11++yy22) +) +k k ((zz11++zz22))q1 + q2 = [w1+w2, q1 + q2 = [w1+w2, v1v1++v2v2]] q1 * q2?q1 * q2?qq1 1 * q* q22 = (= (ww11++ii xx11++j j yy11++k k zz11) * () * (ww22++ii xx22++j j yy22++k k zz22) ) = = ww11ww22 + + i i ww11xx22 + + j j ww11yy22 + + k k ww11zz22 + + --xx11xx22 + + i i ww22xx11 – – j j xx11zz22 + + k k xx11yy22 + + --yy11yy22 + + i i yy11zz22 + + j j ww22yy11 - - k k xx22yy11 + + --zz11zz22 - - i i yy22zz11 + + j j xx22zz11 + + k k ww22zz11

[[w1w2w1w2 – – v1°v2v1°v2, , w1w1v2 v2 ++ w2w2v1v1+ + v1v1v2v2]]

Page 15: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Other operationsOther operations

||q|| = ||q|| = (w(w22 + x + x22 + y + y22 + z + z22))

||q|| = 1 is a unit quaternion||q|| = 1 is a unit quaternion

The inverse of a quaternion, qThe inverse of a quaternion, q-1-1 =[w, =[w, -v-v]/ ||q||]/ ||q||22

q*qq*q-1-1 = [1,0,0,0] = [1,0,0,0]

Practice: Practice: Find ||q|| and qFind ||q|| and q-1-1 of q1 = [1.57 0 0 1], q2 of q1 = [1.57 0 0 1], q2 = [.25 3 4 4]= [.25 3 4 4]

Quaternion multiplication is associate but not Quaternion multiplication is associate but not commutativecommutative

Page 16: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Find the inverse of q = Find the inverse of q = [0,6,8,0][0,6,8,0]

qq-1-1 =[w, =[w, -v-v]/ ||q||]/ ||q||22

qq-1-1 =[0,-6,-8,-0]/(0+36+64+0) = =[0,-6,-8,-0]/(0+36+64+0) = [0,-.06,-.08,0][0,-.06,-.08,0]

Page 17: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Find the inverse of q = Find the inverse of q = [0,6,8,0][0,6,8,0]

qq-1-1 =[w, =[w, -v-v]/ ||q||]/ ||q||22

qq-1-1 =[0,-6,-8,-0]/(0+36+64+0) = [0,-.06,-.08,0] =[0,-6,-8,-0]/(0+36+64+0) = [0,-.06,-.08,0]

To check, q*qTo check, q*q-1-1 should give us the identity should give us the identity quaternion.quaternion.

q*qq*q-1-1 = [= [00--(6*-.06 + 8*-.08 + 0),(6*-.06 + 8*-.08 + 0), [0,0,0]+ [0,0,0]+ [0,0,0][0,0,0]

+[0,0,0]] +[0,0,0]] = [1,0, 0, 0]= [1,0, 0, 0]

[[w1w2w1w2 – – v1°v2v1°v2, , w1w1v2 v2 ++ w2w2v1v1+ + v1v1v2v2]]

Page 18: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

QuaternionsQuaternions

Rotating with QuaternionsRotating with Quaternions

Page 19: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Rotation about an arbitrary Rotation about an arbitrary axisaxis

Let q be a unit quaternion, i.e., ||q|| Let q be a unit quaternion, i.e., ||q|| = 1= 1

q = [w,x,y,z] describes a rotation q = [w,x,y,z] describes a rotation through an angle through an angle where w = where w = cos(cos(/2) and [x,y,z] is the axis of /2) and [x,y,z] is the axis of rotation. rotation. (x,y,z)

Page 20: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

ProcedureProcedure

To create a unit quaternion that To create a unit quaternion that represents a rotation of represents a rotation of degrees degrees about an arbitrary axis about an arbitrary axis vv = [x,y,z]. = [x,y,z].

q = [cos(q = [cos(/2), sin(/2), sin(/2)*/2)*vv/||/||vv||] ||]

Page 21: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Find q, s/t q describes a Find q, s/t q describes a rotation of 60 degrees about rotation of 60 degrees about vv

= [3,4,0].= [3,4,0]. Remember, we need a unit quaternion.Remember, we need a unit quaternion. To get this:To get this:

Compute cos (60/2) = cos30 = 1/2Compute cos (60/2) = cos30 = 1/2 Compute sin (60/2) = sin30 = Compute sin (60/2) = sin30 = 3/23/2 Compute Compute v’v’ = = vv/||/||vv|| = [3,4,0]/5 = [3/5,4/5,0]|| = [3,4,0]/5 = [3/5,4/5,0]

q = [1/2, q = [1/2, 3/2*[3/5,4/5, 0]] 3/2*[3/5,4/5, 0]] = [1/2, 3= [1/2, 33/10, 43/10, 43/10, 0]3/10, 0] ||q|| = ¼ + 27/100 + 48/100 ||q|| = ¼ + 27/100 + 48/100 = (25+27+48)/100 = 1= (25+27+48)/100 = 1

Page 22: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

To rotate a point, P = To rotate a point, P = (p(pxx,p,pyy,p,pzz))

PProtatedrotated = q[0,P]q = q[0,P]q-1 -1

Remember this is quaternion Remember this is quaternion multiplication not matrix multiplication not matrix multiplication!multiplication!

Page 23: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Example: P = (0,1,1) rotated Example: P = (0,1,1) rotated 90 degrees about the vertical 90 degrees about the vertical

y-axis.y-axis.y

z

x

Rotated point should be (1,1,0)

Page 24: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Example: P = (0,1,1) rotated Example: P = (0,1,1) rotated 90 degrees about the vertical 90 degrees about the vertical

y-axis.y-axis. First, compute qFirst, compute q

q = [cos(90/2), sin(90/2)[0,1,0]]q = [cos(90/2), sin(90/2)[0,1,0]] q = [cos45, 0, sin45, 0] = [q = [cos45, 0, sin45, 0] = [2/2, 0, 2/2, 0, 2/2, 2/2,

0]0] Next compute qNext compute q-1-1

qq-1-1 = [ = [2/2, 0, -2/2, 0, -2/2, 0] 2/2, 0] Why?Why? Compute q[0,P]qCompute q[0,P]q-1-1

=[=[2/2, 0, 2/2, 0, 2/2, 0][0,0,1,1][2/2, 0][0,0,1,1][2/2, 0, -2/2, 0, -2/2, 0] 2/2, 0]

Page 25: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Compute q[0,P]qCompute q[0,P]q-1-1

=[ =[2/2, 0, 2/2, 0, 2/2, 0][0,0,1,1][2/2, 0][0,0,1,1][2/2, 0, -2/2, 0, -2/2, 2/2, 0]0]

Remember that q1*q2 = [Remember that q1*q2 = [w1w2w1w2 – – v1°v2v1°v2, , w1w1v2 v2 ++ w2w2v1v1+ + v1v1v2v2] so] soq[0, P] = [q[0, P] = [2/2, 0, 2/2, 0, 2/2, 0][0,0,1,1]2/2, 0][0,0,1,1] = [= [2/2*0 – [0,2/2*0 – [0,2/2,0]°[0,1,1], 2/2,0]°[0,1,1], 2/2[0,1,1] + 0*2/2[0,1,1] + 0*v1 v1 ++ v1 x v2 v1 x v2 ]] = [-= [-2/2, 2/2, 2/2, 2/2, 2/2, 2/2, 2/2]2/2]

q[0, P] qq[0, P] q-1-1 = [- = [-2/2, 2/2, 2/2, 2/2, 2/2, 2/2, 2/2][2/2][2/2, 0, -2/2, 0, -2/2, 0]2/2, 0] = [-= [-2/2* 2/2* 2/2 – [2/2 – [2/2, 2/2, 2/2, 2/2, 2/2]°[0, -2/2]°[0, -2/2, 0], (-2/2, 0], (-2/2)[0, -2/2)[0, -2/2, 2/2,

0] 0] +(+(2/2)*[2/2)*[2/2, 2/2, 2/2, 2/2, 2/2] + 2/2] + v1 x v2v1 x v2 = [0, [1/2,1,1/2] + [1/2 ,0,-1/2] = [0, 1, 1, 0]= [0, [1/2,1,1/2] + [1/2 ,0,-1/2] = [0, 1, 1, 0]

Transformed point is [1,1,0].Transformed point is [1,1,0].

See notes page for ALL the detailsSee notes page for ALL the details

Page 26: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Why use quaternions?Why use quaternions?

If you want to apply multiple If you want to apply multiple rotations, say qrotations, say q11 and q and q22..

When?When? Recall: Recall: q[0,P]qq[0,P]q-1-1

Thus: qThus: q2 2 (q(q11 [0,P] q [0,P] q11-1 -1 ) q) q22

-1 -1

(q(q2 2 qq11) [0,P] (q) [0,P] (q11-1 -1 qq22

-1 -1 )) qq2 2 * q* q11

Why is a quaternion multiplication better than a Why is a quaternion multiplication better than a matrix multiply?matrix multiply?

What about keeping rotations?What about keeping rotations?

Page 27: Quaternions Design and Creation of Virtual Environments CISE 6930/4930 Section 6589/3146

Smooth InterpolationSmooth Interpolation

SLERP – SLERP – spherical linear spherical linear interpolationinterpolation

slerp(t, a, b) = f(t) = (b.aslerp(t, a, b) = f(t) = (b.a-1-1))t t aa What happens when t=0? t=1?What happens when t=0? t=1? In actuality, we need to In actuality, we need to

conserve camera velocity conserve camera velocity (Spherical – Cubic Interpolation) (Spherical – Cubic Interpolation) Why?Why?

Where have you seen this Where have you seen this before?before? Bezier/Hermite curvesBezier/Hermite curves

slerp (t,a,c,d,b)slerp (t,a,c,d,b) a, b – start points, c,d – define a, b – start points, c,d – define

curvecurve The curve touchs cd when t=0.5The curve touchs cd when t=0.5

A

B

C

D