WEBVTT

00:06.430 --> 00:12.370
In this video, we're going to improve on the driving mechanics that we've got on this car.

00:12.550 --> 00:19.000
So we need it to break and to accelerate in a much better way.

00:19.030 --> 00:24.490
Now, if we think about the car when it needs to brake and accelerate, well, when it's going slow,

00:24.490 --> 00:27.820
it needs to obviously accelerate up to a certain speed.

00:27.850 --> 00:30.640
Now, let me just grab this car for you.

00:30.640 --> 00:37.660
And what I'm going to do is just bring it up to the end of the track here so I can just illustrate something

00:37.660 --> 00:39.550
and then zoom in on it.

00:39.640 --> 00:47.080
So at this point, this car's been speeding along towards the end and you're probably thinking.

00:47.890 --> 00:48.780
Okay.

00:48.790 --> 00:55.300
If it was you driving this car, you would see that this is coming up and therefore you need to slow

00:55.300 --> 00:55.600
down.

00:55.600 --> 00:57.090
Therefore, you would need to break.

00:57.100 --> 01:03.970
Now, the car currently is we're saying we want to break into a waypoint and then accelerate out of

01:03.970 --> 01:04.480
a waypoint.

01:04.480 --> 01:10.300
Currently we're breaking into the waypoint and then we keep going and then we break into the waypoint

01:10.300 --> 01:18.910
and we keep going and steering towards them, but we're not considering how much further ahead the curve

01:18.910 --> 01:19.930
actually goes around.

01:19.930 --> 01:25.150
We're only looking like to one waypoint ahead rather than a little bit further.

01:25.450 --> 01:32.140
Now the other thing we should also consider is the angle between the waypoint.

01:32.140 --> 01:38.020
So if this waypoint over here was actually sitting here and this car was coming up to this waypoint,

01:38.020 --> 01:45.310
then needed to go to this waypoint, then the car doesn't actually need to break because it's just going

01:45.310 --> 01:46.630
in a straight line.

01:46.750 --> 01:52.270
So therefore that doesn't make any sense based on the distance to a waypoint.

01:52.600 --> 01:59.860
Instead, we want to look at the angle that the car is about to turn in the future, because when you

01:59.860 --> 02:04.450
turn, you actually want to slow down into that turn.

02:04.690 --> 02:11.290
Therefore, we're going to, instead of looking the distance from waypoints, look at the angle of our

02:11.290 --> 02:13.750
car to our waypoint.

02:13.900 --> 02:14.530
Right?

02:14.530 --> 02:19.030
Let's go into our AI controller and have a look at implementing this.

02:19.480 --> 02:23.350
So we're going to do it for the acceleration and also for the braking.

02:23.350 --> 02:25.000
We're going to leave the steering alone.

02:25.000 --> 02:31.900
So I'm just going to grab this and I'll put it down here so that we can keep them separate.

02:31.900 --> 02:40.180
Now, the first thing we need to do is actually calculate some kind of value to do with the angle that

02:40.180 --> 02:40.750
we're turning.

02:40.750 --> 02:46.810
Now, we already know the target angle that we're turning because we're calculating it up here, but

02:46.810 --> 02:55.330
we need to normalize that in some kind of value between zero and one where zero would be hardly any

02:55.330 --> 03:01.030
angle needs to be turned and one means like 90 degrees needs to be turned, and then we can use that

03:01.030 --> 03:08.080
range or percentage as such as a factor in working out our acceleration and braking.

03:08.140 --> 03:08.530
Okay.

03:08.530 --> 03:16.570
So we're going to calculate something called, first of all, the corner, so float corner and that's

03:16.570 --> 03:19.600
going to equal math f clamp.

03:21.990 --> 03:30.570
And we're going to clamp the math f absolute value of the target angle because remember, it can be

03:30.570 --> 03:38.670
a minus angle, of course, and we're going to clamp it between zero and 90, where 90 is the maximum

03:38.670 --> 03:40.350
angle that we will consider.

03:40.350 --> 03:47.010
Because if it's anything over 90 degrees, then we're still on a break pretty hard, almost fully break

03:47.010 --> 03:48.390
coming into that.

03:48.660 --> 03:51.390
Okay, now from there, we're going to normalize it.

03:51.390 --> 03:55.020
So we'll go float corner factor.

03:55.500 --> 04:00.210
And this will give us sort of a percentage of how bad the corner is or how sharp the corner is.

04:00.210 --> 04:04.050
So we'll go corner divided by 90.0 F.

04:04.050 --> 04:09.180
Now, if the corner is 90 degrees, so very sharp, we'll get a one.

04:09.180 --> 04:13.020
If the corner is zero degrees, then we will get a zero.

04:13.020 --> 04:18.480
So the corner factor, the higher it is, the sharper the corner is going to be.

04:18.570 --> 04:23.220
We can now use this to influence our acceleration value.

04:23.220 --> 04:35.040
So up here we could put that the acceleration equals math F loop between zero and one for our corner

04:35.040 --> 04:35.750
factor.

04:35.760 --> 04:42.930
Now we don't want to have the corner factor as such when it is one.

04:43.230 --> 04:50.490
So when the corner is really, really steep, okay, so 90 degrees then we don't want to go full pelt,

04:50.490 --> 04:52.050
which is the one here.

04:52.050 --> 04:55.680
We actually want to be going at zero acceleration.

04:56.010 --> 05:02.160
That means we can sort of switch this around, this kind of factor with a one minus corner factor.

05:03.690 --> 05:06.300
Okay, so that will give us our acceleration.

05:06.300 --> 05:15.330
Now, you might not even want to have any corner acceleration whatsoever depending on other factors.

