WEBVTT

00:03.930 --> 00:09.150
There are two powerful mathematical tools that we can use with numerous vectors to give us a lot of

00:09.150 --> 00:13.570
extra information about how they lie in space with respect to one another.

00:13.590 --> 00:16.380
They are the DOT product and the cross product.

00:16.410 --> 00:22.740
In this lecture we will be examining the DOT product and how it can be used to turn a character towards

00:22.740 --> 00:24.090
a goal location.

00:24.120 --> 00:30.660
The DOT product is the sum of the multiplication of the corresponding components or coordinate values

00:30.660 --> 00:32.250
in two vectors.

00:32.280 --> 00:40.230
Given the vectors V and W shown here, the DOT product is found by multiplying both x values together,

00:40.380 --> 00:44.280
both y values together, and then adding the result.

00:44.400 --> 00:49.850
It is written as an equation like this with a single dot between the vector names.

00:49.860 --> 00:54.570
This is not a full stop, but an actual dot that sits above the line.

00:54.600 --> 01:05.160
The full equation then appears thus v dot x multiplied by w x plus v y multiplied by w y.

01:05.310 --> 01:12.150
So taking the values from the vectors we have in this case the equation becomes three times six plus

01:12.150 --> 01:14.370
three times negative one.

01:14.820 --> 01:17.850
The DOT product in this case is 15.

01:19.230 --> 01:20.530
Have a go for yourself.

01:20.550 --> 01:23.460
What's the DOT product of these two vectors?

01:23.460 --> 01:27.090
Zero three and negative four two.

01:35.000 --> 01:39.200
The equation becomes zero multiplied by negative four.

01:39.200 --> 01:44.480
Which are the x coordinates plus three multiplied by two?

01:44.480 --> 01:46.070
Which are the y coordinates?

01:46.100 --> 01:51.980
This results in a final value of the DOT product for these two vectors being six.

01:52.070 --> 01:54.890
Now you're probably sitting there thinking, so what?

01:54.920 --> 01:57.600
What does this value we are calculating mean?

01:57.620 --> 02:02.390
Well, it tells us a few things about the relative orientations of the vectors.

02:02.420 --> 02:08.090
Bear with me for a moment and work out the DOT products for each of these pairs.

02:08.120 --> 02:09.830
Pause the video while you do it.

02:09.830 --> 02:10.910
Write them down.

02:16.500 --> 02:17.670
How did you go?

02:17.820 --> 02:20.820
The DOT product for a is ten.

02:20.850 --> 02:27.180
The DOT product for B is zero and the DOT product for C is negative seven.

02:27.450 --> 02:35.070
Now take a moment to have a look at each of the values that you've got for the DOT products and how

02:35.070 --> 02:39.660
the pairs of vectors are orientated with respect to one another.

02:39.660 --> 02:41.970
Can you notice anything?

02:42.360 --> 02:48.960
Well, in the case of a, the DOT product is a positive value and the angle between the vectors is less

02:48.960 --> 02:50.460
than 90 degrees.

02:50.490 --> 02:57.060
In B, the vectors are at 90 degrees to each other and the DOT product is zero.

02:57.450 --> 03:04.950
For C, the vectors make a greater than 90 degrees angle and the DOT product is less than zero.

03:05.220 --> 03:12.360
So just to recap, if the DOT product is greater than zero, then the angle between the vectors is less

03:12.360 --> 03:13.620
than 90 degrees.

03:13.620 --> 03:19.320
If the DOT product is equal to zero, then the angle is exactly 90 degrees.

03:19.320 --> 03:25.680
And if the DOT product is less than zero, then the angle must be greater than 90 degrees.

03:26.010 --> 03:27.930
Where is this useful in games?

03:27.930 --> 03:33.120
Well, it allows us to determine where one game object is in relation to another game object.

03:33.120 --> 03:39.150
Let's say we have a tank and the fuel, the vector showing the forward direction of the tank is the

03:39.150 --> 03:42.120
direction that the tank is basically looking in.

03:42.120 --> 03:48.690
If you draw another vector to the fuel from the tank, you can then use the DOT product to determine

03:48.690 --> 03:52.380
if the fuel is in front or behind the tank.

03:52.380 --> 03:59.700
And this very same principle can be used if you have, for example, an automatic weapon or you have

