Lecture 2: linear algebra, animation basics Computer Animation, Rick Parent, Chapter 2 and Appendix Khan Academy online courses for Calculus and Linear Algebra, geometry, trigonometry https://www.khanacademy.org/ b b a ax ay x by a b a a a sa sa b az z b a b a x bx a y by a z bz bx a y by az x x x ay sa y az sa z z The magnitude (length) of a vector is: v v v v 2 x 2 y Unit vector (magnitude=1.0) v v 2 z a b a x bx a y by a z bz ai bi a b a b cos How do you find the angle θ between vectors a and b? b θ a a b a b cos a b cos a b 1 a b cos a b b θ a 0 < a·b < 1 a·b = 0 b -1 < a·b < 0 a·b = 1 θ a a·b a b 1.0 a b cos a·b = -1 If a and b are arbitrary (non-unit) vectors, then the following are still true: If θ < 90º then a·b > 0 If θ = 90º then a·b = 0 If θ > 90º then a·b < 0 If |u|=1.0 then a·u is the length of the projection of a onto u a u a·u i a b ax bx j ay by k az bz a b a y bz a z by a z bx a x bz a x by a y bx a b a b sin a b area of parallelogram ab a b 0 if a and b are parallel a b is perpendicular to both a and b, in the direction defined by the right hand rule Dot product produces a scalar Cross product of two vectors is a vector Dot product applies to n dimensional vectors Cross product applies to 3 dimensional vectors Intuition: Dot product shows how much part of the vector a is in the same direction as vector b Cross product is how much part of the vector a is perpendicular to the vector b Find the area of the triangle defined by 3D points a, b, and c c b a 1 area b a c a 2 c c-a b a b-a An object is at position p with a unit length heading of h. We want to rotate it so that the heading is facing some target t. Find a unit axis a and an angle θ to rotate around. • • p h t h t p a h t p h t p cos t p t 1 a • t-p θ • p h Sin Cos Tan Cot 0 0 1 0 Not defined 30 1/2 1/√3 √3 45 1/√2 1/√2 1 1 60 3/2 1/2 √3 1/√3 90 1 0 Not defined 0 3/2 cos2θ + sin2θ = 1 1.0 θ cos θ sin θ Law of Sines: a b c sin sin sin Law of Cosines: c a b 2ab cos 2 2 2 b α γ c β a 5 A= −1 3 4 5 Det 𝐴 = −1 3 = 5.4 – (-1.3) = 23 4 1 1 . adj A = . det(𝐴) 23 Inv 𝐴 = 4 Adj A = 1 −3 5 4 −3 1 5 b b a ax ay x by b az z a b a x bx a y by a z bz L MN l11 l12 l l 21 22 l31 l32 l13 m11 m12 l23 m21 m22 l33 m31 m32 l12 m11n12 m12 n22 m13 n32 m13 n11 n12 m23 n21 n22 m33 n31 n32 n13 n23 n33 Let’s say we have a 3D model that has an array of position vectors describing its shape We store all position vectors: vn where 0≤n≤NumVerts-1 If we want to move the object (translate) 𝑣𝑛′ = 𝑣𝑛 + 𝑑 (relative offset) v ' x v ' y v vx ' z 𝑣𝑥′ = 𝑣𝑥 + 𝑑𝑥 𝑣𝑦′ = 𝑣𝑦 + 𝑑𝑦 𝑣𝑧′ = 𝑣𝑧 + 𝑑𝑧 vy vz d x dy dz v ' x v 'y v z' v x vy 1 0 0 v z 0 1 0 d x 0 0 1 vx v x 1 v y 0 v z 0 d x vy v x 0 v y 1 v z 0 d y vz v x 0 v y 0 v z 1 d z dy dz Now, let’s rotate the object in the xy plane by an angle 𝜃, as if we were spinning it around the z axis 𝑣𝑥′ = 𝑣𝑥 cos 𝜃 − 𝑣𝑦 sin(𝜃) 𝑣𝑦′ = 𝑣𝑥 𝑠𝑖𝑛 𝜃 + 𝑣𝑦 cos(𝜃) 𝑣𝑧′ = 𝑣𝑧 Note: a positive rotation will rotate the object counterclockwise when the rotation axis (z) is pointing towards the observer 𝑣𝑥′ = 𝑣𝑥 cos 𝜃 − 𝑣𝑦 sin(𝜃) 𝑣𝑦′ = 𝑣𝑥 𝑠𝑖𝑛 𝜃 + 𝑣𝑦 cos(𝜃) 𝑣𝑧′ = 𝑣𝑧 y 𝜃 = 30 𝑣𝑥′ 𝑣𝑦′ 𝑣𝑧′ 𝑣𝑥 𝑣𝑦 𝑣𝑧 x z 𝑣𝑥′ = 𝑣𝑥 cos 𝜃 − 𝑣𝑦 sin(𝜃) + 0𝑣𝑧 𝑣𝑦′ = 𝑣𝑥 𝑠𝑖𝑛 𝜃 + 𝑣𝑦 cos(𝜃) + 0𝑣𝑧 𝑣𝑧′ = 0𝑣𝑥 + 0𝑣𝑦 + 1𝑣𝑧 𝑣𝑥′ 𝑣𝑦′ 𝑣 ′ = 𝑣. 𝑀 𝑣𝑧′ = 𝑣𝑥 𝑣𝑦 cos(𝜃) 𝑣𝑧 . − sin(𝜃) 0 sin 𝜃 cos(𝜃) 0 0 0 1 1 𝑅𝑥 𝜃 = 0 0 0 cos(𝜃) −sin(𝜃) cos(𝜃) 0 𝑅𝑦 𝜃 = sin(𝜃) cos(𝜃) 𝑅𝑧 𝜃 = − sin(𝜃) 0 0 1 0 0 sin(𝜃) cos(𝜃) −sin(𝜃) 0 cos(𝜃) sin 𝜃 cos(𝜃) 0 0 0 1 If we have a vector v, and an x-axis rotation: v v . Rx ( ) If we then want to rotate it around y-axis: v" v'. R y ( ) v" v. Rx ( ) R y ( ) v" v. ( M 1M 2 M 3 M 4 ) v" v. M total v vx vy vz v v M vx v x a x v y bx v z c x vy v x a y v y by v z c y vz v x a z v y bz v z c z a x M bx cx ay by cy az bz c z v v x a v y b v z c vx v x a x v y bx v z c x d x vy v x a y v y by v z c y d y vz v x a z v y bz v z c z d z a, b, c, d are all constants (12 in total) How do we represent rotation and orientation together in a matrix? vx v x a x v y bx v z c x d x vy v x a y v y by v z c y d y vz v x a z v y bz v z c z d z v ' x v ' y v ' z 1 vx vy vz ax b x 1 cx d x ay by cy dy az bz cz dz 0 0 0 1 3x3 rotation matrix and 3x1 translation vector combined in a 4x4 matrix (with [0 0 0 1] at the right) 3D position vector v is changed to [vx vy vz 1] The line at the right is not used here but it is necessary when rendering objects as a 2D image First, let's look at how projective geometry works in 2D, before we move on to 3D. Imagine a projector that is projecting a 2D image onto a screen. What happens when the projector goes closer to the screen? What is the role of W? Applying it to 3D When W increases the coordinate scales up and when W decreases it scales down. Coordinates are said to be correct in 3D, only when W = 1. (convention) W < 1 everything would look too big W > 1 everything would look too small W = 0 division by zero error W < 0 everything would flip upside down and back-to-front (15,21,3) => (15/3, 21/3, 3/3) => (5, 7, 1) 1/5 (10, 20, 30, 5) = (2, 4, 6, 1) Perspective is the phenomenon where an object appears smaller the further away it is from the camera. (because it is scaled down) A far-away mountain can appear to be smaller than a cat, if the cat is close enough to the camera. Perspective in 3D graphics is implemented by using a transformation matrix that changes the W element of each vertex. Z represent the distance, the larger the Z is, the more it needs to be scaled down. W effects the scale and it is related with Z Perspective projection matrix applied to a homogeneous coordinate 1 0 0 0 0 1 0 0 0 0 1 1 0 2 2 0 3 3 0 4 4 0 1 4 After the perspective projection matrix is applied, each vertex goes under perspective division. Converting a homogeneous coordinate back to W = 1. ¼ (2,3,4,4) = (0.5, 0.75, 1, 1) After the perspective division, W is discarded. Correct 3D coordinate that has been scaled according to a 3D perspective projection Technically, homogeneous vectors are 4D vectors that get projected into the 3D w=1 space v x vy vz vx vw vw vy vw vz vw Computer graphics apps commonly use 4x4 homogeneous matrices A rigid 4x4 matrix transformation looks like this: b ax b x M cx d x ay by cy dy az bz cz dz 0 0 0 1 y c d a x z Where a, b, & c are orthogonal unit length vectors representing orientation, and d is a vector representing position The space that an object is defined in is called object space or local space The object is located at or near the origin and is aligned with the xyz axes The units in this space can be whatever we choose (i.e. meters, etc) A 3D object would be stored on disk and in memory in this coordinate system When we draw the object, we want to transform it into another space We will define a new space called world space or global space This space represents a 3D world or scene and may contain several objects in various locations Every object in the world needs a matrix that transforms its vertices from its own object space into the world space We call this object’s world matrix For example, if we have 100 chairs in a room, we only need to store the object space data for one chair once. We can use 100 different matrices to transform the chair model into 100 locations in the world. The 9 constants make up 3 vectors a, b and c If we think of the matrix as a transformation from object to world space the a vector is essentially the object’s x-axis rotated into world space b is its y-axis in world space and c is its z-axis in world space. d is the position in world space 1 0 I 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Take one more look at the identity matrix It’s a axis lines up with x, b lines up with y, and c lines up with z Position d is at the origin Therefore, it represents a transformation with no rotation or translation 1 0 cos(𝜃) 𝑅𝑥 𝜃 = 0 0 −sin(𝜃) cos(𝜃) 0 𝑅𝑦 𝜃 = sin(𝜃) cos(𝜃) 𝑅𝑧 𝜃 = − sin(𝜃) 0 0 sin(𝜃) cos(𝜃) 0 −sin(𝜃) 1 0 0 cos(𝜃) sin 𝜃 cos(𝜃) 0 0 0 1 0 0 1 0 cos( ) sin( ) Rx ( ) 0 sin( ) cos( ) 0 0 0 cos( ) 0 R y ( ) sin( ) 0 cos( ) sin( ) Rz ( ) 0 0 0 sin( ) 1 0 0 cos( ) 0 0 sin( ) 0 cos( ) 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 1 For example, a translation by vector r followed by a z-axis rotation is: v x' cos( ) sin( ) ' v y sin( ) cos( ) v z' 0 0 0 1 0 0 0 1 0 0 1 0 0 . 0 0 1 0 0 1 0 0 0 rx v x 0 ry v y 1 rz v z 0 1 1 If the upper 3x3 portion is orthonormal, we say that 4x4 matrix is rigid only translated and rotated (it will not have any scale or shears which distort the object) If all row vectors and all column vectors of a matrix are unit length, that matrix is said to be orthonormal This also implies that all vectors are perpendicular to each other Orthonormal matrices have some useful mathematical properties, such as: M-1 = MT If a 4x4 matrix represents a rigid transformation, then the upper 3x3 portion will be orthonormal a b c 1 a bc b ca c ab v vx vy v v M vz 1 ax b x M cx d x vx v x a x v y bx v z c x d x vy v x a y v y by v z c y d y vz v x a z v y bz v z c z d z vw 1 ay by cy dy az bz cz dz 0 0 0 1 v v x a v y b v z c d v v x a v y b v z c d y v=(.5,.5,0,1) (0,0,0) x Local Space v v x a v y b v z c d b Matrix M y y d v=(.5,.5,0,1) (0,0,0) x Local Space (0,0,0) x World Space a v v x a v y b v z c d b v’ y y d v=(.5,.5,0,1) (0,0,0) x Local Space (0,0,0) x World Space a To transform a vector v by matrix M: v’=v·M If we want to apply several transformations, we can just multiply by several matrices: v’=(((v·M1)·M2)·M3)·M4… Or we can concatenate the transformations into a single matrix: Mtotal=M1·M2·M3·M4… v’=v·Mtotal If M transforms v into world space, then M-1 transforms v’ back into local space v v M v v M 1 1 M M I We usually transform vertices from some local space where they are defined into a world space v’ = v·W Once in world space, we can perform operations that require everything to be in the same space (collision detection, high quality lighting…) Then, they are transformed into a camera’s space, and then projected into 2D v’’ = v’·C-1·P In simple situations, we can do this all in one step: v’’ = v·W·C-1·P while (!finished) { UpdateEverything(); DrawEverything(); } Interactive vs. Non-Interactive Real Time vs. Non-Real Time Film Imax NTSC TV PAL TV HDTV Computer 24 fps 48 fps 30 fps (interlaced) 25 fps (interlaced) 50-60 fps >60 fps Animation tools: Maya 3D Studio Max MotionBuilder Blender Game/graphics engines: OGRE3D Unity Unreal Applications of animated characters Health ▪ Analyzing walking styles ▪ Analyzing muscle activations ▪ Analyzing joint movement limits Ergonomics ▪ Usability of devices ▪ Measuring comfort Safety ▪ Simulated worlds for crisis management Entertainment ▪ Games, movies Different approaches: Keyframe animation Motion Capture Physics-based animation Procedural animation Different levels of character motion Representation: skeletal model A VH is represented by a polyhedral model (or mesh) An underlying skeleton deforms this mesh ▪ Joints, connected by bones A pose is defined by the rotations of the joints and the position of the root joint Several standards ▪ H-Anim ISO standard (ISO/IEC 19774) for humanoid modelling and animation Developed in the late 90’s Systematic system for representing humanoids in a network-enabled 3D graphics and multimedia environment http://www.h-anim.org/ Root Torso Pelvis Neck ShoulderL ShoulderR HipL HipR Head ElbowL ElbowR KneeL KneeR WristL WristR AnkleL AnkleR Degree of Freedom (DOF) A variable φ describing a particular axis or dimension of movement within a joint Joints typically have around 1-6 DOFs (φ1…φN) Changing the DOF values over time results in the animation of the skeleton Note: in a mathematical sense, a free rigid body has 6 DOFs: 3 for position and 3 for rotation Kinematics The analysis of motion independent of physical forces. Kinematics deals with position, velocity, acceleration, and their rotational counterparts, orientation, angular velocity, and angular acceleration. Forward Kinematics The process of computing world space geometric data from DOFs Inverse Kinematics The process of computing a set of DOFs that causes some world space goal to be met (I.e., place the hand on the door knob…) Skeleton A pose-able framework of joints arranged in a tree structure. Joint Allows relative movement within the skeleton. Are essentially 4x4 matrix transformations Can be rotational, translational, or other Synonym: bone Core Joint Data DOFs (N floats) Local matrix: L World matrix: W Additional Data Joint offset vector: r DOF limits (min & max value per DOF) Type-specific data (rotation/translation axes, constants…) Tree data (pointers to children, siblings, parent…) 1. Specify all DOF values for the skeleton (done by higher level animation system) 2. Recursively traverse through the hierarchy starting at the root and use forward kinematics to compute the world matrices (done by skeleton system) 3. Use world matrices to deform skin & render (done by skin system) Note: the matrices can also be used for other things such as collision detection, FX, etc. In the recursive tree traversal, each joint first computes its local matrix L based on the values of its DOFs and some formula representative of the joint type: Local matrix L = Ljoint(φ1,φ2,…,φN) Then, world matrix W is computed by concatenating L with the world matrix of the parent joint World matrix W = L · Wparent It is convenient to have a 3D offset vector r for every joint which represents its pivot point relative to its parent’s matrix L offset 1 0 0 rx 0 1 0 ry 0 0 1 rz 0 0 0 1 It is nice to be able to limit a DOF to some range (for example, the elbow could be limited from 0º to 150º) Usually, in a realistic character, all DOFs will be limited except the ones controlling the root One can then adjust each of the DOFs to specify the pose of the skeleton We can define a pose Φ more formally as a vector of N numbers that maps to a set of DOFs in the skeleton Φ = [φ1 φ2 … φN] A pose is a convenient unit that can be manipulated by a higher level animation system and then handed down to the skeleton Usually, each joint will have around 1-6 DOFs, but an entire character might have 100+ DOFs in the skeleton Rotational Hinge: 1-DOF Universal: 2-DOF Ball & Socket: 3-DOF ▪ Euler Angles ▪ Quaternions Free Screw Constraint Etc. Non-Rigid Scale Shear Etc. Translational Prismatic: 1-DOF Translational: 3-DOF (or any number) Compound Design your own... Rotation around the x-axis: 1 0 L Rx x 0 rx 0 cos x sin x ry 0 sin x cos x rz 0 0 0 1 Rotation around the y-axis: cos y 0 L Ry y sin y rx 0 1 0 ry sin y 0 cos y rz 0 0 0 1 Rotation around the z-axis: cos z sin z L Rz z 0 rx sin z cos z 0 ry 0 0 1 rz 0 0 0 1 Rotation around an arbitrary axis a: L Ra a x2 c (1 a x2 ) a x a y (1 c ) a z s a x a z (1 c ) a y s rx a x a y (1 c ) a z s a y2 c (1 a y2 ) a y a z (1 c ) a x s ry a x a z (1 c ) a y s a y a z (1 c ) a x s a z2 c (1 a z2 ) rz 0 0 0 1 For a 2-DOF joint that first rotates around x and then around y: cy s s x y L Rxy x , y c x s y rx 0 cx sx ry sy sxcy cxc y rz 0 0 0 1 Different matrices can be formed for different axis combinations For a 3-DOF joint that first rotates around x, y, then z: c y cz s s c c s x y z x z L Rxyz x , y , z c x s y c z s x s z rx cy sz sx s y sz cxcz cx s y sz sxcz ry Different matrices can be formed for different axis combinations sy sxcy cxc y rz 0 0 0 1 To build a matrix from a set of Euler angles, we just multiply a sequence of rotation matrices together: 1 0 R x R y R z 0 c x 0 s x 0 c y sx 0 c x s y c y cz sx s y cz cx sz c x s y c z s x s z 0 s y cz 1 0 s z 0 c y 0 c y sz sx s y sz cx cz cx s y sz sx cz sy sxc y c x c y sz cz 0 0 0 1 1-DOF translation along an arbitrary axis a: L Ta t rx 1 0 0 t ax 0 1 0 ry t a y 0 0 1 rz t a z 0 0 0 1 For a more general 3-DOF translation: L Txyz t rx 1 0 0 tx 0 1 0 ry t y 0 0 1 rz t z 0 0 0 1 q qx qy qz qw q q x2 q y2 q z2 q w2 1 q a x sin 2 a y sin 1 2q y2 2q z2 2q x q y 2q w q z L Q q 2q x q z 2q w q y rx 2 a z sin 2q x q y 2q w q z 1 2q x2 2q z2 2q y q z 2q w q x ry 2 cos 2 2q x q z 2q w q y 2q y q z 2q w q x 1 2q x2 2q y2 rz 0 0 0 1