Matrices.pptx

advertisement
THE MATH LECTURE
(Part I - Matrices)
INTRODUCTION
•
For 2D games, we use a lot of trigonometry
•
For 3D games, we use a lot of linear algebra
•
Most of the time, we don’t have to use calculus
•
A matrix can:
• Translate (move) a vertex
• Rotate a vertex
• Scale a vertex
•
Math libraries cover up these details
•
Learn it anyway!
INTRODUCTION
•
For 2D games, we use a lot of trigonometry
•
For 3D games, we use a lot of linear algebra
•
Most of the time, we don’t have to use calculus
•
A matrix can:
• Translate (move) a vertex
• Rotate a vertex
• Scale a vertex
•
Math libraries cover up these details
•
Learn it anyway!
MATRICES
•
The foundation of all geometric operations (translation, rotation, scaling, skewing…)
•
Have multiple rows and columns (usually 3x3 or 4x4)
•
Below is an identity matrix 𝑀
1.0
0.0
0.0
0.0
•
0.0
1.0
0.0
0.0
0.0
0.0
1.0
0.0
0.0
0.0
0.0
1.0
We can multiply a matrix
• with another matrix, and get a matrix
• with a vector of “appropriate” dimension (later)
TRANSPOSING A MATRIX
•
Have a transpose (denoted 𝑀 𝑇 ) where 𝑀 𝑇 𝑖𝑗 = 𝑀𝑗𝑖
•
In other words, make the rows columns!
π‘Ž
𝑑
𝑔
𝑏
𝑒
β„Ž
𝑐
𝑓
𝑖
Transpose
π‘Ž
𝑏
𝑐
𝑑
𝑒
𝑓
𝑔
β„Ž
𝑖
TRANSPOSING A MATRIX
•
Have a transpose (denoted 𝑀 𝑇 ) where 𝑀 𝑇 𝑖𝑗 = 𝑀𝑗𝑖
•
In other words, make the rows columns!
•
Mirror image along the diagonal
π‘Ž
𝑑
𝑔
𝑏
𝑒
β„Ž
𝑐
𝑓
𝑖
Transpose
The Matrix has you…
π‘Ž
𝑏
𝑐
𝑑
𝑒
𝑓
𝑔
β„Ž
𝑖
MATRICES
•
There’s also an inverse matrix (denoted 𝑀 −1 ) where 𝑀𝑀 −1 = 𝐼
• Not all matrices are invertible
• Can check by getting the determinant of the matrix (looking for non-zero)
𝑑𝑒𝑑
π‘š11
π‘š21
π‘š31
π‘Ž
𝑐
π‘š12
π‘š22
π‘š32
𝑏
π‘Ž
=
𝑑
𝑐
𝑏
= π‘Žπ‘‘ − 𝑏𝑐
𝑑
π‘š13
π‘š
π‘š23 = π‘š11 22
π‘š32
π‘š33
π‘š23
π‘š21
−
π‘š
12 π‘š
π‘š33
31
π‘š23
π‘š21
+
π‘š
13 π‘š
π‘š33
31
π‘š22
π‘š32
MATRICES
•
There’s also an inverse matrix (denoted 𝑀 −1 ) where 𝑀𝑀 −1 = 𝐼
• Not all matrices are invertible
• Can check by getting the determinant of the matrix (looking for non-zero)
𝑑𝑒𝑑
π‘š11
π‘š21
π‘š31
π‘Ž
𝑐
π‘š12
π‘š22
π‘š32
𝑏
π‘Ž
=
𝑑
𝑐
𝑏
= π‘Žπ‘‘ − 𝑏𝑐
𝑑
π‘š13
π‘š
π‘š23 = π‘š11 22
π‘š32
π‘š33
π‘š23
π‘š21
−
π‘š
12 π‘š
π‘š33
31
π‘š23
π‘š21
+
π‘š
13 π‘š
π‘š33
31
π‘š22
π‘š32
MATRICES
•
There’s also an inverse matrix (denoted 𝑀 −1 ) where 𝑀𝑀 −1 = 𝐼
• Not all matrices are invertible
• Can check by getting the determinant of the matrix (looking for non-zero)
𝑑𝑒𝑑
π‘š11
π‘š21
π‘š31
π‘Ž
𝑐
π‘š12
π‘š22
π‘š32
𝑏
π‘Ž
=
𝑑
𝑐
𝑏
= π‘Žπ‘‘ − 𝑏𝑐
𝑑
π‘š13
π‘š
π‘š23 = π‘š11 22
π‘š32
π‘š33
π‘š23
π‘š21
−
π‘š
12 π‘š
π‘š33
31
π‘š23
π‘š21
+
π‘š
13 π‘š
π‘š33
31
π‘š22
π‘š32
MATRICES
•
There’s also an inverse matrix (denoted 𝑀 −1 ) where 𝑀𝑀 −1 = 𝐼
• Not all matrices are invertible
• Can check by getting the determinant of the matrix (looking for non-zero)
𝑑𝑒𝑑
π‘š11
π‘š21
π‘š31
π‘Ž
𝑐
π‘š12
π‘š22
π‘š32
𝑏
π‘Ž
=
𝑑
𝑐
𝑏
= π‘Žπ‘‘ − 𝑏𝑐
𝑑
π‘š13
π‘š
π‘š23 = π‘š11 22
π‘š32
π‘š33
π‘š23
π‘š21
−
π‘š
12 π‘š
π‘š33
31
π‘š23
π‘š21
+
π‘š
13 π‘š
π‘š33
31
π‘š22
π‘š32
MATRICES
•
There’s also an inverse matrix (denoted 𝑀 −1 ) where 𝑀𝑀 −1 = 𝐼
• Not all matrices are invertible
• Can check by getting the determinant of the matrix (looking for non-zero)
𝑑𝑒𝑑
π‘š11
π‘š21
π‘š31
π‘Ž
𝑐
π‘š12
π‘š22
π‘š32
𝑏
π‘Ž
=
𝑑
𝑐
𝑏
= π‘Žπ‘‘ − 𝑏𝑐
𝑑
π‘š13
π‘š
π‘š23 = π‘š11 22
π‘š32
π‘š33
π‘š23
π‘š21
−
π‘š
12 π‘š
π‘š33
31
Follow the white rabbit…
π‘š23
π‘š21
+
π‘š
13 π‘š
π‘š33
31
π‘š22
π‘š32
A FEW SPECIAL MATRICES
(USED FOR ROTATION)
1
0
0
0 cos(𝛼) −sin(𝛼)
𝑅π‘₯ 𝛼 =
0 sin(𝛼) cos(𝛼)
0
0
0
cos(𝛽) 0 sin(𝛽)
0
1
0
𝑅𝑦 𝛽 =
−sin(𝛽) 0 cos(𝛽)
0
0
0
0
0
0
1
0
0
0
1
cos(𝛾) −sin(𝛾) 0
sin(𝛾) cos(𝛾) 0
𝑅𝑧 𝛾 =
0
0
1
0
0
0
0
0
0
1
MATRIX MULTIPLICATION
•
Multiplying a point by a matrix gives us a new point!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(𝛾)
sin(𝛾)
0
0
−sin(𝛾)
cos(𝛾)
0
0
0
0
1
0
0
0
0
1
1
?
0
?
=
0
?
1
?
Old New
point point
MATRIX MULTIPLICATION
•
Multiplying a point by a matrix gives us a new point!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
1
0
0
?
=
0
?
1
?
= (1 ∗ cos 90 ) + (0 ∗ − sin 90 ) + 0 ∗ 0 + 0 ∗ 1 = 0
0
-1
MATRIX MULTIPLICATION
•
Multiplying a point by a matrix gives us a new point!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
1
0
0
1
=
0
?
1
?
= (1 ∗ sin 90 ) + (0 ∗ cos 90 ) + 0 ∗ 0 + 0 ∗ 1 = 1
MATRIX MULTIPLICATION
•
Multiplying a point by a matrix gives us a new point!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
= (1 ∗ 0) + (0 ∗ 0) + 0 ∗ 1 + 0 ∗ 1 = 0
1
0
0
1
=
0
0
1
?
MATRIX MULTIPLICATION
•
Multiplying a point by a matrix gives us a new point!
•
Let’s say that we want to rotate the point [1 0 0] around the z axis by 90 degrees
•
Points are always stored with a 1 for the 4 th element
cos(90) −sin(90) 0 0
sin(90) cos(90) 0 0
0
0
1 0
0
0
0 1
= (1 ∗ 0) + (0 ∗ 0) + 0 ∗ 0 + 1 ∗ 1 = 1
What IS real?
1
0
0
1
=
0
0
1
1
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
1
0
0
0
0
1
0
0
0 π‘₯
0 𝑦
1 𝑧
0 1
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then:
1
0
0
0
0
1
0
0
0 2
0 1
1 −3
0 1
10
?
5
?
=
−7
?
1
?
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then:
1
0
0
0
0
1
0
0
0 2
0 1
1 −3
0 1
10
10 + 2
5
?
=
−7
?
1
?
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then:
1
0
0
0
0
1
0
0
0 2
0 1
1 −3
0 1
10
10 + 2
5
5+1
=
−7
?
1
?
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then:
1
0
0
0
0
1
0
0
0 2
0 1
1 −3
0 1
10
10 + 2
5
5+1
=
−7
−7 − 3
1
?
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then:
1
0
0
0
0
1
0
0
0 2
0 1
1 −3
0 1
10
10 + 2
5
5+1
=
−7
−7 − 3
1
1
Why, oh why, didn’t I take the blue pill?
TRANSLATION
•
It’s a piece of cake, because the 4 th column is the translation!
•
Imagine we want to move the point 10 5 − 7 by (2, 1, -3). Then:
1
0
0
0
0
1
0
0
0 2
0 1
1 −3
0 1
10
10 + 2
12
5
5+1
6
=
=
−7
−7 − 3
−10
1
1
1
SCALING
•
It’s a piece of cake too, because it’s the diagonal!
•
We can scale along just one axis, or more than one!
•
Imagine we want to scale the point 2 9 4 by the values x, y and z. Then:
π‘₯
0
0
0
0
𝑦
0
0
0
0
𝑧
0
0
0
0
1
2π‘₯
2
9𝑦
9
=
4
4𝑧
1
1
Trace program: running
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 sin(𝛽)
1
0
0 cos(𝛽)
0
0
0
0
0
1
1
0
0
0
0 π‘₯
1 𝑦
0 𝑧
0 0
0
? ?
0
? ?
=
? ?
0
? ?
1
?
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 sin(𝛽)
1
0
0 cos(𝛽)
0
0
0
0
0
1
1
0
0
0
0 π‘₯
1 𝑦
0 𝑧
0 0
0
cos(𝛽) ?
0
?
?
=
0
?
?
1
?
?
?
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 sin(𝛽)
1
0
0 cos(𝛽)
0
0
0
0
0
1
1
0
0
0
0 π‘₯
1 𝑦
0 𝑧
0 0
0
cos(𝛽) 0 ?
0
?
? ?
=
0
?
? ?
1
?
? ?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0
1
0
0
sin(𝛽)
0
cos(𝛽)
0
0
0
0
1
1
0
0
0
0
1
0
0
π‘₯
𝑦
𝑧
0
0
cos(𝛽)
0
?
=
0
?
1
?
0 π‘₯cos 𝛽 + 𝑧sin(𝛽)
?
?
?
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0
1
0
0
sin(𝛽)
0
cos(𝛽)
0
0
0
0
1
1
0
0
0
0
1
0
0
π‘₯
𝑦
𝑧
0
0
cos(𝛽)
0
?
=
0
?
1
?
0 π‘₯cos 𝛽 + 𝑧sin(𝛽) 1
?
?
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0
1
0
0
sin(𝛽)
0
cos(𝛽)
0
0
0
0
1
1
0
0
0
0
1
0
0
π‘₯
𝑦
𝑧
0
0
cos(𝛽)
0
0
=
0
?
1
?
0 π‘₯cos 𝛽 + 𝑧sin(𝛽) 1
?
?
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0
1
0
0
sin(𝛽)
0
cos(𝛽)
0
0
0
0
1
1
0
0
0
0
1
0
0
π‘₯
𝑦
𝑧
0
0
cos(𝛽)
0
0
=
0
?
1
?
0 π‘₯cos 𝛽 + 𝑧sin(𝛽) 1
1
?
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0
1
0
0
sin(𝛽)
0
cos(𝛽)
0
0
0
0
1
1
0
0
0
0
1
0
0
π‘₯
𝑦
𝑧
0
0
cos(𝛽)
0
0
=
0
?
1
?
0 π‘₯cos 𝛽 + 𝑧sin(𝛽) 1
1
𝑦
?
?
?
?
?
?
?
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 sin(𝛽)
1
0
0 cos(𝛽)
0
0
0
0
0
1
1
0
0
0
0 π‘₯
1 𝑦
0 𝑧
0 0
cos(𝛽)
0
0
0
=
0
−sin(𝛽)
1
0
There is no spoon…
0
1
0
0
π‘₯cos 𝛽 + 𝑧sin(𝛽)
𝑦
−π‘₯sin 𝛽 + 𝑧cos(𝛽)
0
1
0
0
1
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 sin(𝛽)
1
0
0 cos(𝛽)
0
0
?
0
0
0
1
1
0
0
0
0 π‘₯
1 𝑦
0 𝑧
0 0
cos(𝛽)
0
0
0
=
0
−sin(𝛽)
1
0
0
1
0
0
π‘₯cos 𝛽 + 𝑧sin(𝛽)
𝑦
−π‘₯sin 𝛽 + 𝑧cos(𝛽)
0
1
0
0
1
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 π‘₯cos 𝛽
1
0 −π‘₯sin 𝛽
0
+ 𝑧sin(𝛽)
𝑦
+ 𝑧cos(𝛽)
0
1
0
0
1
𝑛𝑒𝑀𝑋
π‘œπ‘™π‘‘π‘‹
π‘›π‘’π‘€π‘Œ
π‘œπ‘™π‘‘π‘Œ
=
𝑛𝑒𝑀𝑍
π‘œπ‘™π‘‘π‘
1
1
MATRIX MULTIPLICATION
•
What if you want to rotate a point and then translate it?
•
Need a rotation matrix
•
Need a translation matrix
•
Returns a 4x4 matrix
cos(𝛽)
0
−sin(𝛽)
0
0 π‘₯cos 𝛽
1
0 −π‘₯sin 𝛽
0
+ 𝑧sin(𝛽)
𝑦
+ 𝑧cos(𝛽)
0
1
0
0
1
𝑛𝑒𝑀𝑋
π‘œπ‘™π‘‘π‘‹
π‘›π‘’π‘€π‘Œ
π‘œπ‘™π‘‘π‘Œ
=
𝑛𝑒𝑀𝑍
π‘œπ‘™π‘‘π‘
1
1
Old
point
New
point
JEFF WAS HERE
Download