03:59.700 --> 04:05.580
some kind of camera that wants to detect whether the character has walked into a certain space.

04:05.790 --> 04:11.730
In this case, if the fuel is in sight of the tank, the DOT product of these vectors will be greater

04:11.730 --> 04:12.510
than zero.

04:12.510 --> 04:17.850
However, if the fuel is behind the tank, then the dot product will be less than zero.

04:18.060 --> 04:23.940
We will revisit these principles later on, but for now, what we're most interested in is turning the

04:23.940 --> 04:27.870
tank to face the fuel before it moves towards it.

04:27.870 --> 04:34.530
So the first problem we are faced with is to find the actual angle between the forward facing direction

04:34.530 --> 04:37.710
of the tank and the direction to the fuel.

04:38.010 --> 04:44.160
The relationship between the DOT product and the angle between the vectors can be expressed like this,

04:44.160 --> 04:50.130
where the derivation of this formula is taken from trigonometry and included in the article attached

04:50.130 --> 04:51.120
to this lecture.

04:51.240 --> 04:56.790
This can be further rearranged to make the angle the focus of the equation so that you can write a method

04:56.790 --> 04:59.340
that will return the angle between the vectors.

04:59.340 --> 05:07.770
And in this case, it works out to be the inverse cosine of the product of the vectors divided by their

05:07.770 --> 05:09.480
length multiplied.

05:09.840 --> 05:16.290
Now, if all of this math looks a little bit too complicated and abstract for you, don't worry.

05:16.290 --> 05:20.250
We're going to go through an exercise and we will take it step by step.

05:20.250 --> 05:28.380
So in order to find the angle that our tank needs to turn towards the fuel, first of all, we need

05:28.380 --> 05:29.820
our two vectors.

05:29.820 --> 05:37.590
We need the one that indicates the direction in which the tank is facing, and we need the one between

05:37.590 --> 05:39.930
the tank and the fuel.

05:40.290 --> 05:49.110
Now to find the one in which the tank is facing is pretty easy because in unity it stores it within

05:49.140 --> 05:50.730
the tank itself.

05:50.730 --> 05:57.780
And that particular vector is in this case, the up vector on our tank.

05:57.900 --> 06:03.870
If you were in a 3D environment, which we will be a little later on when we're looking at this type

06:03.870 --> 06:11.010
of angle, turning in our eye characters, then the forward direction that the character is facing is

06:11.010 --> 06:13.050
actually along the Z axis.

06:13.050 --> 06:21.240
So you just have to be careful about which one of those unity transform axes you get hold of to use.

06:21.330 --> 06:23.430
Okay, so this time we're using that up.

06:23.430 --> 06:32.970
Now in order to find the vector between the tank and the fuel, we actually take the fuels position

06:33.120 --> 06:36.540
and we minus the tank's position.

06:36.570 --> 06:42.300
The result is a vector and it's the vector from the tank to the fuel.

06:42.870 --> 06:48.780
So let's start implementing some of this stuff and we're going to do it back in our drive script that

06:48.780 --> 06:50.430
we have on our tank.

06:50.430 --> 06:53.280
So let's open that up.

06:53.670 --> 07:01.560
And once inside of there, we're going to create a new calculate angle method.

07:01.560 --> 07:07.140
Now in here, what we're going to do is create a calculate angle and we'll do that down where we've

07:07.140 --> 07:08.880
got our space bar being hit.

07:08.880 --> 07:15.510
So calculate angle and now we'll put it up in.

07:15.600 --> 07:16.470
Side of him.

07:16.710 --> 07:17.670
So void.

07:17.940 --> 07:22.740
Calculate angle and what we'll do in here.

07:22.740 --> 07:26.220
I'll get you started, but then leave it to you to work out.

07:26.250 --> 07:33.240
First of all, we need to create a vector which will be our tank forward, and that will actually be

07:33.240 --> 07:36.300
a vector three, not a vector two.

07:36.390 --> 07:43.110
That's going to equal, as I showed you in the video, transform dot up in this case instead of like

07:43.110 --> 07:44.580
the forward facing vector.

07:44.580 --> 07:51.120
Because in this case, because we're in 2D, the forward facing vector is the up vector for our tank.

07:51.510 --> 07:53.610
So that is the tank forward.

07:53.610 --> 08:02.190
Then we need a vector three and we need the fuel direction and that's going to equal the fuels position.

