how to go about dynamic road creation in city builder game?

Hello all,

I am working on a city builder style game and have been stumped for weeks on how people go about creating automated roads that connect buildings.

For example I instantiate two buildings and then a road goes around both and connects automatically. I really want to avoid having the player make their own roads.

Does anybody have any advice or is adding road pieces to the build menu the way to go?

Thanks.

I did not implement such a system myself, but my educated guess how to do it, is the following: You could use something like A* (or Dijkstra) algorithm. You first create roadpatches around the buildings (I assume that a road has a minimum size in both dimensions, so I call it patch). Then you try to find the shortest possible way from one building to the next like in character pathfinding.

Therefore every new roadpatch should cause a cost. The algorithm tries to minimize that cost. If a new roadblock would overlap with an already existing roadpatch, the cost could be little or zero and therefore the algorithm would prefer to connect to already existing roads. Furthermore you could increase the cost, if the current shortest path has several changes in direction. This would prevent the roadbuilder from creating jittery roads. (Especially if your roads can only go vertically or horizontally).

By building a road completely around each building like you mentioned, the algorithm would on top of that always find a good connection to the building, no matter where exactly around the building you start searching. That is because already existing roadpatches add little cost.