Game Development Reference
matchSpeed = Me.Speed
'Is the other guy a nutcase?
If OtherGuy.Speed > myDesiredSpeed Then
'He's going faster now than we want to go, ignore him
'and floor it.
matchSpeed = myDesiredSpeed
'Go to match speed if we can and if we want that much.
If matchSpeed > BestNextSpeed() Then matchSpeed = BestNextSpeed()
'Debug.WriteLine(Me.ID & " can do " & matchSpeed.ToString & _
" in lane " & somelane.ToString & " behind " & OtherGuy.ID)
At this point, you should give the code a thorough thrashing. If your code
misbehaves, there are numerous debug messages commented out that can be
turned on. Some of them are split over multiple lines for readability, so you will
need to uncomment all the lines involved. The easiest way is to select the lines
using the mouse and then click the Uncomment button in the toolbar. Look at
how the vehicles behave in tight groups and then open another lane and watch
how they react. Watch an inbound, high-speed unit slow and match speed over
many seconds. If you reduce the number of lanes, the cars in the closed lane come
to a screeching halt and then dart into traffic as soon as they get an open window
ahead of them, often cutting off oncoming cars. All sorts of accordion behaviors
can be demonstrated. In a two-lane situation with the fast lane at 55 pixels per
second and the slow lane at 50 pixels per second (easily arranged with the two
leading trucks), you can watch the last car in the slow lane change lanes after the
faster line gets past, which in turn keeps the slow line pinned to the slow lane
until the new last car in the slow line can make the same maneuver.
Is any of this emergent behavior? Who really cares? Much of the behavior here
can be deduced by studying the AI code of the individual agents. But some of the
patterns, such as accordion speed changes and fighting for a newly opened fast
lane, are not directly programmed in. In any case, the method gives realistic-
looking (if somewhat rude) behaviors quite cheaply—the hallmarks of emergent