In this video, we're going to improve on the driving mechanics that we've got on this car.
So we need it to break and to accelerate in a much better way.
Now, if we think about the car when it needs to brake and accelerate, well, when it's going slow,
it needs to obviously accelerate up to a certain speed.
Now, let me just grab this car for you.
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
and then zoom in on it.
So at this point, this car's been speeding along towards the end and you're probably thinking.
Okay.
If it was you driving this car, you would see that this is coming up and therefore you need to slow
down.
Therefore, you would need to break.
Now, the car currently is we're saying we want to break into a waypoint and then accelerate out of
a waypoint.
Currently we're breaking into the waypoint and then we keep going and then we break into the waypoint
and we keep going and steering towards them, but we're not considering how much further ahead the curve
actually goes around.
We're only looking like to one waypoint ahead rather than a little bit further.
Now the other thing we should also consider is the angle between the waypoint.
So if this waypoint over here was actually sitting here and this car was coming up to this waypoint,
then needed to go to this waypoint, then the car doesn't actually need to break because it's just going
in a straight line.
So therefore that doesn't make any sense based on the distance to a waypoint.
Instead, we want to look at the angle that the car is about to turn in the future, because when you
turn, you actually want to slow down into that turn.
Therefore, we're going to, instead of looking the distance from waypoints, look at the angle of our
car to our waypoint.
Right?
Let's go into our AI controller and have a look at implementing this.
So we're going to do it for the acceleration and also for the braking.
We're going to leave the steering alone.
So I'm just going to grab this and I'll put it down here so that we can keep them separate.
Now, the first thing we need to do is actually calculate some kind of value to do with the angle that
we're turning.
Now, we already know the target angle that we're turning because we're calculating it up here, but
we need to normalize that in some kind of value between zero and one where zero would be hardly any
angle needs to be turned and one means like 90 degrees needs to be turned, and then we can use that
range or percentage as such as a factor in working out our acceleration and braking.
Okay.
So we're going to calculate something called, first of all, the corner, so float corner and that's
going to equal math f clamp.
And we're going to clamp the math f absolute value of the target angle because remember, it can be
a minus angle, of course, and we're going to clamp it between zero and 90, where 90 is the maximum
angle that we will consider.
Because if it's anything over 90 degrees, then we're still on a break pretty hard, almost fully break
coming into that.
Okay, now from there, we're going to normalize it.
So we'll go float corner factor.
And this will give us sort of a percentage of how bad the corner is or how sharp the corner is.
So we'll go corner divided by 90.0 F.
Now, if the corner is 90 degrees, so very sharp, we'll get a one.
If the corner is zero degrees, then we will get a zero.
So the corner factor, the higher it is, the sharper the corner is going to be.
We can now use this to influence our acceleration value.
So up here we could put that the acceleration equals math F loop between zero and one for our corner
factor.
Now we don't want to have the corner factor as such when it is one.
So when the corner is really, really steep, okay, so 90 degrees then we don't want to go full pelt,
which is the one here.
We actually want to be going at zero acceleration.
That means we can sort of switch this around, this kind of factor with a one minus corner factor.
Okay, so that will give us our acceleration.
Now, you might not even want to have any corner acceleration whatsoever depending on other factors.
So what I'm going to put in here is if.
Corner is.
Greater than 20 and the rigid body velocity
magnitude is greater than, let's say, ten.
Then we will set our acceleration.
Otherwise we're just going to use the normal acceleration.
Now, again, we've got these arbitrary values that we're putting in here.
And yes, it's a matter of playing around with them to get the results that you want.
And if there's something else that you want to consider inside of this, if statement, then put it
in there.
Okay, this is not any hard rule, sort of if statement.
It's just totally going with the logic of the situation and it might be something else you need to put
in there that will make this even better.
So that is the logic for doing a math loop for our acceleration.
Now we pretty much want to do the same thing for our braking.
Now, in the case of the braking I'm starting with, if corner is greater than ten and rigid body dot
velocity dot magnitude is greater than ten.
Of course we're going to break.
We don't want to brake unless we're going at a certain speed because there's not really any need to
brake.
So if this is the case, B is going to equal what?
So here's a challenge for you.
Can you finish off this loop statement for B?
And it's kind of like the opposite of what we've done for a pause video.
Now give it a go.
And when we come back, I'll go through that with you.
All right.
How did you go?
Well, it's going to be math after loop and it'll be 0 to 1.
In this case, we just want it to be corner factor.
The sharper the corner, the closer to one.
Our breaking is going to be remembering again that our steering, our acceleration and our braking factors
here that we're calculating to pass through to our go down here are all values between zero and one,
which get multiplied by those max torque, max, brake and max steer values that we were putting together
earlier.
Okay.
So with that done, let's save this and have a look at the result.
Okay.
So first of all, I don't want my car there because it'll turn around and try go back to that first
waypoint, back down the track.
So let me just zoom out of here and grab my car and bring it back.
Okay.
So that hit the key and bring the car back to here machine just a little bit further back.
Okay.
So let's now press play and have a look at the behavior of this car while it's driving up the straight.
I'm going to zoom into this corner because the corner is actually where the important part of our driving
is taking place, essentially.
Okay.
So it's coming up to the corner.
Now, it still has to go straight.
Has to go straight, has to go straight.
It doesn't remember flip to the next waypoint or think that the next waypoint is its location until
it's hit this location here, the other waypoint.
So it's going to think I'm still going straight.
I'm still going straight and still going straight.
And then bang, as soon as it's got within the range of here that considers its reach, that waypoint,
it's then suddenly going, Oh my gosh, I've got to go over here and brake quickly, quickly, bang
into there.
So it now has to consider I should probably be braking further back.
And at this point you can see that we're currently full on brake and we're not going anywhere at all
because we've slowed down.
And so the angle that we've turned on the brake is most likely the angle, some kind of angle like to
where it's considering the next waypoint is over there.
So what we need to do is to implement a way of looking ahead.
And we've already done this when we looked at waypoints and the little tanks to make sure that we could
accurately hit the waypoints.
So do you remember what that technique was?
Well, it was to put in a progress tracker and then have the progress tracker go around the circuit
and hit everything almost perfectly and then have your vehicle follow that progress tracker.
So we'll come back in the next lecture and put that together.