Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been trying to get the hang of vectors and I read Wolfire's blog about linear algebra and I seem to understand most stuff except when it comes to storing a direction as a vector and using it by multiplying the velocity by it. I know it has to be normalized (have a length of 1), but why does it have to be 1 specifically and how would I use it?

Also how would I represent it in 2D [x,y] coordinates. For example if I wanted a direction such as North-East?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

A 2D vector has two values (x and y), and it basically says how far you go from the point of origin in the x- and in the y-direction. For example, a vector of (3,4) goes 3 units in x direction and 4 units in the y direction, resulting in an angled line with a length of 5 (3² + 4² = 9+16 = 25, root of that is 5). So the vector basically gives you two pieces of information:

1) Direction and

2) length

If you now want to calculate where an object moves, you take direction * speed, but you have to normalise the vector to the length of 1 so it doesn't skew your results. If you used the above vector as is, it would multiply your speed by 5 (its length), so your object would move five times as fast as intended. If you have a normalised vector with a length of 1, the 1 is neutral in multiplications and doesn't cause you any trouble.

I'm sure there are more technical explanations, but that's the gist of it.

share|improve this answer
1  
Regarding the North-East-Question: This is a question of definition. If you say North is up/+y and East is right/+x, the vector (3,4) would point North-East, while the vector (3,-4) would point South-East. Both would have the (positive) length of 5 (squaring deletes the minus). –  Larethian Sep 1 at 21:33
    
Thanks. As I mentioned in my comment to Philip's answer, I must have missed the part about North-East. –  Christian Sep 1 at 21:39
1  
@Christian Thanks for the answer and explaining clearly –  user51213 Sep 2 at 16:51

Christian's answer already covers most of what you asked for expect your concrete example from the end of your question about representing "North-East":

First of all, it depends on your coordinate system. But if we look onto your world from the top and assume that North/South is the y-axis (+y = North) and West/East is the x-axis (+x = East), you could represent the direction "North-East" as the (un-normalized) vector [1, 1] or the (normalized] vector:

enter image description here

Example visualization in WolframAlpha.

share|improve this answer
    
Thanks, I kind of missed that part about North-East. –  Christian Sep 1 at 21:36
Why does it have to be 1 specifically?

It doesn't. It's just a standard. If you want so in your game, you can make all your vectors of length 2. I wouldn't call it normalized though because normalization has a mathematical definition of its own.

As long as you have all your vectors in the game standard (all of them of unit length N, doesn't matter what N is, but 1 is pretty standard), you could still do the direction*speed technique.

All that said, I don't know anyone who uses other unit lengths other than 1. I just want you to know that it doesn't have to be so, it's just much easier for everyone.

How would I use it?

A normalized vector would signify direction. That makes it convenient to mash up with speed.

For every frame, instead of doing position = newPosition or position = position + movementVector, you could generalize everything with position = position + direction*speed.

TL;DR

It's just a standard and it makes things easier for everyone.

share|improve this answer
2  
In other words, what number is there that is easier to multiply by than 1 (well, apart from zero because that'd be quite useless in this context)? ;) –  tangrs Sep 2 at 4:57
    
Exactly. It's simply the most convenient thing to do : normalize vectors to unit 1 length. –  Mark Gabriel Sep 2 at 5:00
1  
This is slightly incorrect, and perhaps misleading. If you do not normalize, the math with still go through, but the results will distorted. Note Christian's answer, where he explains why normalization is important. Maybe the effect ends up being what you want for your game, but this is not solid mathematics. It's about being the multiplicative identity, not just being "easy". Multiplication by 2 is quite easy, but being off by a factor of 2 would still skew your output. –  RavensKrag Sep 2 at 10:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.