There are two powerful mathematical tools that we can use with numerous vectors to give us a lot of
extra information about how they lie in space with respect to one another.
They are the DOT product and the cross product.
In this lecture we will be examining the DOT product and how it can be used to turn a character towards
a goal location.
The DOT product is the sum of the multiplication of the corresponding components or coordinate values
in two vectors.
Given the vectors V and W shown here, the DOT product is found by multiplying both x values together,
both y values together, and then adding the result.
It is written as an equation like this with a single dot between the vector names.
This is not a full stop, but an actual dot that sits above the line.
The full equation then appears thus v dot x multiplied by w x plus v y multiplied by w y.
So taking the values from the vectors we have in this case the equation becomes three times six plus
three times negative one.
The DOT product in this case is 15.
Have a go for yourself.
What's the DOT product of these two vectors?
Zero three and negative four two.
The equation becomes zero multiplied by negative four.
Which are the x coordinates plus three multiplied by two?
Which are the y coordinates?
This results in a final value of the DOT product for these two vectors being six.
Now you're probably sitting there thinking, so what?
What does this value we are calculating mean?
Well, it tells us a few things about the relative orientations of the vectors.
Bear with me for a moment and work out the DOT products for each of these pairs.
Pause the video while you do it.
Write them down.
How did you go?
The DOT product for a is ten.
The DOT product for B is zero and the DOT product for C is negative seven.
Now take a moment to have a look at each of the values that you've got for the DOT products and how
the pairs of vectors are orientated with respect to one another.
Can you notice anything?
Well, in the case of a, the DOT product is a positive value and the angle between the vectors is less
than 90 degrees.
In B, the vectors are at 90 degrees to each other and the DOT product is zero.
For C, the vectors make a greater than 90 degrees angle and the DOT product is less than zero.
So just to recap, if the DOT product is greater than zero, then the angle between the vectors is less
than 90 degrees.
If the DOT product is equal to zero, then the angle is exactly 90 degrees.
And if the DOT product is less than zero, then the angle must be greater than 90 degrees.
Where is this useful in games?
Well, it allows us to determine where one game object is in relation to another game object.
Let's say we have a tank and the fuel, the vector showing the forward direction of the tank is the
direction that the tank is basically looking in.
If you draw another vector to the fuel from the tank, you can then use the DOT product to determine
if the fuel is in front or behind the tank.
And this very same principle can be used if you have, for example, an automatic weapon or you have
some kind of camera that wants to detect whether the character has walked into a certain space.
In this case, if the fuel is in sight of the tank, the DOT product of these vectors will be greater
than zero.
However, if the fuel is behind the tank, then the dot product will be less than zero.
We will revisit these principles later on, but for now, what we're most interested in is turning the
tank to face the fuel before it moves towards it.
So the first problem we are faced with is to find the actual angle between the forward facing direction
of the tank and the direction to the fuel.
The relationship between the DOT product and the angle between the vectors can be expressed like this,
where the derivation of this formula is taken from trigonometry and included in the article attached
to this lecture.
This can be further rearranged to make the angle the focus of the equation so that you can write a method
that will return the angle between the vectors.
And in this case, it works out to be the inverse cosine of the product of the vectors divided by their
length multiplied.
Now, if all of this math looks a little bit too complicated and abstract for you, don't worry.
We're going to go through an exercise and we will take it step by step.
So in order to find the angle that our tank needs to turn towards the fuel, first of all, we need
our two vectors.
We need the one that indicates the direction in which the tank is facing, and we need the one between
the tank and the fuel.
Now to find the one in which the tank is facing is pretty easy because in unity it stores it within
the tank itself.
And that particular vector is in this case, the up vector on our tank.
If you were in a 3D environment, which we will be a little later on when we're looking at this type
of angle, turning in our eye characters, then the forward direction that the character is facing is
actually along the Z axis.
So you just have to be careful about which one of those unity transform axes you get hold of to use.
Okay, so this time we're using that up.
Now in order to find the vector between the tank and the fuel, we actually take the fuels position
and we minus the tank's position.
The result is a vector and it's the vector from the tank to the fuel.
So let's start implementing some of this stuff and we're going to do it back in our drive script that
we have on our tank.
So let's open that up.
And once inside of there, we're going to create a new calculate angle method.
Now in here, what we're going to do is create a calculate angle and we'll do that down where we've
got our space bar being hit.
So calculate angle and now we'll put it up in.
Side of him.
So void.
Calculate angle and what we'll do in here.
I'll get you started, but then leave it to you to work out.
First of all, we need to create a vector which will be our tank forward, and that will actually be
a vector three, not a vector two.
That's going to equal, as I showed you in the video, transform dot up in this case instead of like
the forward facing vector.
Because in this case, because we're in 2D, the forward facing vector is the up vector for our tank.
So that is the tank forward.
Then we need a vector three and we need the fuel direction and that's going to equal the fuels position.
So fuel transformed position minus transform dot position like that.
So we now have our two vectors.
Often when you start out doing a lot of mathematical calculations, especially these things with vectors,
they might not be an intuitive way to think about things and therefore your ability to visualize them
might not be as good as if you'd been using them for a long time.
So one little tool I like to use is actually the debug draw ray in here, which will just help you to
see if you've got the right values.
So we need the tank forward to be the forward vector of the tank and the fuel direction to go between
the tank and the fuel.
So we're going to put in some debug dot draw arrays.
So debug dot, draw ray.
And this is just going to draw a little bit of a line on the screen for you so that you can see.
So it's this dot transform dot position and again, I'm using this when I don't need it.
Again, it's just habit for me.
You don't have to put it in there if you don't want to.
So this will be our tank forward from the tank's position and we're going to make it in color dot green
and we'll have it come up for 2 seconds so that we can see it.
Then we want our debug dot draw ray for this dot transformer position as the starting position for our
ray.
And then we're going to draw it to the fuel direction and then color dot red for 2 seconds.
Okay.
Once you've got that, let's just save it and we'll go back into unity and try this out.
So let's press play and once we're in here, we can hit the spacebar.
And you can see that we have that red line there.
Now, where is our forward vector?
Well, it is actually there.
If I can zoom in close up here.
Let's go and have a look.
See, there's the Ford vector in green.
Can you see it on your screen?
You might have to zoom in a bit closer.
There it is.
Now, why is it so tiny while the forward vector or the up vector or even the right vector of any game
object is actually normalized, which means it's a length of one.
That's why it's so short in there.
But that's okay.
We can still see it.
If you actually wanted that to be a bit bigger and show up on the screen, then you could multiply this
tank forward here by say, ten, and it would be a length of ten and you'd be able to see it.
But anyway, this is just so that we can basically visualize these equations and make sure that we've
got the right values.
Okay, so I'm going to leave the rest of this code to actually come up with the angle and print it out
as a challenge for you at the end of this video.
So to find the angle, the formula is this.
So it's the arc cosine, which is what cosine to the negative one means of our v w so one vector product
with the other divided by the length of both vectors multiplied together.
Okay, work through it piece by piece.
Don't get overwhelmed by the whole equation.
Give it some thought.
So when we come back in the next video, we'll continue this and I'll show you how to decipher this
equation if you've got issues with it and get our angle printing out.