VERC: Vectors: An Introduction Last edited 5 years ago2019-04-21 13:07:54 UTC

You are viewing an older revision of this wiki page. The current revision may be more detailed and up-to-date. Click here to see the current revision of this page.
When you want to precisely define the motion of an object, how would you do it? More than likely, you’d give a direction and speed at which it is moving in that direction, as in “the car is moving at 30 m.p.h. due north.” Now, if I was to tell you to give me exactly the same definition but with a vector, what would you do? If you didn’t know what a vector was, then you may just stare blankly and say “uhh” a few times. This tutorial aims to tell you exactly what a vector is, what you can do with them, and how they are used in 3D graphics and physics.

Without vectors, we wouldn’t have moving objects – we wouldn’t have a game. Nothing could move, rotate, skew, scale, or go bang without vectors. So, what the hell are they? Well, they are essentially a direction and a size in any spatial N-dimensions – although all you need to worry about are 2D and 3D vectors. For the purposes of this tutorial, they will be discussed as 3D vectors unless otherwise specified.

To express a vector, 3 points are given – a coordinate in world space. Picture an arrow going from the origin (at the coordinate 0,0,0) to a position in 3D space, and that’s a vector – although it’s not physically an arrow, they are drawn on diagrams as arrows. The length of the arrow is what’s called its magnitude (in the context of physics, its speed, or a distance) and the direction it points in is, well, the vector’s direction.

In an equation or formula, a vector is shown as a bold, capital letter with half an arrow over the top (sometimes, the half-arrow is omitted):
User posted image
Vectors can be written in a couple of different ways, with the main one being:
User posted image
where x, y and z are the 3 components of the vector. There is another notation where a vector is shown as a one-column, three-row matrix:
User posted image
and another where it can be shown as a one-row, three-column matrix (called the transpose of the column vector, shown above):
User posted image
In practice, you don’t need to know exactly what they are used for in the latter two forms (until you start learning matrix mathematics) – all that matters at this moment in time is that they are the 3 main ways used to define a vector.

Vectors can be treated like any other mathematical object – so, they have certain operations that can be done on them, such as addition, subtraction, multiplication, and division. When you add two vectors together, you just add together the respective components, like so:
User posted image
Subtracting one vector from another is exactly the same principle. When you add two vectors together, you end up with a vector that is at the point that is reached when you place one vector onto the end of the other.

Another thing you can do is to multiply or divide a vector by a scalar value (you cannot multiply or divide a vector by another vector) like so:
User posted image
Again, dividing is the same principle. Multiplying and dividing a vector by scalar values will increase or decrease its length, a useful operation.

So, you’ve got your vector, but what do you do with it? Well, earlier I mentioned that a vector has magnitude and direction. The direction is obvious, but getting the magnitude requires some manipulation – specifically, a 3D version of Pythagoras’ theorem (the square on the hypotenuse is equal to the sum of the squares on the other two sides). The length of a vector, shown by two bar lines on either side of the vector’s symbol, is defined by:
User posted image
The three items within the square root are the three components of the vector, squared.

Now you can get the length of your vector, and you know its direction, but what if you want to change the length of the vector and keep the direction? You need to change the vector so that it has a magnitude of 1 – and to do that, you must normalize the vector. To normalize a vector, you divide the vector by its magnitude:
User posted image
This will give you a vector of length 1, providing the original vector was not of length 0 (i.e. it was 0,0,0). When a vector is of magnitude one, it is called a unit vector. When you have a unit vector, you can multiply it by the length you want it to be, or you can perform operations on it as is (unit vectors are very useful, as you will find out later).

Now we start to get into more vector-specific operations. We’ll start with the dot product, as it’s one of the most useful operations you can do. It is defined by:
User posted image
As you can see, this gives you a scalar value. So what use is this? Well, take a look at this:
User posted image
Alpha (the weird-looking ‘a’ – a Greek letter) is the angle between the two vectors on the 3D plane that has the origin, and the two vectors lying on it. This means that we can use the dot product to calculate the angle between two vectors, and therefore how far apart they are. If both vectors are unit-length (see earlier on normalization) then the equation simplifies to:
User posted image
Giving:
User posted image
As you can guess, this is an exceedingly useful tool in 3D graphics, and one that is used in all kinds of ways (one notable one being determining if a polygon faces away from or towards the camera).

Another extremely useful vector operation is the cross product. It takes two vectors and gives another vector, perpendicular to the input two. It is defined by:
User posted image
Again, this is used widely in 3D graphics, two uses being calculating the normal vector (not to be confused with normalization) of a triangle (the vector that is perpendicular to its plane) and finding a third axis given two others.

So, that’s all well and good, but how can these be applied in a practical manner? Well, let’s take a practical example to show how vectors are invaluable in physics. Imagine we have a plane flying along in the air, in a specific direction, at a specific speed. As you have probably guessed, we can represent this with a vector. In this sort of situation, we would normally store a scalar value for the speed, and a unit-length vector for the direction. We’ll call the vector V, and the speed s. We’ll also assume that the plane has a position stored (vectors can also be used to simply store positions, as they store exactly the same information as a 3D coordinate), which we’ll call P. Consider this:
User posted image
In the above equation, t is the current time, in seconds. So what does this mean? Well, it means that the object’s position at time t is equal to its speed multiplied by the current time, multiplied by its direction. (If you’re not sure about this, do some diagrams on paper using 2D vectors, and vary the speed and time.)

So, using vectors, we are already able to show the position of an object at any moment in time (providing it has a constant speed – an exercise would be to modify it so that it has constant acceleration over time). However, in our world, there are other forces that affect a moving object – one of them being gravity.

Gravity can also be represented as a vector, pointing downwards. The longer the vector, the stronger gravity is. We’ll call our gravity G, and assume that for a downward gravitational pull the Y component of the vector is negative. This gives us our new formula:
User posted image
We’re now making use of vector addition to pull the position towards the ground (or whatever direction gravity is pulling in) over time. You can add other things, such as air resistance, but they all follow the same principle.

All the principles discussed in here are widely used within 2D and 3D graphics, and physics. Without vectors, we’d be a bit stuck – we would have no measure of velocity and no measure of direction. Hopefully this tutorial has enlightened you as to what vectors are and how they can be used.

Notes for advanced readers

Only 3-dimensional vectors were discussed in this document. However, vectors can be n-dimensional, as mentioned at the beginning. Here are the formulae for all of the operations discussed above (except for the cross product, which is hard to interpret as a formula in an arbitrary number of dimensions except as a determinant, which is beyond the scope of this document). In all formulae, n is the number of dimensions, and a number subscript of a vector variable indicates the nth component:

Vector addition:
User posted image
Vector multiplication:
User posted image
Vector magnitude:
User posted image
Vector dot product:
User posted image

References

Mathematics for 3D Game Programming and Computer Graphics, Eric Lengyel, Charles River Media, 2002
This article was originally published on the Valve Editing Resource Collective (VERC).
TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.

Comments

You must log in to post a comment. You can login or register a new account.