Friday, September 19, 2014

What is Vector2D, Vector2D magnitude, Vector2d.normalized? Learn all about Vectors and their normalized vectors, dot product and other necessary stuff.

Hi,

Today I will explain a little bit of Vectors and their properties. We have to learn a few things about Vectors, both in 2D and 3D because we do need them in Unity. By the way, I will give Vector2D functions and try to explain using them but these rules and functions also apply to Vector3 counterparts. Just replace Vector2D with "Vector3D" , the rest is all the same.

tip: learn this basic vector knowledge well, these will help you in the long run of Unity and game making business.

Lets summarry the main parts:
1) Vector2d logic and What Vector2.normalized and Vector2d magnitude value means?
2) Dot vector product.
3) How to and Why to compare them?

I will try to explain them step by step.


First of all, what is a normalized Vector means? 
By saying normalized vector, we are talking about a vector which has a length of 1 unit. It makes a vector bigger if it has a length smaller than 1 or makes a vector smaller if it's  original length bigger than 1.

Lets say we have a Vector2D named A, which is 2 points in x and 3 points in y, like this:
Vector2D A = new Vector2D(2,3);

if we get its normalized vector, we will use "normalized" function of Vector2D. Like this:
Vector2D B = A.normalized;

Now we get a B vector, which is just a smaller copy of the original A vector. BOTH vectors have the SAME DIRECTION! this is important to know. But normalized one is just a smaller copt of the original.

Because a normalized vector has a magnitude of 1. Always. Period.

Btw, is you dont know the term "magnitede" dont worry. Magnitude is just a fancy word for "length" in mathematics. So when you hear the word "magnitude" of a vector, just think it's LENGTH.

Lets be more clear, a normalized Vector has a length of 1, and has the same direction of the original vector.

here is a picture of a vector (shown as original vector) and it's normalized vector:



Lastly, some well-known normalized Vectors:

Vector2D.right : Vector2(1, 0) : a normalized vector to the right.
Vector2D.up : Vector2(0, 1) : a normalized vector to the up.

here is an imaginary images of  static normalized Vectors.



Now to the second step...

2) Vector DOT product: 

Dont tnik too much about this term. It just compares (usually) 2 normalized vectors. This is that dot product goodness is all about. You give 2 normalized (which means 2 vectors with a length of 1) to the dot function of Vector2D, and you get a comparizon of those 2 vectors. Since they do have the same lenght (they both have 1 unit lenght remember?) the only think that they can be compared to is their direction. When you Vector.Dot 2 vectors, you get a float number! So what does that float number symbolize, how can a comparison function gives you a float number and expects you to get that comparison from that number?

Well, The main trick here is how to read that float number.

First of all, dot function always gives a float number between -1 and +1. What ever you do, whichever 2 vectors you give, you will always get a  number between -1 and 1. So here comes the magic:

a) if 2 vectors  both do have exactly the same direction, then you get number +1;
b) if 2 vectors  have completely opposite directions, , then you get number -1;
c) if 2 vectors are perpendicular, then you get number 0.
d) you get different number  between +1 and -1 for other cases which gives us a clue of their position to each other.

are you getting the point?

You can compare two vectors directions by looking at Vector2D.Dot function, which the term comes "getting a dot product of two vectors".

An example image of Dot Product Results and different two vectors:




So you learned what s a magnitude, how to get a vectors normalized value, and how to compare two normalized vectors using Vector's Dot method (getting dot product and examining the float number) now you can do some neat stuff with this knowledge, like handlind and managing  SLIDE controllers.

On my next article, I will explain how can you capture slide gestures, tap and click on mobile screen and how to optimize slide and how to get which way user slides from and slides through.. Wont last long, so hope to see you soon.