Basics. What do vectors have?

While having 2 points and x/y components should be enough for every vector to make him happy, they have more useful properties. Each vector has length for example. Length of the vector is actual distance between start and end points. Since we already know its x/y components, we can very easily calculate its length:

v.len=Math.sqrt(v.vx*v.vx+v.vy*v.vy);

This is actually good old Pythagoras theorem from the good old Greek mathematician Pythagoras. You multiply x component with itself, multiply y component with itself, add the results up and find square root.

Vectors are quite nice because you dont need to use angles and sinuses and other scary stuff when you deal with vectors. But I know many people still love angles too and because those people, while being strange, are still people, we should be able to convert any vector to the angle/length format. Luckily such conversion is easy (yep, everything is easy so far):

angle=Math.atan2(v.vy, v.vx)

You probably know how Flash likes to measure all angles in radians, so if you want to make your movie clip always heads in the direction of its movement vector, you need to convert the angle in radians into degrees:

angledeg=angle*180/Math.PI;

Sometimes you have angle and you have the length and you would like to make vector from those, but you dont know how. No worry, you are not doomed, it is actually not too complicated (see, I didnt use word "easy" this time):

v.vx=v.len*Math.cos(angle);
v.vy=v.len*Math.sin(angle);

Which vector is normal?

It may be surprise to you, but not all vectors are normal. Again, pretty much like people, some are normal and others are little bit heading toward the forest. Unlike the people, every vector can be normalized. When normalized, we will find unit-sized vector with exactly same direction, but with length of 1.

v.dx=v.vx/v.len;
v.dy=v.vy/v.len;

By dividing both x and y component with vectors length we have found its unit-sized vector which like all things in vectors, has 2 components dx and dy. Unit-sized vector has length of 1 and you may want to test it. Really, use our friend Pythagoras to find length of dx and dy. If you are actually too lazy to test it, and you trust my word, then fine. Normalized vector only gives us the direction, it doesnt say much about the distance. Its like when you say the "railway station is that way", you also give direction, but not the actual distance to the station.

When dividing, you should be careful to make sure you are not dividing with zero. Vector can have zero-length and it would still be vector, but normalizing it can be difficult if you dont check the length first. Flash gives value of "Infinity" for the division with zero.

Vector also has "normals", those are vectors exactly perpendicular to our vector. Or if you are still stuck with angles, then normals are rotated 90 degrees. Since each vector has direction then 2 normals exist, right hand normal and left hand normal. Lets suppose you stand up (I know you are actually sitting and reading, and you probably like to sit and hopefully like what you are currently reading, but standing once in a while is good too) then your face is looking at the direction of vector. If you now would raise both hands up directly away from you, your hands would be showing the right and left hand normals.

Right hand and left hand normals can be calculated:

v.rx = -v.vy;
v.ry = v.vx; 
v.lx = v.vy;
v.ly = -v.vx; 

And to finish up, I have created little movie, where you can drag the points around and see how the vector changes:

Black line is the vector, blue line is unit vector, green line is right hand normal and red line is left hand normal. You can download the source fla too.

Next: Adding. Projecting.

Top