Basics. Dealing with multiple vectors

Having one single vector can be fun and many times is already enough to make you feel smart and powerful and ready to take over the world. In reality, however, you rarely deal with one single vector, commonly you have 2, 3 or even more vectors. So its important to know how to manage many vectors or they might get out of your control and its not too much to fight wild and angry vectors.

First of, many times you have more then one vector affecting your object. Simple example is gravity: when your object is moving (it has nice clean movement vector), but at same time gravity is pulling your object back to the land. Of course gravity, as any other force can be declared as vector (its always directed toward the earth, in side view game it would be directed straight down). Now you have 2 vectors and you need to find the resulting vector so you can move your object. To add the vectors you add their x and y components separately:


And if you have more vectors, you can keep adding them all same way.


Sometimes it is useful to know how are 2 vectors directed, meaning would they both send our object in same direction or is one of the vectors heading east and other vector west.

dp = v1.vx*v2.vx + v1.vy*v2.vy;

We multiply x and y components of both vectors and add the results. This variable dp is called "Dot product of vectors v1 and v2". And dp is not another vector, its just a number, if its positive number, then both vectors go in same direction, but if its negative, then we have vectors in opposite directions.

Now lets say we have 2 vectors affecting the object and we would like to know how would vector v1 look like if it would be projected on the vector v2. It is extremely useful to be able to convert vector on any coordinates system (vector v2 and its normal create one coordinate system) so the projected components can be changed separately. Calculating vectors v1 projection on vector v2:


Projection is again vector and to find it, we multiply the dot product of both vectors (dp) with vectors v2 normalized unit-size vector (dx/dy).

Here I made example of projecting vectors. You can drag the points around and see how the projection components change:

Black line is the vector v1, blue lines are vector v2 and its normal, red and green lines are projections of v1 on v2 and normal of v2. You can download the source fla.

Next: Move. Accelerate.