Decelerating and changing lanes
At this point we have cars driving in lanes and creating traffic jams, but they change velocity very abruptly instead of decelerating.
To do this we thought that we might need a deceleration formula, such as the ones found at http://byjus.com/deceleration-formula.
The most interesting version of the deceleration formula was the following one.
Where deceleration is -a, starting velocity is u, final velocity is v and s is the distance covered.
To use this formula, v would be the velocity if the slower neighbour detected within a radius, and u would be the velocity of the current car. s would be the value we would need to calculate and use. So we would need to set a to an arbitrary value.
The problem with calculating this is that we need to do it in every frame, but we don’t actually want s to change once we’ve calculated s for one car. So updating s every frame is not an option.
Instead, by getting help at a project session, we found that we could use interpolation to get a linear change to make the car decelerate little by little instead of abruptly changing speed. Unity has one such function called Lerp (https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html).
The second thing we implemented this time was changing lanes.
We started by implementing the conditions for which the cars would change lane. They were, when a car detects a car in front and changes its speed to that of the neighbouring car, it will change lane to the left. The actual changing of lane is done by changing the y position by an appropriate amount. We needed to add limitations on this, to make sure that the lane the furthest to the left is the last lane they can change to, so that they stay on the freeway.
When this was done the cars change lanes to the left when a car in front is too slow, but, they don’t check to see if the lane beside is actually free, and end up changing on top of each other.
To fix this we want to make the car aware of the cars in the neighbouring lanes as well as the cars in front, which it can do in the same way as it does when checking neighbours in front. The difference is that now it doesn’t only check on its x axis (which is in front). We needed to test a few times with different values, to make sure that the radius checked was big enough for the cars to stop changing into each other.
To make the simulation more realistic, we also want the car that changed lane to keep it’s old speed, so that it will pass the slower car, which we implemented.
Video of the lane changing:
The next step: use lerp for changing lane smoothly instead of teleporting (tip from project session)
+ Fixing bugs in lane changing, since it doesn’t always seem to work as smoothly as we would like it to.
Also, get a more interesting scene by adding texture and maybe converting to 3D (to be able to change camera angle and have a cooler view, also tip from project session).
When changing to 3D we need to change x and y in scripts because as it is, it looks like this:
Which is more like space ships than cars, but apart from tha, the lane changing and freeway driving works.
No comments:
Post a Comment