Basics. Vectors, where do they come from?

If you have tried to make some games then its very likely that you have already tried to place and move movie clips on stage. Well then you already know how every movie clip has 2 coordinates: x and y. So you can say each point in the stage can be declared like this:

p={x:10, y:5};

Great, with 1 single line we have created new object called "p" (as Point) and not only have we declared it, but we have also managed to give it 2 properties "x" and "y". You can now access the properties any time you need like this:

p.x
p.y

So the point object p has its x property set to 10 and y property set to 5. You could now create movie clip and place the movie clip on those coordinates. Of course, you dont have to create and place movie clips, all the math will still work without any movie clips, we only use movie clips to illustrate things and usually in real game you still have something on stage that moves around or else it will be one very well coded but extremely boring game.

Do remember that Flash uses inverted y coordinate, in most universes known to man positive y coordinate heads up, but in Flash it heads down.

So, we have created 1 point and we can see how it can be placed on stage, its time we go and make something more difficult. We now make 2 points:

p0={x:10, y:5};
p1={x:12, y:6};

And before you even noticed you have made your first vector. Dont be ashamed, it can happen to anyone and its really quite normal that people make vectors. Vectors, you see, can be made with 2 points, point p0 is called "starting point" and point p1 (hold your breath) is "ending point". It is important to remember that vectors have directions like humans have feelings, and vector p0->p1 is not same vector as p1->p0, they are very much alike, they have same length and same points, but they have opposite directions. And our first-born declared nicely with all its glory:

v={
p0:{x:10, y:5},
p1:{x:12, y:6}
};

And we can even draw the vector with its points:

You may think now its all too easy and you may also think why the coffee sometimes tastes strange, but we are not finished yet. Vectors can also be made with starting point and direction components. Wait, dont throw the poor p1 point out of the Windows yet! We may need it later. So, how do we make vector without endpoint? Easy:

v={
p0:{x:10, y:5},
vx:2,
vy:1
};

Looks familiar? It should be, as this is exactly same vector as we made before. We have same starting point (check!) and we have made x component with value of 2 and y component with value of 1. This vector is saying (if vectors could talk) "start from the point with coordinates x=10 and y=5 and go 2 steps right and 1 step down". Its unbelievable, but if you do so, you end up in the exact spot where point p1 was. In fact, end point of every vector can be calculated from its starting point and its x and y components:

v.p1={};
v.p1.x=v.p0.x+v.vx;
v.p1.y=v.p0.y+v.vy;

And, in case you start with vector that has 2 points, you can always find its x and y components:

v.vx=v.p1.x-v.p0.x;
v.vy=v.p1.y-v.p0.y;

From now on, whenever we talk about some vector, we know it has 2 points p0 and p1, both points also have x/y coordinates and vector has x/y components.

If you have starting point and vector to move the object then this vector can be called with many different names, all meaning basically same: "speed", "velocity", "movement", "displacement". Dont be confused by all the names, they still just move the object in x and y directions.

Next: Length. Normals.

Top