Tile based traffic controller system - source included

While working on an optimisation problem, I came up with this small demo / library for allowing simple controlling of hundreds of vehicles moving around a tile-based road system.

This zip contains the demo shown (with random road layouts) and also the library itself, with a… reasonable amount of commenting, so people can hopefully understand it :wink:

Any questions, ask away!

Nicely done, and smooth too!

I’ll have to have a dig around the code, but thanks for sharing, code snippets like these are always invaluable.

Cheers, always good to know things might be useful!

The data format for setting up the traffic routes isn’t necessarily the clearest, but if you look within assets/source there’s a PSD file with some additional information so hopefully you can see how the routes match up to the tiles.

The goal here was to pre-process as much data as possible so moving traffic involved as little work as possible, which I think is now reasonably optimised :slight_smile:

To get many cars running as possible, you can choose how many groups they are split evenly into. Each call to update() updates a single group. Fewer groups = smoother running (as each group gets updated more times per second), but also means heavier CPU load per update.

Speed values are multiplied by the number of groups, to maintain overall the same velocity over time, but naturally the more groups you have the jerkier the movement. It is a trade-off, but more groups is likely better on slower hardware than overall game slow-down.

Now if only I could dedicate more time to it, I’d put in freeways with multiple lanes, and it also did occur to me that getting in cars stopping at cross-sections is actually fairly simple, which might look quite sweet (bearing in mind cars don’t collide with other cars nor even know of their existence!)

Thank you for sharing. Will definitely check it out  :slight_smile:

Nicely done, and smooth too!

I’ll have to have a dig around the code, but thanks for sharing, code snippets like these are always invaluable.

Cheers, always good to know things might be useful!

The data format for setting up the traffic routes isn’t necessarily the clearest, but if you look within assets/source there’s a PSD file with some additional information so hopefully you can see how the routes match up to the tiles.

The goal here was to pre-process as much data as possible so moving traffic involved as little work as possible, which I think is now reasonably optimised :slight_smile:

To get many cars running as possible, you can choose how many groups they are split evenly into. Each call to update() updates a single group. Fewer groups = smoother running (as each group gets updated more times per second), but also means heavier CPU load per update.

Speed values are multiplied by the number of groups, to maintain overall the same velocity over time, but naturally the more groups you have the jerkier the movement. It is a trade-off, but more groups is likely better on slower hardware than overall game slow-down.

Now if only I could dedicate more time to it, I’d put in freeways with multiple lanes, and it also did occur to me that getting in cars stopping at cross-sections is actually fairly simple, which might look quite sweet (bearing in mind cars don’t collide with other cars nor even know of their existence!)

Thank you for sharing. Will definitely check it out  :slight_smile: