Workshop › OutRun-style Racer › Step 33 of 44
Running into a car from behind costs most of your speed. A crash is: the car is less than 120 ahead of yours, less than 0.35 across from you, and you are faster. Then your speed drops to half of its speed.
```js
const gap = ahead(position + PLAYER_Z, car.z)
// Driving into a car from behind: you slow down to half its speed.
if (gap < 120 && Math.abs(car.x - playerX) < 0.35 && speed > car.speed) speed = car.speed / 2
```gap is how far the car is in front of your car (position + PLAYER_Z), not of the camera.Math.abs(car.x - playerX) is how far apart you are across the road.&& must hold; then speed = car.speed / 2.In the cars loop in update, under the car.z line, write the three new lines.
Skills: Collision detection
← Traffic moves Counting laps →