WEBVTT

00:04.360 --> 00:08.230
The cross product of two vectors results in another vector.

00:08.290 --> 00:13.850
It is performed on vectors in three dimensions, but still has relevancy to the issue.

00:13.870 --> 00:19.840
We are trying to solve, which is to determine which way to turn to face one object towards another.

00:20.050 --> 00:25.540
As you saw previously, although we are working with a two dimensional environment, it does indeed

00:25.540 --> 00:28.840
have a third dimension that's coming out of the screen.

00:29.320 --> 00:35.560
Given the two vectors V and W, the cross product is defined like this.

00:36.010 --> 00:36.700
Whoa.

00:36.700 --> 00:38.290
That's a little scary, right?

00:38.290 --> 00:45.520
But if you examine it closely, you'll see why it is called a cross product as it multiplies each component

00:45.520 --> 00:48.070
of the vectors with every other component.

00:48.220 --> 00:50.410
Let's look at it like this.

00:50.860 --> 00:58.210
The first part is v dot y multiplied by w dot z.

00:58.840 --> 01:05.080
This is followed by the reverse values of v z multiplied by w y.

01:05.440 --> 01:13.870
Then we cross v z with w x and then v dot x with w z.

01:14.230 --> 01:19.870
Then finally v x with w, y and v.

01:20.110 --> 01:20.740
Y.

01:20.740 --> 01:22.840
With w x.

01:24.410 --> 01:28.790
The resulting victor is one that is perpendicular to the other two.

01:29.090 --> 01:34.010
Now, the interesting thing about the cross product is that it's not reversible.

01:34.160 --> 01:38.720
V Cross W does not equal W cross.

01:38.720 --> 01:45.320
V In fact, if you perform the operation the other way around, you will still get evicted.

01:45.320 --> 01:53.180
That is perpendicular to both V and W, but it will be facing in the other direction and we can exploit

01:53.180 --> 01:56.460
this fact in determining which way our tank should turn.

01:56.480 --> 02:00.020
Let's take a look at an example as to how this will work.

02:00.050 --> 02:06.140
Here is the positioning of the tank and fuel, similar to the example we are working with in Unity and

02:06.140 --> 02:11.670
these other vectors for the forward direction and the direction towards the tank.

02:11.690 --> 02:15.410
The cross product between these yields zero.

02:16.190 --> 02:17.120
Zero.

02:17.180 --> 02:19.190
-0.7.

02:19.280 --> 02:23.510
Now, let's look at the situation when the fuel is on the other side.

02:23.870 --> 02:35.720
These are the relevant vectors and this is the cross product 000.7 in determining whether we do an anticlockwise

02:35.720 --> 02:38.120
or a clockwise turn.

02:38.180 --> 02:42.270
The clue is in the results of the cross products.

02:42.290 --> 02:45.470
Take a close look at both of those cross products.

02:45.470 --> 02:48.530
What is the only difference?

02:56.000 --> 02:57.440
Did you work it out?

02:57.500 --> 03:03.760
It's the Z coordinate in the case where we want to turn clockwise.

03:03.770 --> 03:07.400
The Z coordinate is a negative value.

03:07.430 --> 03:13.610
In the case where we want to turn anticlockwise, the Z value is a positive value.

03:13.640 --> 03:21.830
Therefore, from this we can deduce that if the cross product's Z value is less than zero or negative,

03:21.830 --> 03:25.010
then we're going to do a clockwise turn.

03:25.280 --> 03:32.330
Otherwise, if it's a positive value greater than zero, then we're going to do an anticlockwise turn.

03:33.080 --> 03:37.340
It's time now to have a look at how this works in our environment with our tank.

03:37.340 --> 03:43.220
Now, you might not have taken a lot of notice in the angles that were being calculated before.

03:43.250 --> 03:50.360
If we kind of face the fuel here and then hit the spacebar, you'll notice I've also made my green vector

