The Homicidal Chauffeur

Imagine it's late at night. You are on foot and must cross a vast empty parking lot. Suddenly, from the shadows you hear a car engine start. Lights come on. Tires squeal. The car speeds toward you. You can turn faster but he can move faster. Can you survive?

Let's build a computer simulation to find out. In this simulation a white turtle represents the pedestrian and a red turtle represents the car. Sliders allow us to adjust the speeds of the car and pedestrian as well as the turning radius of the car. If and when the car hits the pedestrian, we hear a beep and the simulation ends. (Try the simulation at HC.html.)

We can customize the framework we developed earlier for this (be careful not to overwrite it, though.)

The init procedure will create two turtles:

crt 2

We define three globals:

globals [pedestrian car halt]

Here's how they will be initialized:

to init-globals
  set car turtle 0
  set pedestrian turtle 1
  set halt false
end

Updating a turtle depends on which turtle:

to update-turtle
  ifelse self = car
  [update-car]
  [update-pedestrian]
end

On each update the pedestrian turns at a 90 degree angle from the approaching car, then runs as fast as he can:

face car
rt 90
fd pedestrian-speed

Heading

The heading of a turtle is the number of degrees from North (Up). This is a number from 0 to 360. (So heading = 275 degrees is due west.) We can set the heading of a turtle directly with:

set heading x

We can also set the heading relative to the current heading:

rt x ; set heading heading + x
lt x ; set heading heading – x

NetLogo provides some useful procedures for computing headings:

towards agent           ; = heading towards agent

towardsxy x y           ; = heading toward the point (x, y)

face agent              ; set heading towards agent

facexy x y              ; set heading towardsxy x y

subtract-headings x y   ; = z where y +/- z = x

Let's figure out how update-car works. Suppose the car's heading is 70 degrees. Suppose the heading in the direction of the pedestrian is 50 degrees. Then the car should set it's heading to 50 degrees assuming it has a turning radius of at least 20 degrees. If its turning radius is only 5 degrees, then the best it can do is set its heading to 65 degrees. On the other hand, if the pedestrian's heading is 90 degrees (due east), then the car should set its heading to 75 degrees. After the heading is set, the car should advance. Be careful not to overshoot the pedestrian. To compute the distance to the pedestrian the car should use:

let dist distance pedestrian

It can now use the forward command (fd) to move forward. However, if car-speed < dist, the best it can do is move forward by car-speed steps.