by

Developer Diary #4: Traffic Anarchy

Jan
14
'
15

Today I decided to start making use of my lane overlap detection and made cars aware of overlaps.

Before tackling higher-level traffic rules like right-before-left, yielding or traffic lights, it was important to just get basic collision avoidance going (because you will need it if the higher level rules fail to do their job).

This was my first result:

(Sorry for the low-fps GIFs, they are the quickest to produce)

You can see cars on the intersection noticing each other (yellow dots) and using the overlap data to find the place where to stop in order to avoid crashing into other cars (red dots).

It's quite interesting to see that giving cars just this basic collision advoidance results in  barbaric but kinda fair "whoever comes first, goes first" traffic patterns.

There was still a bug in the calculation, causing some crashes to still occur (because one car wrongly assumed that the other one already left the overlap).

After fixing that, the behaviour became even more stable - cars could even somewhat reasonably handle this chaotic situation:

For low speeds it already worked really well, but for high speeds there was the problem that cars only started to care about overlaps once they entered the intersection, which was often too late.

So the last thing I worked on today was to make cars think about overlaps even before, when approaching an intersection. It is unfnished, because I have to sleep now, but this is its current state:

(Dark red dots show stopping location determined before even entering the intersection. They seem to be correct, but for some reason, cars don't appropriately slow down yet, only once they enter the intersection)

Two small asides:

a) for people who worry about performance of such detailed simulation, it is important to realize that only the very first cars in each lane have to do this slightly complicated obstacle detection. But even for them, most of the work has already be done by figuring out the overlap intervals beforehand. All that needs to be done is some lookup and simple mapping of car positions from one lane to another.

Note: all that the other cars have to do is stupidly follow the vehicle in front of them and not crash into it. Their next-vehicle-ahead changes very infrequently!

b) I really noticed that the reimplementation of the road system starts to pay off. With the old road system I worked on the same problem for at least a week and didn't get as far. Today: just one day!

That's all! I hope you enjoyed it, see you soon!

Discussion on /r/Citybound
Support Citybound