QBXL Technical Articles

(Mostly) Everything about Vectors!

By SJ Zero

Vectors are one of the concepts in math whose name is so cool you imagine it just HAS to be difficult. All the great concepts of math have names that sound like Roman Gods or alien invaders; Vector in invader, Calculon(ed: I think that's calculus) the destroyer, dot product the accountant! These are the concepts whose legendary frightfulness keeps children from running away from home, and whose reputation is largely undeserved.

The opposite of a vector is a scalar, a number without a direction, which represents only a magnitude is called a scalar. 2 litres of milk. 100 kilometers (without saying WHERE these kilometers are). 9.81 meters per second squared. degrees by itself is a scalar.

A Vector is a number with a magnitude and a direction. If you've been programming longer than a week, you've already dealt with them and you don't even know it. A set of X, Y coordinates describing the motion of an object can be considered a vector. A scalar whose negative sign represents a direction can be a vector, like the distance from a reference point. (ie. -1cm away from 0 and 1cm away from 0 meaning different things). The concept of a vector is really simple.

More often than not though, when someone actually refers to a vector as a vector, you're looking at a vector with a magnitude r and a direction theta, which can be in radians or degrees. That's mostly because most people don't think of vectors as any number with a direction and magnitude. That's ok though, because these are fairly easy to do as well:

The format for a 2d vector can be expressed in the following way:

r<theta
100cm<61 degrees
50km<11 rad
900 parsecs<11 gradians

A 3d vector has 2 angles; one for the X dimension, and one for the Z dimension.

Vector math has been made complicated by enough people that I won't try to. I'll just give you the basic formulas to work with vectors.

Basically, a vector can resolve to two formulas, keeping in mind that :

x = R * COS(theta in radians)
y = R * SIN(theta in radians)

That's all there is to it. If you were using vectors in a game to control the movement of a car, you might add the above values to the position of the car each frame, for instance:

car.x = r * cos(theta in radians) + car.x
car.y = r * sin(theta in radians) + car.y

And that's pretty much all there is to it. Adding another dimension adds a Z to the mix, but luckily the axes are independant, so it just means adding another variable to the mix.

Sometimes you'll want to add or subtract vectors. You should NOT do this by adding the magnitudes and directions together! The best way to do this is by changing them into X and Y coordinates, then adding the X and Y coordinates together and turning the result back into the form you want. Behold:


Here we convert both vectors into X, Y form:
X1 = r1 * cos(theta1 in radians)
X2 = r2 * cos(theta2 in radians)
Y1 = r1 * sin(theta1 in radians)
Y2 = r2 * sin(theta2 in radians)

Now we do the operation:

newX = x1 + x2
newY = y1 + y2

Finally, we change back to polar form:

newtheta = arctan(newy/newx) <- that may need to be flipped...y/x or x/y?
newR = (newX^2+newY^2)^.5

and we have our new vector in polar form, ready to go. It's all just that easy! I hope this is enough to give you some ideas.


--SJ Zero has been using vectors for years without realizing it.