SE450: Project Description: Car Behavior [3/16] Previous pageContentsNext page

The behavior of a car depends up the distance to the nearest obstacle. There are three attributes regulating this behavior, which may vary from car to car.

 maxVelocity    // The maximum velocity of the car (in meters/second)
 brakeDistance  // If distance to nearest obstacle is <= brakeDistance,
                //   then the car will start to slow down (in meters)
 stopDistance   // If distance to nearest obstacle is == stopDistance,
                //   then the car will stop (in meters)

Cars also have the following attribute, which determines how much space the consume:

 length  // Length of the car (in meters)

Suppose a car has the following values for the first three attributes.

 maxVelocity = 30.0 
 brakeDistance = 10.0
 stopDistance = 1.0

If the nearest obstacle is 12 meters away, then the car will assume its maxVelocity. If the nearest obstacle is 1 meter away, the car will stop. If the nearest obstacle is 5 meters away, the car will consider the distance to the obstacle in computing its velocity. You can use the following formula to compute the next position of the car.

double velocity = (maxVelocity / (brakeDistance - stopDistance))
                  * (distanceToObstacle - stopDistance);
velocity = Math.max(0.0, velocity);
velocity = Math.min(maxVelocity, velocity);
nextFrontPosition = frontPosition + velocity * timeStep;

Since we do not consider acceleration, you do not need to store the velocity as an attribute.

A student comment The updateVelocity algorithm allowed cars move at their maximum velocity if there were no obstacles within braking distance. However, it's possible for a slow car to be in front of a fast car, outside the fast cars braking distance but within the fast cars maximum velocity. On the next time step the fast car jumps ahead of the slow car. To clarify, a fast car has position 0, maximum velocity 30, and braking distance 10. A slow car has position 15 and maximum velocity 10. On the next time step the fast car will update it's position to 0+30 = 30, leapfrogging the slow car that is now at position 15+10 = 25. To fix this, I changed the updateVelocity algorithm to say that if there's a car outside of our braking distance but inside our max velocity, we adopt velocity of distanceToObject / 2.

Previous pageContentsNext page