At some point when creating a game of any complexity you will
probably have to deal with vectors. They are used in
physics, in AI, in trigonometry and many other situations, but what
is a vector? Well, to put it simply, a vector is a directed
quantity. Let's start by looking at a 1 dimensional vector, which
is just the same as a single number, by drawing a numbered line
with an arrow starting at zero and ending at 5. This is the vector
a which is equal to 5 and if we draw another arrow starting
at the 5 and ending at the 8 we have vector b which is equal
to 3.
You should realise that it doesn't matter where a vector
starts, all that matters is how long it is and what direction it
goes in. So vector b starts at 5, is 3 units long and points
to the "right", making it identical to a vector starting at 0 and
going to 3. Now, you can also add these vectors together, by
putting the two vectors a and b end to end to get the
vector c which is equal to 8. What about negative numbers?
Well, if, in the above image, a vector that points to the "right"
corresponds to a positive number, you can see that a vector
pointing to the "left" would correspond to a negative number,
making a one-dimensional vector nothing more than a signed (+/-)
number. This explains the essential concept of a vector: only
length and direction ("left" or "right" in this case) count, not
position.
So, what about 2 dimensional vectors? Well, we can think of them as
consisting not just of "left" and "right, but "up" and "down"
too:
Now, those are not actually vectors yet as we still have to
reduce them down using their start and end coordinates. Looking at
vector a we can see it has a start coordinate of [2,2] and
an end coordinate of [4,3] and so to get the vector from this we
need to reduce it down by subtracting the end coordinates from the
start coordinates like this: [(x2-x1), (y2-y1)] = [(4-2), (3-2)] =
[2,1]. Let's do the same for vector b now: [(-1.2 -(-3.2))
,(2.1 - 1.1)] = [2 ,1]. Notice something? Those two vectors are the
same! This is yet another demonstration that a vector has no
position, only direction and length, and we can draw those vectors
relative to each other around a local [0,0] axis:
This means that a 2D vector is defined by two values, an
x and a y position relative to the local [0,0] axis.
And what about 3D vectors? Well, they have the added dimension of
"depth" to contend with and would be calculated as positions
x, y and z around a local axis something like
this: