GameDev.netVectors and Matrices: A Primer

Vectors and Matrices: A Primer
## PrefaceHey there! This tutorial is for those who are new to 3D programming, and need to brush up on that math. I will teach you two primary things here, Vectors and Matrices (with determinants). I'm not going to go into everything, so this isn't designed as a standalone reference. A lot of mathematics books can probably discuss this much better, but anyway, without further ado, lets get on with it shall we? ## Vectors## Vector basics What is a vector?Vectors are the backbone of games. They are the foundation of graphics, physics modelling, and a number of other things. Vectors can be of any dimension, but are most commonly seen in 2 or 3 dimensions. I will focus on 2D and 3D vectors in this text. Vectors are derived from hyper-numbers, a sub-set of hyper-complex numbers. But enough of that, you just want to know how to use them right? Good. The notation for a vector is that of a bold lower-case letter, like vector equations and column vectors. Vectors can also be written using the two end points with an arrow above them. So, if you have a vector between the two points A and B, you can write that as .A vector equation takes the form
The coefficients of the This diagram shows a vector from the origin to the point ( 3, 2, 5 ) in 3D space. The
This can also be related to the deltas of a line going through 2 points. The second way of writing vectors is as where There are various advantages to both of the above forms, but I will continue to use the column vector form, as it is easier when it comes to matrices. ## Vector MathYou can manipulate vectors in various ways, including scalar multiplication, addition, scalar product and vector product. The latter two are extremely useful in 3D applications. There are a few things you should know before moving to the methods above. The first is finding the in 3D and in 2D, where ## AdditionVector addition is pretty easy. All you do is add the respective components together. So for instance, take the vectors:
The addition of these vectors would be: Get it? This can also be represented very easily in a diagram, but I will only consider this in 2D, because it's easier to draw.
This works in the same way as moving the second vector so that its beginning is at the first vector's end, and taking the vector from the beginning of the first vector to the end of the second one. So, in a diagram, using the above example, this would be: This means that you can add multiple vectors together to get the ## SubtractingSubtracting is very similar to adding, and is also quite helpful. All you do is subtract the components in one vector from the components in the other. The geometric representation however is very different. Let and Then The visual representation of this is: Here, Where instead of having:
we have
which, according to what was said about the addition of vectors would produce: You can see that putting ## Scalar multiplicationScalar multiplication is easy to come to grips with. All you do is multiply each component by that scalar. So, say you had the vector
This has the effect of lengthening or shortening the vector by the amount To find the unit vector of this, we would divide That is the unit vector Scalar multiplication is also used in the vector equation discussed earlier. The constants ## The Scalar Product (Dot Product)The The definition the scalar product is the following: Where q is the angle between the 2 vectors The scalar product can also be written in terms of Cartesian components., I will not go into how this is derived, but the final, simplified formula of
We can now put these two equations equal to each other, yielding the equation: With this, we can find angles between vectors. This is used extensively in the lighting part of the graphics pipeline, as it can see whether a polygon is facing towards or away from the light source. This is also used in deciding what side of planes points are on, which is also used extensively for culling. ## The Vector Product (Cross Product)The
For those that are already familiar with determinants, the vector product is basically the expansion of the following determinant: For those that aren't, the vector product in expanded form is: Read " Since the cross product finds the perpendicular vector, we can say that:
Using scalar multiplication along with the vector product we can find the "normal" vector to a plane. A plane can be defined by two vectors, This first finds the vector perpendicular to the plane made by This is a very important point. If you put the inputs the wrong way round, the graphics API will not produce the desired lighting, as the normal will be facing in the opposite direction. ## The Vector Equation of a Straight LineThe vector equation of a straight line is very useful, and is given by a point on the line and a vector parallel to it: Where In expanded form, the equation becomes: This is called the Using this to find the vector equation of a line through two points is easy: If Using the vector equation we can define planes, and test for intersections. I won't go into planes much here, as there are many tutorials on them elsewhere, I'll just skim over it. A plane can be defined as a point on the plane, and two vectors that are parallel to the plane, or: where Take the line: and the plane: To find the intersection point we simply equate, so that:
We then solve for There are many benefits for using the normal-distance form of a plane too. It's especially useful for testing what sides of a plane points or other shaped objects are. To do this, you dot the normal vector and the position vector of the point being tested, and add the distance of the plane from the origin. So, if you have the plane and the point and behind if it is < 0. If the result equals zero, the point is on the plane. This is used heavily in culling and BSP trees. ## Matrices## What is a Matrix anyway?A matrix can be considered a 2D array of numbers. They take the form: Matrices are very powerful, and form the basis of all modern computer graphics, the advantage of them being that they are so fast. We define a matrix with an upper-case bold type letter. Look at the above example. The dimension of a matrix is its height followed by its width, so the above example has dimension 3x3. Matrices can be of any dimensions, but in terms of computer graphics, they are usually kept to 3x3 or 4x4. There are a few types of special matrices; these are the The identity matrix can be any dimension, as long as it is also a square matrix. The zero matrix is a matrix that has all its elements set to 0. The Vectors can also be used in column or row matrices. I will use column matrices here so that it is easier to understand. A 3D vector which you can see is the same layout as using column vectors. ## Matrix arithmeticI won't go into every matrix manipulation, but instead I'll focus on the ones that are used extensively in computer graphics. ## Matrix MultiplicationThere are two ways to multiply a matrix: by a scalar, and by another conformable matrix. First, let's deal with the matrix/scalar multiplication. This is pretty easy, all you do is multiply each element by the scalar. So, let where this can also be written as: Multiplying a matrix by another matrix is more difficult. First, we need to know if the two matrices are Take three matrices You perform the multiplication by multiplying each row in
So, with that in mind, let's try an example:
It's as simple as that! Some things to note: A matrix multiplied by the identity matrix is the same, so: The The transpose of this matrix would be: Simple enough eh? And you thought it was going to be hard! ## DeterminantsI'm going to talk a little bit about determinants now, as they are useful for solving certain types of equations. I will discuss easy 2x2 determinants first. Take a 2x2 matrix: The determinant of a matrix For higher dimension matrices, the determinant gets more complicated. Let's discuss a 3x3 matrix. You pass along the first row, and at each element, you discount the row and column that intersects it, and calculate the determinant of the resultant 2x2 matrix multiplied by that value. So, for example, take this 3x3 matrix: Ok then, Step 1: move to the first value in the top row, Step1: Step2: We repeat this all along the top row, with the sign in front of the value of the top row alternating between a "+" and a "-", so the determinant of Now, how do we use these for equation solving? Good question. I will first show you how to solve a pair of equations with 2 unknowns. Take the two equations: We first You can see it's laid out in the same way, which makes it easy. Now, to solve the equation in terms of k, dividing the result by the original determinant. So, that would be:_{2}To solve for Let's try an example to see this working: We push the coefficients into a determinant, and solve: To find To find See, it's as simple as that! Just for good measure, I'll do an example using 3 unknowns in 3 equations: Solve for Solve for Solve for And there we have it, how to solve a series of simultaneous equations using determinants, something that can be very useful. ## Matrix InversionEquations can also be solved by inverting a matrix. Take the following equations again: We push these into 3 matrices to solve: Let's give these names such that: We need to solve for B, and since there is no "matrix divide" operation, we need to invert the matrix Now we need to know how to actually do the matrix inversion. There are many ways to do this, and the way I am going to show you is by no means the fastest. To find the inverse of a matrix, we need to first find its c The sign infront of c Where That's easy enough isn't it? Thought so J. Just do the same for every element, and build up the co-factor matrix. Done? Good. Now that the co-factor matrix has been found, the inverse matrix can be calculated using the following equation: Taking the previous example and equations, let's find the inverse matrix of First, the co-factor matrix and | So To solve the equations, we then do: We can then find the values of A matrix is called ## Matrices in computer graphicsAll graphics APIs use a set of matrices to define transformations in space. A transformation is a change, be it translation, rotation, or whatever. Using column a matrix to define a point in space, a ## Transformation MatricesMost graphics APIs use 3 different types of primary transformations. These are: - Translation
- Scale
- Rotation
I won't go into the derivation of the matrices for these transformations, as that will take up far too much space. Any good math book that explains affine space transformations will explain their derivations. You have to pre-multiply points by the transformation matrix, as it is impossible to post-multiply because of the dimensions. Therefore, a point ## TranslationTo translate a point onto another point, there needs to be a vector of movement, so that where In matrix form, this turns into: Where ## ScalingYou can scale a vertex by multiplying it by a scalar value, so that where In matrix form this is: ## RotationRotation is the most complex transformation. Rotation can be performed around the 3 Cartesian axes. The rotation matrices around these axis are: To find out more about how these matrices are derived, please pick up a good math book, I haven't got the time to write it here. Some things about these matrices though: Any rotation about an axis by q can be undone by a successive rotation by -q. So: Also, notice that the cosine terms are always on the top-left to bottom-right diagonal, and the sine terms are always on the top-right to bottom-left diagonal, we can also say that: Rotations matrices that act about the origin are orthogonal. Note that these transformations are cumulative. That is, if you multiplied a vertex by a translation matrix, then by a scale matrix, it would have the effect of moving the vertex, then scaling it. The order that you multiply becomes very important when you multiply rotation and translation matrices together, as ## Projection MatricesThese are also complicated matrices. They come in two flavours, Anyway, on to the matrices. The orthographic projection matrix: The The perspective correct projection matrix is: ## ConclusionWell, that's it for this tutorial. I hope that I've helped you understand vectors and matrices including how to use them. For further reading I can recommend a few books that I have found really useful, these are: Interactive Computer Graphics A Top Down Approach with OpenGL" Edward Angel: Covers a lot of theory in computer graphics, including how we perceive the world around us. This book covers a lot of the matrix derivations that I left out. All in all, a very good book on graphics programming and theory. With exercises too, which is nice. Mathematics for Computer Graphics Applications Second Edition M.E Mortenson: This is solely about the mathematics behind computer graphics, and explains a lot of material in a very easy to understand manner. There are loads of exercises to keep you occupied. The book explains things such as vectors, matrices, transformations, topology and continuity, symmetry, polyhedra, half-spaces, constructive solid geometry, points, lines, curves, surfaces, and more! A must for anyone serious in graphics programming. You won't see a line of code or pseudo-code though.
That's about it! I hope I haven't scared you off graphics programming. Most APIs, including Direct3D and OpenGL, will hide some of this away from you. If you need to contact me at all, my email address is: phil.dadd@btinternet.com. I don't want any abuse though - if you don't like this tutorial I accept constructive advice only. ## CreditsI'd like to give credit to "Advanced National Certificate Mathematics Vol.: 2" as that's where I got the simultaneous equations from in the part on determinants, so I knew the answers were whole, and that they worked out. I would also like to give credit to Miss. A Miller who proof read this tutorial for me.
© 1999-2011 Gamedev.net. All rights reserved. |