Download 12 Matrices - Games @ UCLAN

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Quaternions and spatial rotation wikipedia , lookup

Tensor operator wikipedia , lookup

Transcript
CO1301: Games Concepts
Lecture 12
Matrices
Dr Nick Mitchell (Room CM 226)
email: [email protected]
Material originally prepared by Gareth Bellaby
Reading
Rabin, Introduction to Game Development:
 4.1:
"Mathematical Concepts"
Van Verthe, Essential Mathematics for Games:
 Chapter 2: "Linear Transformations and Matrices"
 Chapter 3: "Affine Transformations".
Frank Luna, Introduction to 3D Game Programming with
DirectX 9.0c: A Shader Approach
 Chapter 2: "Matrix Algebra"
Wendy Stahler, Fundamentals of Math and Physcis for Games
Programmers, Pearson, Prentice Hall, 2006.
Model Positioning Reminder
Each model has three axes
definied: x, y and z.
The axes of the model are
local to the model.
The axes are relative to the origin of the model,
 i.e. ( 0, 0, 0 ).
Each model also has a position.
The
position is in world coordinates.
The position is the location of its local origin.
3D Models in graphics
Recorded as a 4x4 matrix.
x-axis
y-axis
z-axis
position
 xx
y
 x
 zx

 Px
xy
yy
xz
yz
zy
Py
zz
Pz
0

0
0

1
Matrices
Singular: matrix
Plural: matrices
A matrix is a rectangular table of numbers.
A matrix is composed of rows and columns.
1 5 2
3 2 7


 4 1 2
4 2 1 


7 5 3
 4 2
 3 2


3 5
Why are matrices important?
A console typically runs at 30 frames per second. A
PC game may run at 60 frames per second.
You therefore only have something in the region
of between 0.017 to 0.033 of a second to
perform all of the operations you need to carry
out for the game. This includes AI, game logic,
physics and, of course, updating and rendering
the scene.
Computer games need to be efficient.
Matrices are efficient.
Why are matrices important?
Matrices are an essential part of graphics.
Transformations are carried out using matrices.
Matrices are a key element of linear algebra and hence of
computer graphics.
A single matrix can express a complex transformation.
 For example, a single matrix can rotate a model in all
3 axes simultaneously, whilst at the same time moving
and scaling.
Matrices can be combined together.
 The resulting matrix will perform all of the operations
of the individual matrices but in one single
mathematical operation.
Rendering a model
How is a model rendered on the screen?
A
model is made up of polygons. In practice these
are triangles.
 A triangle is stored using the coordinates of each of
its 3 points - the vertices.
 (e.g. look at an .x file for vertex lists).
 The coordinates of the vertices are in local spacerelative to the origin of the model.
 Another way of saying this is that the coordinates
of the vertices of the triangles are in model space.
 The model is also rotated - along 3 different axis.
Rendering a model
So...
 Every
vertex needs to be transformed into world
space (3 rotations, a transformation, possibly
scaling).
The screen is 2D...
 The model needs projecting from 3D into 2D.
 The camera has an imaginary screen somewhere in
front of it. The model (now in world coordinates)
is converted from 3D space into the 2D space of
the screen. The model is projected onto this
screen.
Transformation
When you have a go with DirectX over summer you
will see that matrices are used to perform this
transformation: the world, view and projection
matrices.
 You will also see that the matrices can be combined
into a single matrix that carries out all of the maths
in one operation.
 There are calls within DirectX to do all of the
operations.
You need to understand the background.
You also need to understand the maths because some
of the more interesting graphics requires other types
of matrix operations.
Matrices
Most important thing to remember with matrices
is:
 Row
- Column
First manifestation of row-column is the size the
matrix. You write the numbers of rows first and the
number of columns second.
So a 2x3 matrix is composed of 2 rows and 3
columns.
1 1 2
3 2 1


Matrix Sizes
Rows by columns.
3x1:
3x2:
1 
3
 
0.5
1 1 
 3 2


2 1
1x3:
0.2
1 1
2x3:
1 1 2
3 2 1


4x4:
1
3

6

7
1
2
1
9
2
1
2
5
3

5
2

3
Matrix Addition
Matrices can be summed together.
Addition is done component by component (the
same way as vectors).
This only works if the matrices are the same size.
1 1 4 3 1  4 1  3 
3 2  2 2  3  2 2  2

 
 

Matrix Addition
3 3 4 3 7 6
3 2  2 2  5 4

 
 

2
3 1  1 0.3 1  3 3.3 2
2 1 2 1 4 2
3 2  2 1  5 3

 
 

