Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CO1301 - Games Concepts Week 20 Matrices continued Gareth Bellaby.

Similar presentations


Presentation on theme: "1 CO1301 - Games Concepts Week 20 Matrices continued Gareth Bellaby."— Presentation transcript:

1 1 CO1301 - Games Concepts Week 20 Matrices continued Gareth Bellaby

2 2 Topics 1.Matrices continued 2.Efficiency 3.Numbers

3 3 Topic 1 Matrices continued

4 4 Matrices ●A model has an associated 4 x 4 matrix. x-axis y-axis z-axis position

5 5 3D Models in graphics ●Recorded as a 4x4 matrix. x-axis y-axis z-axis position

6 6 Matrices ●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 7 Matrix maths reminder ●Size is expressed as rrows by columns. ●Matrix addition. ●Scalar multiplication. ●Matrix multiplication.

8 8 Scalar multiplication ●Scalar multiplication is simply when each component of a matrix is multiplied by a single value, in effect scaling it.

9 9 Matrix Multiplication The location of the result in the new matrix is the position where the row and the column intersect.

10 10 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 A T. ●So a n x m matrix becomes an n x m matrix.

11 11 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.

12 12 Vectors ●A matrix can have just one column or just one row. ●A row or column matrix can be used to represent a vector. vector is:row matrix is: ●DirectX uses row vectors (OpenGL uses column vectors).

13 13 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.

14 14 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".

15 15 Rotation using a matrix

16 16 Topic 2 Efficiency

17 17 Efficiency ●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 18 Tree collision Collision is currently being implemented as: ●What could be done in order to reduce the computational cost?

19 19 Improving 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:

20 20 Improving 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.

21 21 Improving 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 22 Efficiency ●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 23 Partitioning 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 24 Partitioning 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 25 Deriviving 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 26 Spatial 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 27 Spatial 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.


Download ppt "1 CO1301 - Games Concepts Week 20 Matrices continued Gareth Bellaby."

Similar presentations


Ads by Google