Making Cars aware of each other
Up until now we have spawned cars the way we want to on our freeway. The problem is that none of the cars are aware of its neighbours, and so they drive through each other.
We need to make the cars aware of their neighbours, just like the fish in the second lab were aware of their neighbours. So, we will reuse the code doing that, changing it so that it is more appropriate to our case.
We will, just like for the fish, use a radius, to decide how far to look for neighbours. We set radius as a variable in our Cars class, corresponding to Boids in lab 2. We also only check for cars in the same lane (only using the x value).
We got stuck a while on the fact that our spawned cars were not children to the class called “School”, like for the school of fish in the second lab, until we found the two rows of code that fixed that. Before we fixed that, the cars couldn’t use the code we had written in the School class to use when updating, and instead they just stood still where they spawned.
With that fixed and working, we then needed to decide what to do when a car detects another car within that radius.
After some testing of different values and actions we found that we needed to have a maximum and a minimum radius.
The action that we decided made the most sense and looked the best was to check on every update of the simulation if the neighbour was in between minradius and maxradius, and if it was, to change the velocity of the car to the same as its neighbour. Then, if a car detects a neighbour inside its minradius, it will lower its velocity to be slower than the neighbour (so that they are not to close to each other). When the car has slowed down enough to find itself in between the minradius and maxradius instead, it will change its velocity to that of the neighbour.
The part with the minradius also solved a problem we had of cars spawning on top of each other and therefore forming a long box instead of separating since they matched their speeds to the car in front.
This is all illustrated in the following video:
The next step will be to make the deacceleration more natural, since now we just decide that the velocity changes instead of it changing gradually.
No comments:
Post a Comment