03:50.360 --> 03:54.650
a bit longer by actually multiplying it by ten in the code.

03:54.650 --> 03:57.710
So we're facing the fuel here.

03:57.710 --> 03:59.600
It's about an angle of two.

03:59.600 --> 04:06.050
Now if I turn and say, Look up over here and press it, now I get an angle of 23.

04:06.230 --> 04:07.220
Well, that's great.

04:07.220 --> 04:13.250
Now if I turn my tank down here, so I'm trying to get like another angle and hit it again.

04:13.250 --> 04:17.990
So this is, you know, I'm getting an angle of 24, but let's say it was 23.

04:18.410 --> 04:24.800
How do I know or how does this tank, when it becomes an I character, know which way to turn?

04:24.800 --> 04:29.180
Does it turn this way or does it turn this way in order to face the tank?

04:29.180 --> 04:37.070
Because whenever we do the calculation of the angle using the DOT product and our cosine, we always

04:37.070 --> 04:38.000
get a positive angle.

04:38.000 --> 04:40.400
We will not get a negative angle.

04:40.430 --> 04:47.750
Now, yes, unity does provide a signed angle method which will work out whether it's negative or not.

04:47.750 --> 04:49.550
But what actually goes into that?

04:49.550 --> 04:55.940
We're going to use the cross product, as we've just seen in the theory, to work out whether we have

04:55.940 --> 05:00.200
a clockwise or an anticlockwise turn.

05:00.530 --> 05:00.980
Okay.

05:01.010 --> 05:08.360
So let's go back into our code and we're going to add in another method called Cross.

05:08.360 --> 05:10.910
So this is my calculate angle.

05:10.910 --> 05:15.710
You'll see I've multiplied the tank forward by ten to get that green line longer if you want to do that

05:15.710 --> 05:15.950
too.

05:15.950 --> 05:20.030
And I've made it 5 seconds so it stays on the screen for a little bit longer.

05:20.360 --> 05:24.170
So under here, let's go void cross.

05:24.200 --> 05:26.180
We're going to pass through two vectors.

05:26.180 --> 05:32.780
So vector three is V and vector three.

05:32.780 --> 05:34.610
The second one is a W.

05:34.610 --> 05:41.990
Now inside of here, we're going to work out what the cross product is using the formula for working

05:41.990 --> 05:49.220
out the cross product, which is a whole bunch of multiplications and minuses between the X, Y and

05:49.220 --> 05:50.120
Z values.

05:50.120 --> 05:51.860
So this is your challenge.

05:51.860 --> 05:53.150
Here is the formula.

05:53.150 --> 05:56.870
Again, what I'd like you to do is complete this cross.

05:56.870 --> 06:05.090
So it actually performs a cross product calculation on here between V and W, and what I want it to

06:05.090 --> 06:09.740
do is not return a void, but to return a vector three.

06:09.740 --> 06:18.470
So that vector three is going to give you that perpendicular vector that's perpendicular to V and W

06:18.470 --> 06:19.640
when they are cross.

06:19.640 --> 06:20.720
So pause video.

06:20.720 --> 06:22.460
Now go and give that a go.

06:22.460 --> 06:25.640
And when we come back, I'll go through the solution with you.

06:26.390 --> 06:27.170
Welcome back.

06:27.170 --> 06:29.450
How did you go working that out?

06:29.450 --> 06:29.680
Okay.

06:29.720 --> 06:35.690
There's a long lot of mathematics going on in here, so let's do this bit by bit.

06:35.690 --> 06:46.310
So we'll create a float for our X moult and we'll create one for our float for the Y moult and we'll

06:46.310 --> 06:49.850
create one for our float for the Z moult.

06:50.060 --> 06:50.340
Okay.

06:50.450 --> 06:57.410
So the X moult is going to equal now the way I remember this is if it's calculating the X value, then

06:57.410 --> 07:01.610
it's using the Y and the Z values in the calculation.

