A commonly used calculation in AI is working out the distance between two game objects.
It's not only important in determining where things are in the environment with respect to one another,
but also for calculating angles and normalizing vectors.
You'll see the distance calculation used over and over again in this course, especially when trying
to determine if an enemy is in range or where other resources are.
To calculate a distance, you're going to need two points.
These can be two separate locations or either end of a vector.
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
cell that is at 12 on the X and ten on the Y.
So how do we find the distance between these?
Well, any two points in an environment, whether it be 2D or 3D, can be used to form a right angle
triangle, as you can see here.
Once you have a right angle triangle or that you know that it's possible to form a right angle triangle,
which it always is in this case, then you can apply Pythagoras theorem.
Now, do you remember what Pythagoras Theorem was from your high school mathematics?
Well, recall for a moment, if you will, the longest side of a right angle triangle is called the
hypotenuse and the other sides.
If we label them A and B and it doesn't matter which order you do that in, then Pythagoras Theorem
says that the length of the hypotenuse equals the square root of A squared plus B squared.
So that's the lengths of A and B squared.
And how do we find these lengths?
Well, if you have a look at a, it happens to be the difference in the X direction of where the tank
is and where the fuel cell is.
And B is the difference between where the tank is and the fuel cell is in the Y direction.
So what does this look like when we work it out?
Well, first of all, we need to figure out what A is and A is going to be the difference between the
X coordinates of the tank and the fuel.
So X one minus x two and B is going to be the difference between the Y coordinates of the tank and the
fuel.
Now, we're going to do a squared operation on these so it doesn't matter what order you do them in.
As long as if the tank is item one, then the fuel always remains item two so that the x and y coordinates
have to be in the same order inside those equations.
So then when we plug in the actual values, we get three -12 squared plus two minus ten squared, which
gives us down the track 81 plus 64.
The square root of that is 12.
So that means that the length of the hypotenuse or the distance between the tank and the fuel in this
case is 12.04.
Now, it doesn't matter where in the environment your points are, you can always make a right angled
triangle.
But the important thing here is that that right angled triangle is going to have one side that is parallel
to the x axis and one side is parallel to the y axis.
And the length of those two sides are always going to give you that difference that you need to put
in to Pythagoras Theorem.
We're going to investigate distance calculations in 2D.
So to do this, I want you to open up a new unity project and then bring in the start of file.
Attash This lecture.
If you aren't seeing the view that I've got here, you can click on this 2D button at the top and it
should align your scene for you.
Now over in the game view.
I got this by setting my aspect ratio to 16 to 9, which will give you this.
Now at this point, if we press play, you'll see that you can use the arrow keys to drive the tank
around.
Now, this drive script that's on here is exactly the same drive script we've used in the previous lecture
for our little farmer villager.
And what we're going to do is actually calculate the distance between the tank and the fuel that's up
here.
Okay, so let's open up the drive script.
Okay.
So you'll find that we have our updates occurring in update.
And as we were talking about before, we could probably put these into late update though it's not going
to make a big difference in this particular scenario.
First of all, this script is attached to the tank, so the transform that's referred to inside of this
script belongs to the tank.
Therefore the transform position is the position of the tank.
Now we're going to add in the fuel, so we're going to bring that in at the top as a game object.
So public game object, fuel like that.
Okay.
So if we just hit save on that, switch back into unity for a moment and then select the tank, what
you want to do with your script where you've got the fuel in it, is drag from the hierarchy and drop
it down here into the drive script.
So now that you can reference it from inside of there, okay, back to our code, we're going to put
some more code in here to determine when we've hit the spacebar.
And when we've hit the spacebar, we're going to print out the distance between the tank and the fuel.
So this will go if.
Input, not get key down and inside of here we'll put key code dot space.
Now, if that is the case, then we're going to calculate.
Distance like that.
Okay, now calculate distance is a method we have to write.
So let's come up here above our late update and we'll put void, calculate distance and inside of here
we're going to calculate the distance and then do a debug log to print it out.
So let's create, first of all, a float for distance.
And then we'll go debug dot log and put distance.
Or something like that.
So you can see that print out in the console.
Okay.
With that done, let's just save it and we will test it.
Of course, it's only going to print out zero, but at least we will get something printing out just
to make sure that we haven't got any compilation bugs so far.
Okay, so press play.
Now I don't need to really move my tank, but I'm just going to hit the spacebar in the console.
You should get distance zero going on in there.
So that's ready to go.
We're going to go back into that drive script and in calculate distance, use Pythagoras theorem to
calculate the distance between the tank and the fuel.
So this is the challenge for you using the formula for Pythagoras Theorem, which is on the screen now,
I want you to write the code to calculate the actual distance so that we can then print it out.
So pause the video.
Now go and do that.
And when we come back, I'll go through the answer with you.
Welcome back.
How did you go with that calculation?
Right.
Well, distance in this case is going to equal the math ef dot square root.
So let's find square root of math.
F dot pow of fuel.
Dot transformed position.
Minus transform position.
Here's the power of two.
Plus it's go down on the next line for that.
-- effect.
Pal.
Few transformed opposition, minus transformed opposition to the power of two.
Okay, now, I've just realized that the first one here is going to be the difference between our x
values.
So X from both of those, and then the other one is the Z value.
Now, why are we using the Z value?
Because in this case, if we have a look on our screen here, we have the Z and the X values.
As far as the Y value goes, we're not actually moving up and down, so we're not really interested
in any distance in the Y direction, though if you wanted to put that in, then you're just going to
include another plus and then the same calculations for the Y to the power of two.
All right.
So this is going to give you a new value of distance.
So just save that.
Let's go back into here and we press play so that we can test it out.
So looking in your console as you're moving around with the tank, so hit the spacebar and you'll see
a distance.
Now, this in theory is going to get shorter the closer you get to the fuel, which it is.
So that's a good way of actually visually checking.
Your calculations are kind of doing the right thing.
It's always good to be able to make that sort of check on these calculations.
Okay, great.
So that is now working.
So that's how to include Pythagoras theorem in this.
When we come back in the next lecture, we're going to continue looking at calculations of distance
and how else you can actually do it and the unity values that you can use as well.