- [Instructor] In game development,
we use vectors to define meshes, directions
and all manner of other calculations,
which makes them essential to understand.
A vector is a line drawn between two points,
vectors also have a length known as their magnitude.
Let's start off by simply looking at 2D vectors.
A 2D vector is a way of representing a point
from the origin, the 0,0 point on a graph,
to any point on a 2D plane.
Since it is from the origin, it has an implied direction.
It's made up of two components, X and Y,
these represent the distance from zero
along the X and Y in each axis.
In this example, our vector goes from the origin
to position 12, 5, the length of the distance
between these points is called the magnitude,
this can be worked out mathematically
by using Pythagoras' theorem,
which states that the square of the hypotenuse
is equal to the sum of the squares on the other two sides.
The hypotenuse in vector maths
is the magnitude that we're trying to find.
Imagine that there are two people in a field,
called Charles and Quentin, with a lethal vendetta.
Being gentlemen, they've agreed to a pistol duel,
but their guns only have a range of 12 units,
so can they shoot one another?
Let's find out.
As you can see, the magnitude is the square root
of the sum of the X and Y positions on the grid squared,
which, when added together, makes 169,
the square root of 169 is 13, so Charles and Quentin
will be unable to shoot one another, which is excellent,
as violence has no place in this tutorial.
Also, Charles totally had a crossbow the whole time.
Let's look at another example
of using vectors to solve a problem in 2D space.
We've already learned that vectors can be used
to denote a position in space relative to the origin point,
but you should also know that a moving object
has a velocity, a change in position over time,
this can also be expressed as a vector.
In this diagram, Frederick is at position 5, 6
and has a velocity of 12, 5 per hour,
this means that he will travel in a direction
that is 12 further in the X-axis
and five further in the Y-axis,
so, in order to find his new position after one hour,
we'd add his current position vector to his velocity vector
to discover where he will end up, 17, 11.
Remember that all vectors are expressed
relative to the origin, the zero point in space
and this is no different for velocity vectors
in the same way that 5, 6
is a position relative to the zero position,
12, 5 is a velocity relative to zero motion,
so whilst he may still have the same velocity,
17, 11 is Frederick's new position vector after one hour.
This is useful in game development tasks
that involve prediction, you should note
that the components of the final position
are equal to the sum of the components of the two vectors,
5 + 12 is 17 in the X-axis
and 6 + 5 is 11 in the Y-axis.
This also applies to subtraction,
three-dimensional vectors work the same as 2D,
but extrapolated into the Z-axis, which represents depth.
The X and Z axes make up a horizontal plane
and Y is the direction that points up.
Unity works on a left-handed coordinate system,
this means that if you hold your left hand
with your index finger pointed upwards
and your thumb pointed out to form an L shape
and your middle finger pointing away from you,
then the thumb represents the X-axis,
the index finger, the Y-axis
and the middle finger, the Z-axis.
This hand gesture can be helpful
to remind you of the order of the axes X, Y and Z.
Given that Z represents depth,
in our previous example, Charles and Quentin
would have actually been stood on the X/Z plane,
keeping in mind that any coordinates stated in 3D
will be ordered X/Y/Z,
Quentin would have been stood at 0, 0, 0, the 3D origin
and Charles at 12, 0, 5.
To make things more three-dimensional,
we could imagine that Charles was on a higher vantage point,
now stood upon his seven units or podium,
Charles is at position 12, 7, 5.
The calculation to find the magnitude of the vector
between Charles and Quentin is the same as in 2D,
but now features the Z-axis.
Unity makes it easy to perform calculations like this
with its helper function, Vector3.magnitude,
see the documentation linked below for more details.
There are a couple of useful functions for 3D vectors,
the Dot and Cross Products.
The Dot Product takes two vectors
and produces a scaler, a single value, from them.
To find the Dot Product from two vectors,
we take their component parts,
the X, Y and Z values and multiply them together
to find the sum of the resultant values.
This is expressed, for example,
as (AxBx) + (AyBy) + (AzBz),
with this product you can find out information
about the two vectors you've specified.
One example of this is finding out
whether the two vectors are perpendicular to one another,
if the Dot Product of two vectors equals zero,
the vectors are perpendicular.
In this example, we have two vectors, A and B,
vector A is 0, 7, 1, B is 0, 0, 6.
We can see in this example
that the sum of the multiplied components is six,
which means that the vectors are not perpendicular.
If vector A was 0, 7, 0 and B was 0, 0, 6,
then we can see that the Dot Product equals zero
and the vectors are indeed perpendicular.
Here is another example
of using the Dot Product in this way,
this time vector A is 4, 5, 11 and B is -3, -2, 2.
Multiplying these components give us -12, -10, 22,
the sum of which is zero,
so these two vectors are also perpendicular.
An example of using the Dot Product
could be when creating a flight simulator,
you would check the world up vector
with the forward vector of the plane,
if the two were perpendicular,
if the Dot Product equaled zero,
then the plane should have the least amount of drag.
As the Dot Product increases in a positive value,
we would know that the plane is pulling up
and we could add more drag.
If the Dot Product increases in negative values,
we would know that the plane is in a dive.
Unity has a helper function
to perform Dot Product calculations easily,
for details of this, see the documentation link below.
The Cross Product is a different way
of combining two vectors,
instead of producing a scaler, a single value,
the Cross Product produces another vector, specifically,
a vector that is perpendicular to the original two.
For example, if we took vector A and vector B
and crossed them, then the Product would be vector C,
one that is perpendicular to A and B,
this is mathematically denoted
by the caret symbol, the up arrow shown here.
Since Unity's coordinate system is left-handed,
so is its Cross Product,
putting your left hand into the same pose
as to determine the alignment of the axes,
you can determine the direction of a Cross Product vector.
In this example, your thumb and index finger
represent vectors A and B, the known vectors
and your middle finger represents
the Cross Product result, C.
To look into the Cross Product in detail,
we would arrange our vector A and B components like this,
but how does it work mathematically?
To work out the Cross Product,
we'd place our two vectors next to each other
and then repeat this below.
>From here, we can combine the components
one at a time to find the Cross Product.
First, we multiply Ay and Bz,
we then subtract Az multiplied by By,
this gives us the X component of the Cross Product.
Next, we do the same with Az and Bx
and Ax and Bz to give us the Y component.
Finally, we do the same again with Ax and By
and Ay and Bx to get the Z component.
The good news is that whilst this is a detailed calculation,
Unity has another helper function to perform this for you,
it looks like this.
One example of using the Cross Product
is finding the axis around which to apply torque
in order to rotate a tank's turret.
Given that you have the direction
that the turret is currently facing
and the direction that it needs to face,
you would cross these two vectors
to find the axis around which to apply rotational torque.