4
\$\begingroup\$

I would like to create a traffic system in my tile-based isometric game and I would like to ask for some good approaches because I don't have a clue how to begin.

Right now I have a tilemap and keep its data in an 2d array. I have... 1. tile objects (with properties like index, tile type etc.) 2. building objects (with properties like origin, size etc.).

I´ve already implemented a A* algorithm which allows me to find the best/shortest path from one tile to another but now I have to realize the movement from start to end tile with features like stackable cars.

I though about setting up a property for my tile object which tells me if a tile contains a car but right now two cars fits on one tile in my game on each lane so this approach might be not the best solution.

Edit: i´ve added an image with lanes drawn on my tilemap. Having lane objects might be a good idea but I need to connect them to the tile objects somehow.

Lanes on TileMap

Have anyone implemented a traffic system in a tile-based game or have any ideas how to realize that and would share his/her knowledge with me? Would be great!

Thanks a lot!

\$\endgroup\$
  • 2
    \$\begingroup\$ How you display the world (isometric) and how you simulate the world should be separate things. The data structures and algorithms for your traffic system are independent of how you display the world. Can you narrow the scope of this question a bit? Implementing a traffic system can be a pretty broad topic. Please edit your question with those changes. \$\endgroup\$ – MichaelHouse Jun 21 '16 at 16:17
  • \$\begingroup\$ "I don't have a clue how to begin"; come on... \$\endgroup\$ – v.oddou Jun 22 '16 at 1:46
  • \$\begingroup\$ Does each car need to travel from a particular origin to a particular destination, or do you just need a sense of activity in your streets? The first GTA was mainly focused on the latter, so their street tiles were tagged with which directions a car on them could travel. Each car would be spawned at the edges of the screen, pick a direction given from its current tile, and follow that into the next tile — meandering with no real destination in mind, but obeying local traffic rules so it looked plausible enough. \$\endgroup\$ – DMGregory Jun 22 '16 at 3:23
2
\$\begingroup\$

The simple solution would be to just have each actor calculate the ideal path using A* and then stick to it. Implement each lane of a road as a first-in-first-out queue. The edges in your A* graph should be the lanes, not the tiles. When a car wants to move onto a lane but that lane is already occupied by the maximum number of cars, they simply wait until it is empty.

The result will be that there will be traffic jams even though there are alternative routes which are longer, but would still be faster because they are less frequented. You could let the player figure that problem out on their own. Or you could help them by taking congestion into account when calculating paths so the AI actors avoid high-traffic areas.

First you need to figure out which lanes are congested. You can calculate the congestion factor of a lane by counting the number of cars waiting to enter the lane each tick and calculating the average over multiple ticks. Then you can use that congestion factor to add an additional cost to this lane when calculating A* routes. That way the AI actors will avoid highly frequented lanes and will distribute more evenly on alternative routes.

By the way, when you are looking for inspiration, you might take a look at Cities:Skylines. They have a really neat traffic simulation system.

\$\endgroup\$
  • \$\begingroup\$ The lane approach sounds interesting and logical. But for sure there have to be a relationship between a road lane and a road tile otherwise it´s not possible to spawn cars etc. Or what would be your suggestion? Anyway its kind of easy to define the traffic lights because you have to place one where lanes cross. Thanks for your idea! \$\endgroup\$ – Yheeky Jun 22 '16 at 12:49
  • \$\begingroup\$ @Yheeky Yes, each tile has n lanes (per direction) and each lane can hold n cars. \$\endgroup\$ – Philipp Jun 23 '16 at 2:41
0
\$\begingroup\$

I'm aware of two ways to do system simulations.

One is macro models. You create formulas that takes areas densities in account and evaluate likely traffic at various hours, and that gives you an "extensive variable" (in physics science terms). This technique is the fastest, and most controllable, but potentially less realistic and also boring.

The other is micro models. You create agents that live in the system, each with local laws and limited scope. "it's 8am I need to drive to this industrial site, A* and follow route. along the route stop if vehicle in front of me, wait until way is free."
This is computationally heavy, but it will give you emergent behaviors. That is, stuff you never planned or thought about, will happen. It's the most interesting way of doing things.

On the realism side, it's hard to say, for example "crowd simulation" research showed that adding inertia to people movement created emergent vortices and clogging that were seen in videos of real life crowds too. Which means, the slightest change on your local laws can radically change the macro result.

Same with weather simulation, first models included only atmosphere, then it included ground albedo, then it included vegetation, and each time you put something in, you don't get more precise results, you get antipodal results. Even with everything we can know about the state of earth at some point in time, US weather simulation predicted that Caterina would not land, while EU simulators predicted it would.
That's the misfortune of agents, the macro result is uncontrollable.

Maybe you can invent an hybrid, where macro simulation would weight in agent decisions.

\$\endgroup\$
  • \$\begingroup\$ I don't want to make it too complicated. My traffic system should have the basic features like single lanes and traffic lights. Traffic jams and business hours can be realized by increasing the spawn rate. I don't need something like multiple lanes or lane crossing. \$\endgroup\$ – Yheeky Jun 22 '16 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.