zomgistania

Main page | About | Articles | Previous Posts | Archives

Tuesday, July 04, 2006

Traffic simulation

Since I've been very much in trouble because of traffic jams, I've been thinking about how traffic and such is actually simulated in games and applications.

Basically you need to have roads and vehicles. You also want intersections, otherwise it might be a quite useless sim :)


Roads are organized in stretches: a certain speed limit, a certain length and certain amount of lanes. For the sim let's say each stretch also has a starting point and an ending point, which can be connected to other stretches of road or intersections and such.

A lane on the other hand is a piece of a road which has a direction, coming or going, which depends on the point of view but for the sake of simplicity let's just define that "going" means moving from the start point of a stretch to the end point and "coming" the other way around.

Cars drive on lanes. They drive at a certain speed. They can also switch lanes if there are more lanes going to the same direction, so they need to be aware of other lanes and other vehicles around them. They also need to have a size and a position on the lane, so other vehicles can avoid them and such. This should also be used to determine whether a lane is full when a car is in an intersection trying to enter a lane.


There is also another way of thinking the road-lane thing... you could have "just" lanes. They would then hold the begin and end point links to other lanes, have a certain length etc.
I haven't yet determined which one is a better approach, the road-lane or just lanes.


So far code-wise this means something like the following:
A Road class which holds Lane objects, has a speed limit and a length. Also a begin and an end points, which can be references to other Road objects

A Lane class, has a reference to the parent road, a direction (coming/going) and a list of Cars on the lane. They should contain methods for cars entering and leaving the lane.

A Car class which has a speed, size and position in the current lane. It should also hold a variable which marks where it wants to turn in the next intersection so it can navigate to the proper lane and turn in the intersection.
Cars need methods for scanning for other cars, moving, stopping, changing lanes, maybe more.


The whole deal of moving around the lanes could also be left just for the Cars. The lanes wouldn't know about the cars on them. The cars would navigate how they best see to fit and only use the Lanes to help navigate around. This is another thing I haven't yet tried, as I'm still working on implementing the roads, lanes, cars and movement and such.



Intersections. Let's keep it simple for now at least. When a car arrives at an intersection, it must pick the lane where it can continue to the direction it wants to go. Therefore the lanes in intersections need to have target lanes in the road objects which are linked to the intersection. Also, cars should obey some kind of traffic regulations like letting the cars coming from right go first. So to do that the cars will have to be yet again able to see if there are any cars coming to the intersection from the other roads. Other things to note are, of course, traffic lights. Red or green for keeping things simple again. If the light is red, car stops. Green, go!



I made some classes for working some of these things out. I might post them later if I get them doing anything intresting.



Here's a small traffic simulator in Java. It may seem to get stuck when you adjust the sliders but just wait a moment and it'll continue.

2 Comments:

  • That link is awesome! I have been interested in how traffic jams are caused for a long time now and this is highly interesting.

    By Anonymous Anonymous, at 7:14 AM  

  • I have this theme for my College project. I would really need this project with comments and all. Could we have an agreenment on this? My e-mail is robert_hellboy@yahoo.com

    By Blogger Dragut Robert, at 3:37 PM  

Post a Comment

<< Home