Vector Mathematics

In the wiki I found these lines of code:
[lua]local pos = self.Owner:GetShootPos()
local ang = self.Owner:GetAimVector()
pos = pos+(ang*80)[/lua]

This is the position 80 units in front of the player, but I dont understand how multiplying a vector by 80 gives a vector in front of the player.

Can anyone explain this to me?

And also: Would I for getting 80 units behind the player just divide? And for left and right?

I’m not going to try and explain what a normalized vector is 'cause I’m afraid I will just confuse you even more:eng99:

To get a position 80 units behind the player you multiply by -80
To get the right direction you do
ang = ang:Angle():Right()
And you guessed it, to get left you multiply by -80

I’m using 2D space to explain it because it’s more convenient, but thinking in 3D isn’t much harder.

First of all, you can see a vector as a difference of two positions. Let’s say you have a point A which is located at (1;5), and another point B which is located at (2;3), the vector which goes from A to B is the position of B minus the position of A, so (1;-2).

A position is nothing else than a vector, A is basically the vector which goes from the origin (0;0) to itself.

They are usually represented as arrows. The value of a vector is only the difference between both of its ends, so you can represent a vector anywhere, as long as it has the same length and points in the same direction, it’s the same vector.
On the above picture, every red arrow is the EXACT SAME VECTOR.

You can see this as a top down view of the map:

O is the origin of the map, basically, it’s position (0;0).
P is the player. So the red vector is basically what you get by calling self.Owner:GetShootPos().

The blue vector is what you get from self.Owner:GetAimVector(). Its length is exactly 1, because it’s quite convenient like that. A vector with a length of 1 is what people call a “normalized vector”.

If you calculate the red vector + the blue vector, you will get the position of the player + 1 unit forwards to where he is looking.

Now if you want to get 80 units forwards instead of 1, all you have to do is multiply the blue vector by 80.

red vector + (80 * blue vector)

If you have trouble visualizing how you can “add” vectors, look at this:

Let’s say you want to add the red vector with the green vector. Can’t really see how to do it there.
But hey, you can move them around in your mind, and their values will stay the same. So you can move the beginning of the green vector to the end of the red vector, and you have this:

Bam, you have the blue vector, which is the sum of the red and green vectors. It works if you put the beginning of the red vector to the end of the green vector too, it’s exactly the same.

If you multiply a vector by a number, the vector will keep the same direction, but it will just be longer. If you multiply it by 2, it will be twice as long. If you divide it by 2, it will be half as long. If you multiply it by -1, it will face the other way. And so on.

You can multiply a vector by another vector, but that’s a really particular case, and you shouldn’t need it right now.


Thanks guys, I (surprisingly) understood everything you explained here. Thanks a lot for this :smiley:

Wait, did you paint all the just for this thread? Awesome:buddy:

That deserves a space on the wiki methinks