The cross product of two vectors results in another vector.
It is performed on vectors in three dimensions, but still has relevancy to the issue.
We are trying to solve, which is to determine which way to turn to face one object towards another.
As you saw previously, although we are working with a two dimensional environment, it does indeed
have a third dimension that's coming out of the screen.
Given the two vectors V and W, the cross product is defined like this.
Whoa.
That's a little scary, right?
But if you examine it closely, you'll see why it is called a cross product as it multiplies each component
of the vectors with every other component.
Let's look at it like this.
The first part is v dot y multiplied by w dot z.
This is followed by the reverse values of v z multiplied by w y.
Then we cross v z with w x and then v dot x with w z.
Then finally v x with w, y and v.
Y.
With w x.
The resulting victor is one that is perpendicular to the other two.
Now, the interesting thing about the cross product is that it's not reversible.
V Cross W does not equal W cross.
V In fact, if you perform the operation the other way around, you will still get evicted.
That is perpendicular to both V and W, but it will be facing in the other direction and we can exploit
this fact in determining which way our tank should turn.
Let's take a look at an example as to how this will work.
Here is the positioning of the tank and fuel, similar to the example we are working with in Unity and
these other vectors for the forward direction and the direction towards the tank.
The cross product between these yields zero.
Zero.
-0.7.
Now, let's look at the situation when the fuel is on the other side.
These are the relevant vectors and this is the cross product 000.7 in determining whether we do an anticlockwise
or a clockwise turn.
The clue is in the results of the cross products.
Take a close look at both of those cross products.
What is the only difference?
Did you work it out?
It's the Z coordinate in the case where we want to turn clockwise.
The Z coordinate is a negative value.
In the case where we want to turn anticlockwise, the Z value is a positive value.
Therefore, from this we can deduce that if the cross product's Z value is less than zero or negative,
then we're going to do a clockwise turn.
Otherwise, if it's a positive value greater than zero, then we're going to do an anticlockwise turn.
It's time now to have a look at how this works in our environment with our tank.
Now, you might not have taken a lot of notice in the angles that were being calculated before.
If we kind of face the fuel here and then hit the spacebar, you'll notice I've also made my green vector
a bit longer by actually multiplying it by ten in the code.
So we're facing the fuel here.
It's about an angle of two.
Now if I turn and say, Look up over here and press it, now I get an angle of 23.
Well, that's great.
Now if I turn my tank down here, so I'm trying to get like another angle and hit it again.
So this is, you know, I'm getting an angle of 24, but let's say it was 23.
How do I know or how does this tank, when it becomes an I character, know which way to turn?
Does it turn this way or does it turn this way in order to face the tank?
Because whenever we do the calculation of the angle using the DOT product and our cosine, we always
get a positive angle.
We will not get a negative angle.
Now, yes, unity does provide a signed angle method which will work out whether it's negative or not.
But what actually goes into that?
We're going to use the cross product, as we've just seen in the theory, to work out whether we have
a clockwise or an anticlockwise turn.
Okay.
So let's go back into our code and we're going to add in another method called Cross.
So this is my calculate angle.
You'll see I've multiplied the tank forward by ten to get that green line longer if you want to do that
too.
And I've made it 5 seconds so it stays on the screen for a little bit longer.
So under here, let's go void cross.
We're going to pass through two vectors.
So vector three is V and vector three.
The second one is a W.
Now inside of here, we're going to work out what the cross product is using the formula for working
out the cross product, which is a whole bunch of multiplications and minuses between the X, Y and
Z values.
So this is your challenge.
Here is the formula.
Again, what I'd like you to do is complete this cross.
So it actually performs a cross product calculation on here between V and W, and what I want it to
do is not return a void, but to return a vector three.
So that vector three is going to give you that perpendicular vector that's perpendicular to V and W
when they are cross.
So pause video.
Now go and give that a go.
And when we come back, I'll go through the solution with you.
Welcome back.
How did you go working that out?
Okay.
There's a long lot of mathematics going on in here, so let's do this bit by bit.
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
create one for our float for the Z moult.
Okay.
So the X moult is going to equal now the way I remember this is if it's calculating the X value, then
it's using the Y and the Z values in the calculation.
If it's the Y moult, it's using the X and the Z in the calculation.
So it doesn't use itself in the calculation, if that makes sense.
So this will be the Y multiplied by Z minus V, dot z multiplied by w dot y.
So the next one is going to be equal to everything but y.
So v dot x multiplied by w dot z minus v dot z multiplied by w dot x getting the hang of it.
And finally we've got the x multiplied by y minus the dot y multiplied by w x.
Okay.
So then we just have to put them together.
So we return x molt inside of of course, a new vector three, vector three.
X, molt, y molt and z molt.
Okay.
How did you go working that out?
Looks like I'm missing a bracket, so it's just a matter of doing them step by step with any of these
really complex equations.
Break them right down and just do one little bit at a time.
So let's put this to work up here.
So it's the Z value that we're getting back from the cross that is the important part of working out,
whether it's clockwise or anticlockwise.
So we'll create an int for clockwise and we'll set that to one.
And then we're going to say just in here, if cross of our tank forward and our tank direction or our
fuel direction, I should say fuel direction.
So if the cross of those two dot Z is less than zero, then clock wise is going to equal minus one.
And then we'll use this as a multiplier for our turning angle.
So in order to turn our tank just automatically inside of this method, let's put this dot transform,
dot rotate.
We'll rotate around the Z axis, which is the axis coming out of the screen for this 2D stuff.
So zero zero and we're going to rotate by angle, which we've already calculated multiplied by math
f dot rad two degrees because we've got our angles coming back in radians.
But this rotate uses a degrees value and we'll multiply that by clockwise so we can turn one way or
the other.
Okay, let's try this out so we'll save it.
Go back into unity.
Now, when we hit the spacebar, our tank is going to automatically turn to look at the fuel for us.
And this is like the very basics of creating that look at method that we used previously.
So let's press play.
Okay, so if I hit the spacebar, my tank snaps around to face the fuel.
If I drive over here and hit the spacebar, I snap around and face the fuel like that.
Okay, fantastic.
It works really well.
So although Unity has these methods that do this automatically for you, if you ever find yourself in
a case where you don't have those methods available to you or something isn't working exactly right,
at least now you've got like the underlying fundamentals of these calculations and you now know what
the dot product and cross product are and how they're very, very useful in games and I especially with
navigating.