1 / 22

CO1301: Games Concepts

CO1301: Games Concepts. Lecture 13 Matrices + Efficiency. Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby. Matrix Multiplication. Matrices can be multiplied together. Remember with matrices: Row - Column

dolphin
Download Presentation

CO1301: Games Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CO1301: Games Concepts Lecture 13 Matrices + Efficiency Dr Nick Mitchell (Room CM 226) email: npmitchell@uclan.ac.uk Material originally prepared by Gareth Bellaby

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

  3. Step guide • 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.

  4. Matrix Multiplication example

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

  6. Matrix Multiplication example

  7. Matrix Multiplication • Anyone notice what operation is being performed here? • 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. • It is the dot product of row i from A with column j from B.

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

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

  10. Rotation using a matrix

  11. Topic 2 • Efficiency

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

  13. Tree collision • Collision is currently being implemented as: • What could be done in order to reduce the computational cost?

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

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

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

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

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

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

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

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

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

More Related