Simulate traffic to the point that it should be realistic enough for the real world.
To make it easier for now, just using a square-based city (like New York) should be enough.
Creating a correct Behaviour Tree.
Making the cars aware of each other.
Making sure there are different speeds possible.
Add traffic lights to make the scene look alive.
Do all of this in a framework provided by the teachers.
A lot of games in this day and age have big open worlds, mostly cities.
To make these cities look alive, there are cars driving around in non-ending cycles.
This is a background element that is very important, because you do want the streets to be alive.
Because of this, I wanted to recreate something alike.
Just something really basic, but something that works without a problem.
I accomplished this process in 4 steps:
Step 1 - Creating the grid.
I took a grid from a starting project from our teachers, and modified the pathfinding so that we can have different "speeds" for certain roads.
We get a start and end point and we use AStar for finding the path.
Because this is a directional graph, I took away the connections for some parts and put buildings there.
Step 2 - Adding a car.
For this I just needed to make a Car class, but also give this car a Behavior Tree.
After this, I just spawned a car on a random place in the grid, made that the start point, made it look for a random end point and made the car move towards it.
When the car reaches the end, it would find a new end point and this is the general cycle.
Step 3 - Adding multiple cars + making them aware of each other.
Spawning the multiple cars was not a big deal. Just keep going for random start points that aren't assigned to any other car, and spawn them there.
But making them aware is a bit different.
This handles the "right of way". To accomplish this, I gave every car a cone of vision.
If another car enters the cone of vision, located on the right side, going to the left side and is moving, we halt our own movement.
Yes we check if they are moving, this is to avoid stopping for cars that are waiting for a red light.
Step 4 - Adding Traffic Lights.
Luckily was this part not so hard.
We basically visualize this on the grid, yet behind the scenes the grid value changes.
Since the cars use the value of the connection for the speed, changing this to 0 (and changing it back for path calculations, but then turning it back to 0), will work splendid.
And that's how we get a nice looking simple Traffic Simulation.
Of course, I didn't explain everything in here, for further information with more detail and images you can go to my GitHub.
There, you'll also be able to find the code for it.
This was a really fun short project to work on!
I learned how useful Behavior Trees can be, yet on the same hand how difficult they can be to either debug or at times implement.
This project was created for my "Research Project" for the course "Gameplay Programming".
I do noticed that it really isn't a research, but more like a casual project.
The teachers also told me that during the presentation of the exam for it.
But at the same time they did also mention that I nicely reflects my knowledge of Behavoir Trees and Directional Graphs.
So In the end, I'm really happy with the result and cannot complain at all about it because I genuinly had a lot of fun making it.
During the progress a lot of unwanted bugs kept popping up, so that made this so enticing.
Down below you will be able to download an exe and run it yourself to see how to cars react!
To move around in the world, use the right mouse button, zoom out/in using the scroll wheel.