CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby Lecture 11 Cross Product & the Model Matrix
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 1: "Vector Algebra"
Vectors & Dot Product (revision) Cross Product
Vectors A vector is a directed edge. In 3 dimensions a vector has three components: v(x, y, z) For example the vector a = (2, 2, 0)
Length of a vector The length of a vector can be calculated from its components.
The normalised vector A normalised vector is a vector whose length is 1. Also known as the unit vector. A vector can be normalised by dividing each of its components by its length:
Dot product The dot product of two vectors v and w is: The relationship between two vectors can be calculated using the dot product. The dot product of two vectors expresses the angle between the vectors:
Cross Product The cross product (like the dot product) takes its name from the symbol used: a single cross between two vectors. Pronounced "The cross product of v and w ", or just the shorthand of "the cross of v and w ". The cross product takes two vectors and produces a single vector as its result.
Cross Product Illustration Vector v Vector w Cross Product v×w
Cross Product The cross product of two vectors is the vector which are orthogonal (perpendicular or at right-angles) to both of the vectors. There will always be two vectors which are orthogonal to our original two vectors: one sticking upwards and the other downwards (relative to the original vectors). The cross product is not commutative. The order of the vectors produces the direction.
Why is it important to you? What is interesting about the cross product? The cross product of two vectors v and w would produce the local axis of rotation between the two vectors. For example the cross product is used to construct a LookAt() function and to implement shader techniques such as normal mapping and parallax mapping.
Introducing Transformations
Model Positioning Reminder A 3D model has its own local space: Three 3D vectors X,Y & Z These are the local axes of the model. So when you move a model according to its local z axis you are calling up the local axes of the model in order to do the movement.
Model Positioning These local axes define the local rotation of the model, e.g. rotating around its local y-axis, etc. The following terms are less commonly used nowadays but just in case you come across them: rotation around the local X is pitch (up and down) rotation around the local Y is yaw (side to side) rotation around the local Z is roll
3D Models in graphics 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 ). The position of the model are its coordinates in world space. The position is the location of its local origin.
3D Models in graphics Every model has 4 items of data: Vector representing the x-axis (relative to model origin) Vector representing the y-axis (relative to model origin) Vector representing the z-axis (relative to model origin) Vector containing the coordinates of the model (relative to the world origin) Why is it done in this way? Is it possible to do this any other way?
3D Models in graphics Each model within the TL-Engine has a floating point array associated with it. The array is actually a matrix and we'll talk about matrices in a while. It can be considered to be a 4 by 4 array: float matrix[4][4];
3D Models in graphics The first row of the array is the model's local x-axis. The second row of the array is the model's local y-axis. The third row of the array is the model's local z-axis. The final row of the array are the model's coordinates. Ignore the final component of the vectors for now.
3D Models in graphics As a 4x4 array: Initialisation in C++: float matrix[4][4] = { { 0.2, 0.4, 0.1, 0.0 }, { 1.0, 1.0, 1.0, 0.0 }, { 0.3, 0.3, 0.3, 0.0 }, { 10.0, 3.0, 7.0, 1.0 } };
Transforming the model The matrix can be obtained using the GetMatrix() method. float matrix[4][4]; box->GetMatrix( &matrix[0][0] ); The fourth row of the array are the coordinates of the model. The fourth row is accessed by setting the first subscript of the array to 3. matrix[3][0]; // Px matrix[3][1]; // Py matrix[3][2]; // Pz
Transforming the model So doing the following would set the coordinates of the model to ( 4, 6, 3 ) matrix[3][0] = 4.0f; matrix[3][1] = 6.0f; matrix[3][2] = 3.0f; The matrix can be set using the SetMatrix() method. box->SetMatrix( &matrix[0][0] ); And the box (in this case) would move to location ( 4, 6, 3 ).
Scaling The axes also provide a scaling. The length of the vectors X, Y & Z can define the scaling in that axis 1.0 = normal, 2.0 = double size etc. Effectively scaling local space So if you obtain the matrix of the model and multiply the components of the first row by 2, you will double the size of the model along the x-axis.
Scaling To scale the model in all of its axes, you would need to multiply the second and third rows of the array in the same way as the first, the effect of this would be to reproduce the Scale() method: float matrix[4][4]; box->GetMatrix( &matrix[0][0] ); matrix[0][1] *= 2.0f; matrix[0][2] *= 2.0f; matrix[0][3] *= 2.0f; box->SetMatrix( &matrix[0][0] );
Introduction to Matrices
Matrices Singular: matrix Plural: matrices A matrix is a rectangular table of numbers. A matrix is composed of rows and columns.
Why are matrices important to you? Matrices are an essential part of graphics. Transformations are carried out using matrices. Matrices are key element of linear algebra and hence of computer graphics.
Matrices 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.
Matrix Sizes Rows by columns. 2x3: 3x2: 3x1: 1x3: 4x4:
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.
Matrix Addition
Subtraction is identical to addition.
Scalar multiplication Scalar multiplication is simply when each component of a matrix is multiplied by a single value, in effect scaling it.