05:15.330 --> 05:17.820
So what I'm going to put in here is if.

05:19.580 --> 05:22.520
Corner is.

05:25.040 --> 05:32.150
Greater than 20 and the rigid body velocity

05:34.280 --> 05:38.430
magnitude is greater than, let's say, ten.

05:38.450 --> 05:40.880
Then we will set our acceleration.

05:40.880 --> 05:44.020
Otherwise we're just going to use the normal acceleration.

05:44.030 --> 05:48.320
Now, again, we've got these arbitrary values that we're putting in here.

05:48.320 --> 05:55.460
And yes, it's a matter of playing around with them to get the results that you want.

05:55.460 --> 06:01.220
And if there's something else that you want to consider inside of this, if statement, then put it

06:01.220 --> 06:01.610
in there.

06:01.610 --> 06:05.750
Okay, this is not any hard rule, sort of if statement.

06:05.750 --> 06:11.150
It's just totally going with the logic of the situation and it might be something else you need to put

06:11.150 --> 06:14.150
in there that will make this even better.

06:14.750 --> 06:21.170
So that is the logic for doing a math loop for our acceleration.

06:21.170 --> 06:25.880
Now we pretty much want to do the same thing for our braking.

06:25.880 --> 06:37.750
Now, in the case of the braking I'm starting with, if corner is greater than ten and rigid body dot

06:38.000 --> 06:43.870
velocity dot magnitude is greater than ten.

06:43.880 --> 06:45.920
Of course we're going to break.

06:45.920 --> 06:51.320
We don't want to brake unless we're going at a certain speed because there's not really any need to

06:51.320 --> 06:51.890
brake.

06:51.890 --> 06:56.840
So if this is the case, B is going to equal what?

06:56.870 --> 06:59.720
So here's a challenge for you.

06:59.750 --> 07:03.860
Can you finish off this loop statement for B?

07:03.860 --> 07:07.640
And it's kind of like the opposite of what we've done for a pause video.

07:07.640 --> 07:08.450
Now give it a go.

07:08.450 --> 07:11.540
And when we come back, I'll go through that with you.

07:13.430 --> 07:14.090
All right.

07:14.120 --> 07:15.110
How did you go?

07:15.140 --> 07:20.060
Well, it's going to be math after loop and it'll be 0 to 1.

07:20.630 --> 07:23.660
In this case, we just want it to be corner factor.

07:23.690 --> 07:26.570
The sharper the corner, the closer to one.

07:26.570 --> 07:34.070
Our breaking is going to be remembering again that our steering, our acceleration and our braking factors

07:34.070 --> 07:41.210
here that we're calculating to pass through to our go down here are all values between zero and one,

07:41.210 --> 07:47.660
which get multiplied by those max torque, max, brake and max steer values that we were putting together

07:47.660 --> 07:48.410
earlier.

07:49.100 --> 07:49.790
Okay.

07:49.790 --> 07:55.760
So with that done, let's save this and have a look at the result.

07:55.810 --> 07:56.030
Okay.

07:56.030 --> 08:01.910
So first of all, I don't want my car there because it'll turn around and try go back to that first

08:01.910 --> 08:04.100
waypoint, back down the track.

08:04.100 --> 08:11.450
So let me just zoom out of here and grab my car and bring it back.

08:11.480 --> 08:11.740
Okay.

08:11.840 --> 08:20.330
So that hit the key and bring the car back to here machine just a little bit further back.

08:20.450 --> 08:20.870
Okay.

08:20.870 --> 08:27.820
So let's now press play and have a look at the behavior of this car while it's driving up the straight.

08:27.830 --> 08:34.490
I'm going to zoom into this corner because the corner is actually where the important part of our driving

08:34.490 --> 08:36.500
is taking place, essentially.

08:36.740 --> 08:36.940
Okay.

08:37.010 --> 08:39.350
So it's coming up to the corner.

08:39.620 --> 08:41.420
Now, it still has to go straight.

08:41.420 --> 08:43.100
Has to go straight, has to go straight.

08:43.400 --> 08:53.060
It doesn't remember flip to the next waypoint or think that the next waypoint is its location until

08:53.060 --> 08:57.110
it's hit this location here, the other waypoint.

08:57.110 --> 08:59.420
So it's going to think I'm still going straight.

08:59.420 --> 09:00.680
I'm still going straight and still going straight.

09:00.680 --> 09:05.780
And then bang, as soon as it's got within the range of here that considers its reach, that waypoint,

09:05.780 --> 09:10.850
it's then suddenly going, Oh my gosh, I've got to go over here and brake quickly, quickly, bang

09:10.850 --> 09:11.960
into there.

09:12.140 --> 09:17.630
So it now has to consider I should probably be braking further back.

09:17.630 --> 09:25.100
And at this point you can see that we're currently full on brake and we're not going anywhere at all

09:25.520 --> 09:27.200
because we've slowed down.

09:27.590 --> 09:34.610
And so the angle that we've turned on the brake is most likely the angle, some kind of angle like to

09:34.610 --> 09:38.720
where it's considering the next waypoint is over there.

09:39.020 --> 09:44.780
So what we need to do is to implement a way of looking ahead.

09:44.960 --> 09:50.960
And we've already done this when we looked at waypoints and the little tanks to make sure that we could

09:50.960 --> 09:52.550
accurately hit the waypoints.

09:52.550 --> 09:54.710
So do you remember what that technique was?

09:55.130 --> 10:01.400
Well, it was to put in a progress tracker and then have the progress tracker go around the circuit

10:01.400 --> 10:07.670
and hit everything almost perfectly and then have your vehicle follow that progress tracker.

10:07.670 --> 10:11.150
So we'll come back in the next lecture and put that together.
