Been messing around with code. Hmm, not going great, but have worked out logic for braking that appears fairly solid:
- work out distances between you and other cars
- if velocity is smaller than the distance, use velocity
- if distance is negative, ignore
- find the minimum value, i
- subtract 1 from i — this is the distance between current car and next car.