WEBVTT

00:04.370 --> 00:09.680
A commonly used calculation in AI is working out the distance between two game objects.

00:10.100 --> 00:15.380
It's not only important in determining where things are in the environment with respect to one another,

00:15.380 --> 00:18.710
but also for calculating angles and normalizing vectors.

00:19.040 --> 00:24.200
You'll see the distance calculation used over and over again in this course, especially when trying

00:24.200 --> 00:28.880
to determine if an enemy is in range or where other resources are.

00:29.240 --> 00:33.110
To calculate a distance, you're going to need two points.

00:33.590 --> 00:38.110
These can be two separate locations or either end of a vector.

00:38.120 --> 00:45.110
In this case, on the screen I've got the tank that's at three on the X and two on the Y and the fuel

00:45.110 --> 00:50.210
cell that is at 12 on the X and ten on the Y.

00:50.240 --> 00:54.290
So how do we find the distance between these?

00:54.410 --> 01:01.370
Well, any two points in an environment, whether it be 2D or 3D, can be used to form a right angle

01:01.400 --> 01:03.420
triangle, as you can see here.

01:03.440 --> 01:09.440
Once you have a right angle triangle or that you know that it's possible to form a right angle triangle,

01:09.440 --> 01:14.360
which it always is in this case, then you can apply Pythagoras theorem.

01:14.390 --> 01:19.790
Now, do you remember what Pythagoras Theorem was from your high school mathematics?

01:19.940 --> 01:25.670
Well, recall for a moment, if you will, the longest side of a right angle triangle is called the

01:25.670 --> 01:28.610
hypotenuse and the other sides.

01:28.610 --> 01:35.210
If we label them A and B and it doesn't matter which order you do that in, then Pythagoras Theorem

01:35.210 --> 01:42.590
says that the length of the hypotenuse equals the square root of A squared plus B squared.

01:42.590 --> 01:46.910
So that's the lengths of A and B squared.

01:47.360 --> 01:49.710
And how do we find these lengths?

01:49.730 --> 01:58.430
Well, if you have a look at a, it happens to be the difference in the X direction of where the tank

01:58.430 --> 02:00.770
is and where the fuel cell is.

02:00.770 --> 02:09.950
And B is the difference between where the tank is and the fuel cell is in the Y direction.

02:10.540 --> 02:12.550
So what does this look like when we work it out?

02:12.560 --> 02:18.160
Well, first of all, we need to figure out what A is and A is going to be the difference between the

02:18.160 --> 02:21.130
X coordinates of the tank and the fuel.

02:21.130 --> 02:28.930
So X one minus x two and B is going to be the difference between the Y coordinates of the tank and the

02:28.930 --> 02:29.580
fuel.

02:29.590 --> 02:36.460
Now, we're going to do a squared operation on these so it doesn't matter what order you do them in.

02:36.460 --> 02:45.610
As long as if the tank is item one, then the fuel always remains item two so that the x and y coordinates

02:45.610 --> 02:49.360
have to be in the same order inside those equations.

02:49.660 --> 02:57.160
So then when we plug in the actual values, we get three -12 squared plus two minus ten squared, which

02:57.160 --> 03:00.370
gives us down the track 81 plus 64.

03:00.400 --> 03:02.680
The square root of that is 12.

03:02.680 --> 03:09.760
So that means that the length of the hypotenuse or the distance between the tank and the fuel in this

03:09.760 --> 03:13.120
case is 12.04.

03:13.120 --> 03:18.940
Now, it doesn't matter where in the environment your points are, you can always make a right angled

03:18.940 --> 03:19.540
triangle.

03:19.540 --> 03:26.230
But the important thing here is that that right angled triangle is going to have one side that is parallel

03:26.230 --> 03:30.250
to the x axis and one side is parallel to the y axis.

03:30.250 --> 03:36.190
And the length of those two sides are always going to give you that difference that you need to put

03:36.190 --> 03:38.200
in to Pythagoras Theorem.

03:39.190 --> 03:43.900
We're going to investigate distance calculations in 2D.

03:43.900 --> 03:50.560
So to do this, I want you to open up a new unity project and then bring in the start of file.

03:50.560 --> 03:51.910
Attash This lecture.

03:51.910 --> 03:59.650
If you aren't seeing the view that I've got here, you can click on this 2D button at the top and it

03:59.650 --> 04:02.800
should align your scene for you.

04:02.800 --> 04:05.650
Now over in the game view.

04:05.680 --> 04:13.480
I got this by setting my aspect ratio to 16 to 9, which will give you this.

04:13.480 --> 04:20.950
Now at this point, if we press play, you'll see that you can use the arrow keys to drive the tank

04:20.950 --> 04:21.400
around.

04:21.430 --> 04:28.210
Now, this drive script that's on here is exactly the same drive script we've used in the previous lecture

04:28.210 --> 04:31.120
for our little farmer villager.

04:31.330 --> 04:40.090
And what we're going to do is actually calculate the distance between the tank and the fuel that's up

04:40.090 --> 04:40.750
here.

04:40.870 --> 04:44.650
Okay, so let's open up the drive script.

04:45.070 --> 04:45.550
Okay.

04:45.550 --> 04:50.560
So you'll find that we have our updates occurring in update.

04:50.560 --> 04:56.140
And as we were talking about before, we could probably put these into late update though it's not going

04:56.140 --> 04:59.530
to make a big difference in this particular scenario.

05:00.160 --> 05:09.190
First of all, this script is attached to the tank, so the transform that's referred to inside of this

05:09.190 --> 05:10.810
script belongs to the tank.

05:10.810 --> 05:14.560
Therefore the transform position is the position of the tank.