08:02.190 --> 08:13.080
So fuel transformed position minus transform dot position like that.

08:13.320 --> 08:16.170
So we now have our two vectors.

08:17.010 --> 08:22.080
Often when you start out doing a lot of mathematical calculations, especially these things with vectors,

08:22.080 --> 08:28.290
they might not be an intuitive way to think about things and therefore your ability to visualize them

08:28.290 --> 08:31.380
might not be as good as if you'd been using them for a long time.

08:31.380 --> 08:38.760
So one little tool I like to use is actually the debug draw ray in here, which will just help you to

08:38.760 --> 08:41.310
see if you've got the right values.

08:41.520 --> 08:46.980
So we need the tank forward to be the forward vector of the tank and the fuel direction to go between

08:46.980 --> 08:48.630
the tank and the fuel.

08:48.630 --> 08:51.930
So we're going to put in some debug dot draw arrays.

08:52.080 --> 08:55.200
So debug dot, draw ray.

08:55.200 --> 08:59.490
And this is just going to draw a little bit of a line on the screen for you so that you can see.

08:59.490 --> 09:05.430
So it's this dot transform dot position and again, I'm using this when I don't need it.

09:05.430 --> 09:07.290
Again, it's just habit for me.

09:07.290 --> 09:09.720
You don't have to put it in there if you don't want to.

09:09.750 --> 09:18.360
So this will be our tank forward from the tank's position and we're going to make it in color dot green

09:19.110 --> 09:22.980
and we'll have it come up for 2 seconds so that we can see it.

09:22.980 --> 09:32.910
Then we want our debug dot draw ray for this dot transformer position as the starting position for our

09:33.360 --> 09:33.990
ray.

09:33.990 --> 09:43.380
And then we're going to draw it to the fuel direction and then color dot red for 2 seconds.

09:43.380 --> 09:43.680
Okay.

09:43.680 --> 09:49.470
Once you've got that, let's just save it and we'll go back into unity and try this out.

09:49.470 --> 09:55.290
So let's press play and once we're in here, we can hit the spacebar.

09:55.290 --> 09:59.310
And you can see that we have that red line there.

09:59.310 --> 10:02.580
Now, where is our forward vector?

10:02.580 --> 10:03.660
Well, it is actually there.

10:03.660 --> 10:06.210
If I can zoom in close up here.

10:06.240 --> 10:07.290
Let's go and have a look.

10:07.290 --> 10:09.000
See, there's the Ford vector in green.

10:09.000 --> 10:11.160
Can you see it on your screen?

10:11.160 --> 10:12.990
You might have to zoom in a bit closer.

10:12.990 --> 10:13.830
There it is.

10:13.830 --> 10:20.700
Now, why is it so tiny while the forward vector or the up vector or even the right vector of any game

10:20.700 --> 10:25.620
object is actually normalized, which means it's a length of one.

10:25.620 --> 10:27.870
That's why it's so short in there.

10:27.870 --> 10:28.410
But that's okay.

10:28.410 --> 10:29.640
We can still see it.

10:30.060 --> 10:36.720
If you actually wanted that to be a bit bigger and show up on the screen, then you could multiply this

10:36.720 --> 10:41.370
tank forward here by say, ten, and it would be a length of ten and you'd be able to see it.

10:41.370 --> 10:48.150
But anyway, this is just so that we can basically visualize these equations and make sure that we've

10:48.150 --> 10:50.220
got the right values.

10:50.220 --> 10:56.160
Okay, so I'm going to leave the rest of this code to actually come up with the angle and print it out

10:56.160 --> 10:59.820
as a challenge for you at the end of this video.

10:59.820 --> 11:05.160
So to find the angle, the formula is this.

11:05.160 --> 11:15.900
So it's the arc cosine, which is what cosine to the negative one means of our v w so one vector product

11:15.900 --> 11:23.880
with the other divided by the length of both vectors multiplied together.

11:23.880 --> 11:27.000
Okay, work through it piece by piece.

11:27.000 --> 11:29.940
Don't get overwhelmed by the whole equation.

11:29.940 --> 11:31.440
Give it some thought.

11:31.440 --> 11:37.140
So when we come back in the next video, we'll continue this and I'll show you how to decipher this

11:37.140 --> 11:41.610
equation if you've got issues with it and get our angle printing out.