1 1 2 1 3 2
Matrix Addition
 1  1 2 1  3 0
  3 2    2 2    1 4

 
 

Subtraction is identical to addition.
1 1 2 2  1  1
 2 2   2 2    0 0 

 
 

Scalar multiplication
Scalar multiplication is simply when each
component of a matrix is multiplied by a single
value, in effect scaling it.
1 1 2 2
2



3 2 6 4
32 4 1  6 12  3
Matrix Multiplication
Matrices can be multiplied together.
Remember with matrices:
 Row
- Column
The second manifestation of row-column is matrix
multiplication: when two matrices are multiplied
together the rows of the first matrix are combined
with the columns of the second matrix.
The number of columns in the first matrix has to be
equal to the number of rows in the second.
Step guide
1.
2.
3.
4.
5.
6.
7.
Take a row of the first matrix.
Take a column from the second matrix.
The row is rotated clockwise so that it sits above the
column.
The components of each will pair up, row component
with column component (if this does not happen then
the matrices cannot be multiplied).
Multiply the pairs together.
Sum all of the results.
The final result is one component of the new matrix.
Matrix Multiplication example
1 1 4 3
3 2 2 2



 1 4  1 2 1 3  1 2 


3  4  2  2 3  3  2  2
6 5


16 13
Matrix Multiplication
 The location of the
result in the new
matrix is the
position where the
row and the column
intersect.
Matrix Multiplication example
1 
 4  1 3  
  2 5 0  2


3
 4 1  1 2  3  3 


 2 1  5  2  0  3
11
 
8
Matrix Multiplication
Anyone notice what operation is being performed
here?
● It is the dot product of row i from A with
column j from B.
Multiplying a matrix by a compatible vector will
perform a linear transformation on the vector.
Multiplying matrices together will produce a single
matrix that performs their combined linear
transformations.
Matrix Multiplication
The size of the row must be equal to the size of the
column in order to do matrix multiplication.
Or to put this the other way around. The number
of columns in the first matrix has to be equal to the
number of rows in the second.
Examine the dimensions of the matrices. The two
inside numbers must be equal:
 2x3 . 3x1
The size of the resulting matrix will be the two
outside numbers. So the result of 2x3 . 3x1 will be a
2x1 matrix.
Properties of Matrix Multiplication
(AB)C = A(BC)
(A + B)C = AC + BC
● AB ≠ BA
● This is important in graphics because matrices are
multiplied and it is important to get the order right .
Transpose
● The transpose of a matrix simply interchanges the rows
and the columns of the matrix.
● It is important because vectors in graphics can be
ordered by rows (DirectX) or columns (OpenGL and
HLSL).
● Represented by a superscript T. The transpose of
matrix A is AT.
● So a nxm
matrix
becomes an
nxm matrix.
2 1 
3 6   2 3 0 
1 6  1




0  1
The Identity Matrix
The identity matrix is a special matrix. The identity
matrix is a square matrix that has zeros for all
elements expect along the main diagonal.
Multiplying a matrix by the identity matrix leaves
the matrix unchanged.
For example used as a starting point before
additional operations are added to a matrix.
1 0
0 1 


1 0 0
0 1 0 


0 0 1
1
0

0
0

0 0 0
1 0 0

0 1 0

0 0 1
Vectors
● A matrix can have just one column or just one row.
● A row or column matrix can be used to represent a
vector.
X
Y
Z
vector is:
row matrix is:
0.1,0.5,0.2
0.1
0.5 0.2
● DirectX uses row vectors (OpenGL uses column
vectors).
Transforming a Vector
If you multiply a row matrix by a 3x3 matrix, the
result is another row matrix.
The 3x3 matrix therefore acts to transform the
row matrix.
However, a 3x3 matrix can only produce some of
the transformations required in 3D graphics.
So another component is added at the end to
produce a row matrix with a length of 4. The
transformation matrix now be a 4x4 matrix.
Rotation using a matrix
Matrix multiplication is used to perform rotations.
Well start with Euler rotation. In year 3 you'll look
at a faster alternative.
Euler is pronounced "oiler".
x
y
 cos 
 sin 
z 1 
 0
 0

sin 
cos 
0
0
0 0
0 0
   x
1 0
0 1
y z 1
Rotation using a matrix
0
1
0 cos 
RotateX  
0  sin 
0
0

 cos 
 sin 
RotateZ  
 0
 0

0
sin 
cos 
0
sin 
cos 
0
0
0
0
1
0
0
cos 
 0
0
 RotateY  
0
 sin 
 0
1

0
0

0
1
0  sin 
1
0
0 cos 
0
0
0
0

0
1