1 co1301 - games concepts week 20 matrices continued gareth bellaby
TRANSCRIPT
1
CO1301 - Games ConceptsWeek 20
Matrices continued
CO1301 - Games ConceptsWeek 20
Matrices continued
Gareth BellabyGareth Bellaby
2
TopicsTopics
1. Matrices continued
2. Efficiency
3. Numbers
3
Topic 1Topic 1
Matrices continued
4
MatricesMatrices
●A model has an associated 4 x 4 matrix.
1
0
0
0
zyx
zyx
zyx
zyx
PPP
zzz
yyy
xxxx-axis
y-axis
z-axis
position
5
3D Models in graphics3D Models in graphics
●Recorded as a 4x4 matrix.
1
0
0
0
zyx
zyx
zyx
zyx
PPP
zzz
yyy
xxxx-axis
y-axis
z-axis
position
6
MatricesMatrices
●A matrix is a rectangular table of numbers.
●A matrix is composed of rows and columns.
● In C++
float matrix[4][4] =
{ { 0.2, 0.4, 0.1, 0 },
{ 1, 1, 1, 0 },
{ 0.3, 0.3, 0.3, 0 },
{ 10, 3, 7, 1 } };
7
Matrix maths reminderMatrix maths reminder
●Size is expressed as rrows by columns.
●Matrix addition.
●Scalar multiplication.
●Matrix multiplication.
8
Scalar multiplicationScalar multiplication
●Scalar multiplication is simply when each component of a matrix is multiplied by a single value, in effect scaling it.
46
22
23
112
31261423
9
Matrix MultiplicationMatrix Multiplication
The location of the result in the new matrix is the position where the row and the column intersect.
10
TransposeTranspose
● 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.
161
032
10
63
12●So a n x m
matrix becomes an n x m matrix.
11
The Identity MatrixThe 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.
10
01
100
010
001
1000
0100
0010
0001
12
VectorsVectors●A matrix can have just one column or just one
row.
●A row or column matrix can be used to represent a vector.
ZYX
2.05.01.0 2.0,5.0,1.0
vector is: row matrix is:
●DirectX uses row vectors (OpenGL uses column vectors).
13
Transforming a VectorTransforming 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.
14
Rotation using a matrixRotation 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".
1
1000
0100
00cossin
00sincos
1 zyxzyx
15
Rotation using a matrixRotation using a matrix
1000
0100
00cossin
00sincos
1000
0cos0sin
0010
0sin0cos
1000
0cossin0
0sincos0
0001
RotateZ
RotateYRotateX
16
Topic 2Topic 2
Efficiency
17
EfficiencyEfficiency
●Last week I talked about the need for efficiency. The time available to perform operations within a computer game is small (0.017 to 0.033 seconds per frame) and so efficiency is a dominant concern within games programming.
● If you have 10,000 trees. You need to perform collision detection for all of the trees. That's a lot of processing.
18
Tree collisionTree collision
Collision is currently being implemented as:
occured. hascollision athen
tree, theof radius distance If
distance 222
zyx
●What could be done in order to reduce the computational cost?
19
Improving efficiencyImproving efficiency
●Firstly, can the collision detection be made more efficient?
●Examine the calculation. Collision detection depends upon a distance check. The most expensive element of the calculation is the square root. But it can be dispensed with:
2222
222
(distance)
distance
zyx
zyx
20
Improving efficiencyImproving efficiency
●Rewrite the check so that the square is no longer used.
●Square both sides of the equation.
●Perform the check against the square of the tree radius.
occured. hascollision athen
, tree) theof (radius (distance) If
(distance)
distance
22
2222
222
zyx
zyx
21
Improving efficiencyImproving efficiency
●The square of the radius can be pre-calculated so it costs nothing.
●A simple rewriting of the calculation has improved matters.
●The use of squares is common within graphics, e.g. for distance tests.
22
EfficiencyEfficiency
●Could anything else be done to improve efficiency?
●Does the program have to check all of the trees?
●Most of the trees are so far away that we can ignore them. It is possible to devise a method so that only likely candidates are checked.
23
Partitioning the worldPartitioning the world
●Grid out the world.
●Record the trees according to which grid they are in.
●Only check those trees which are in the same grid as the player.
●10 by 10 grid.
●Each grid has 100 trees within it.
24
Partitioning the worldPartitioning the world
●This example will use clear coordinates and scaling, but note that the principle holds true for all contexts. Each cell of the grid willl be 100 units by 100 units.
●Need an efficient way to derive the current grid of the player. Remember that integer devision within C++ produces an integer result and that the result is always rounded down. Divide the player coordinates (x and z) by 100. The result is the grid reference.
25
Deriviving the grid cellDeriviving the grid cell
●player location ( 40, 30 )
=> ( 40/100, 30/100 )
=> ( 0, 0 )
●player location ( 140, 130 )
=> ( 140/100, 130/100 )
=> ( 1, 1 )
●player location ( 450, 70 )
=> ( 450/100, 70/100 )
=> ( 4, 0 )
26
Spatial PartitioningSpatial Partitioning
● In programming terms you could create a 2D array which corresponds exactly to the grid coordinates. Each cell could then have the locations of its own trees stored.
●This is an simple example of a technique called "spatial partitioning", i.e. partitioning or sub-dividing space into chunks. Spatial partitioning is used extensively within computer games, e.g. in a game level so that only nearby models or visible models are rendered.
27
Spatial PartitioningSpatial Partitioning
●For example, it is possible to extend the grid approach to include a check to see whether the trees get rendered.
●There's a nice version of this later on in Frank Luna, Introduction to DirectX, in Chapter 18 when he uses a grid to filter out geometry that is not in view of the camera.
●More sophisticated techniques include Binary Space Partioning (BSP) trees and Oct-trees.