07:01.610 --> 07:05.240
If it's the Y moult, it's using the X and the Z in the calculation.

07:05.390 --> 07:09.500
So it doesn't use itself in the calculation, if that makes sense.

07:09.500 --> 07:23.600
So this will be the Y multiplied by Z minus V, dot z multiplied by w dot y.

07:24.800 --> 07:28.970
So the next one is going to be equal to everything but y.

07:28.970 --> 07:42.500
So v dot x multiplied by w dot z minus v dot z multiplied by w dot x getting the hang of it.

07:42.500 --> 07:55.400
And finally we've got the x multiplied by y minus the dot y multiplied by w x.

07:55.710 --> 07:56.220
Okay.

07:56.220 --> 07:58.920
So then we just have to put them together.

07:58.950 --> 08:05.970
So we return x molt inside of of course, a new vector three, vector three.

08:07.390 --> 08:14.650
X, molt, y molt and z molt.

08:16.130 --> 08:16.490
Okay.

08:16.520 --> 08:18.290
How did you go working that out?

08:18.680 --> 08:23.750
Looks like I'm missing a bracket, so it's just a matter of doing them step by step with any of these

08:23.750 --> 08:25.160
really complex equations.

08:25.160 --> 08:29.030
Break them right down and just do one little bit at a time.

08:29.840 --> 08:33.110
So let's put this to work up here.

08:33.110 --> 08:39.170
So it's the Z value that we're getting back from the cross that is the important part of working out,

08:39.170 --> 08:41.600
whether it's clockwise or anticlockwise.

08:41.600 --> 08:47.600
So we'll create an int for clockwise and we'll set that to one.

08:48.290 --> 08:58.520
And then we're going to say just in here, if cross of our tank forward and our tank direction or our

08:58.520 --> 09:01.070
fuel direction, I should say fuel direction.

09:01.070 --> 09:15.230
So if the cross of those two dot Z is less than zero, then clock wise is going to equal minus one.

09:15.230 --> 09:20.180
And then we'll use this as a multiplier for our turning angle.

09:20.330 --> 09:28.550
So in order to turn our tank just automatically inside of this method, let's put this dot transform,

09:29.210 --> 09:31.100
dot rotate.

09:31.190 --> 09:37.700
We'll rotate around the Z axis, which is the axis coming out of the screen for this 2D stuff.

09:37.700 --> 09:48.530
So zero zero and we're going to rotate by angle, which we've already calculated multiplied by math

09:48.530 --> 09:54.590
f dot rad two degrees because we've got our angles coming back in radians.

09:54.590 --> 10:03.710
But this rotate uses a degrees value and we'll multiply that by clockwise so we can turn one way or

10:03.740 --> 10:04.790
the other.

10:04.820 --> 10:07.310
Okay, let's try this out so we'll save it.

10:07.520 --> 10:09.320
Go back into unity.

10:09.410 --> 10:16.610
Now, when we hit the spacebar, our tank is going to automatically turn to look at the fuel for us.

10:16.610 --> 10:22.460
And this is like the very basics of creating that look at method that we used previously.

10:22.940 --> 10:25.460
So let's press play.

10:25.490 --> 10:30.380
Okay, so if I hit the spacebar, my tank snaps around to face the fuel.

10:30.380 --> 10:37.850
If I drive over here and hit the spacebar, I snap around and face the fuel like that.

10:38.300 --> 10:39.950
Okay, fantastic.

10:39.950 --> 10:41.210
It works really well.

10:41.210 --> 10:47.270
So although Unity has these methods that do this automatically for you, if you ever find yourself in

10:47.270 --> 10:51.890
a case where you don't have those methods available to you or something isn't working exactly right,

10:51.890 --> 10:59.120
at least now you've got like the underlying fundamentals of these calculations and you now know what

10:59.120 --> 11:06.230
the dot product and cross product are and how they're very, very useful in games and I especially with

11:06.230 --> 11:07.340
navigating.