05:14.770 --> 05:20.680
Now we're going to add in the fuel, so we're going to bring that in at the top as a game object.

05:20.680 --> 05:26.650
So public game object, fuel like that.

05:26.650 --> 05:27.060
Okay.

05:27.070 --> 05:36.880
So if we just hit save on that, switch back into unity for a moment and then select the tank, what

05:36.880 --> 05:42.970
you want to do with your script where you've got the fuel in it, is drag from the hierarchy and drop

05:42.970 --> 05:45.730
it down here into the drive script.

05:45.730 --> 05:54.550
So now that you can reference it from inside of there, okay, back to our code, we're going to put

05:54.550 --> 06:00.010
some more code in here to determine when we've hit the spacebar.

06:00.070 --> 06:05.770
And when we've hit the spacebar, we're going to print out the distance between the tank and the fuel.

06:06.040 --> 06:08.020
So this will go if.

06:09.660 --> 06:20.490
Input, not get key down and inside of here we'll put key code dot space.

06:22.430 --> 06:28.010
Now, if that is the case, then we're going to calculate.

06:29.240 --> 06:31.490
Distance like that.

06:31.970 --> 06:35.000
Okay, now calculate distance is a method we have to write.

06:35.000 --> 06:44.960
So let's come up here above our late update and we'll put void, calculate distance and inside of here

06:45.980 --> 06:51.470
we're going to calculate the distance and then do a debug log to print it out.

06:51.470 --> 06:58.220
So let's create, first of all, a float for distance.

06:59.420 --> 07:04.490
And then we'll go debug dot log and put distance.

07:07.590 --> 07:09.150
Or something like that.

07:09.660 --> 07:13.500
So you can see that print out in the console.

07:13.540 --> 07:13.830
Okay.

07:13.830 --> 07:16.530
With that done, let's just save it and we will test it.

07:16.560 --> 07:20.940
Of course, it's only going to print out zero, but at least we will get something printing out just

07:20.940 --> 07:25.350
to make sure that we haven't got any compilation bugs so far.

07:25.380 --> 07:26.970
Okay, so press play.

07:27.540 --> 07:32.670
Now I don't need to really move my tank, but I'm just going to hit the spacebar in the console.

07:32.670 --> 07:35.370
You should get distance zero going on in there.

07:35.700 --> 07:38.430
So that's ready to go.

07:38.670 --> 07:47.520
We're going to go back into that drive script and in calculate distance, use Pythagoras theorem to

07:47.520 --> 07:53.400
calculate the distance between the tank and the fuel.

07:53.400 --> 08:01.260
So this is the challenge for you using the formula for Pythagoras Theorem, which is on the screen now,

08:01.260 --> 08:07.770
I want you to write the code to calculate the actual distance so that we can then print it out.

08:07.770 --> 08:08.730
So pause the video.

08:08.730 --> 08:10.170
Now go and do that.

08:10.170 --> 08:13.170
And when we come back, I'll go through the answer with you.

08:17.040 --> 08:17.710
Welcome back.

08:17.730 --> 08:20.280
How did you go with that calculation?

08:20.280 --> 08:20.650
Right.

08:20.670 --> 08:29.820
Well, distance in this case is going to equal the math ef dot square root.

08:30.840 --> 08:34.770
So let's find square root of math.

08:34.770 --> 08:40.320
F dot pow of fuel.

08:40.860 --> 08:44.280
Dot transformed position.

08:45.000 --> 08:50.370
Minus transform position.

08:52.780 --> 08:54.190
Here's the power of two.

08:56.030 --> 09:00.050
Plus it's go down on the next line for that.

09:01.970 --> 09:03.350
-- effect.

09:04.200 --> 09:05.130
Pal.

09:06.120 --> 09:15.690
Few transformed opposition, minus transformed opposition to the power of two.

09:15.740 --> 09:20.790
Okay, now, I've just realized that the first one here is going to be the difference between our x

09:20.790 --> 09:21.780
values.

09:22.320 --> 09:27.860
So X from both of those, and then the other one is the Z value.

09:27.870 --> 09:30.720
Now, why are we using the Z value?

09:30.750 --> 09:40.590
Because in this case, if we have a look on our screen here, we have the Z and the X values.

09:40.590 --> 09:46.140
As far as the Y value goes, we're not actually moving up and down, so we're not really interested

09:46.140 --> 09:52.860
in any distance in the Y direction, though if you wanted to put that in, then you're just going to

09:52.860 --> 09:59.400
include another plus and then the same calculations for the Y to the power of two.

09:59.640 --> 09:59.970
All right.

09:59.970 --> 10:04.320
So this is going to give you a new value of distance.

10:04.320 --> 10:06.240
So just save that.

10:06.690 --> 10:13.650
Let's go back into here and we press play so that we can test it out.

10:14.130 --> 10:21.240
So looking in your console as you're moving around with the tank, so hit the spacebar and you'll see

10:21.240 --> 10:21.780
a distance.

10:21.780 --> 10:28.260
Now, this in theory is going to get shorter the closer you get to the fuel, which it is.

10:28.560 --> 10:31.500
So that's a good way of actually visually checking.

10:31.500 --> 10:35.190
Your calculations are kind of doing the right thing.

10:35.190 --> 10:40.410
It's always good to be able to make that sort of check on these calculations.

10:40.860 --> 10:41.850
Okay, great.

10:41.850 --> 10:43.770
So that is now working.

10:43.770 --> 10:48.660
So that's how to include Pythagoras theorem in this.

10:48.660 --> 10:53.670
When we come back in the next lecture, we're going to continue looking at calculations of distance

10:53.670 --> 11:00.630
and how else you can actually do it and the unity values that you can use as well.
