# Making an AI car that dodges traffic

I am making a racing game where you race an AI opponent across a traffic-filled 3 lane highway, and I’m trying to figure out what would be the best way to make the opponent AI dodge the traffic. Currently I just check what traffic is ahead of the car by reading the position (with some additional checks) and tell the car to turn accordingly, however it often gets stuck between traffic that is too close to eachother or doesn’t turn fast enough. Any help is appreciated.

Could you pls post a video and some code? Thanks!

what i’ve used for that in the past is something called ‘steering behaviors’ or ‘flocking behaviors’ (same thing afaik). The TL:DR version is: imagine each car has an invisible feeler coming out of it to the slight left and to the slight right. like 15-30 degrees offset from center, either way. and all those do is return if an obstacle was encountered prior to the feeler reaching it’s goal distance (which doesn’t have to be particularly long, even a couple vehicle lengths is probably fine).

So now you got two feedback values, of whether there’s an obstacle to the left or right, or both (Dead ahead). then the idea is, if one side has an obstacle and the other doesn’t, turn away from the obstacle at a rate determined by how close it is. So a far away obstacle would result in a slight turn away from it, but if it’s very close, a sharp turn. Additionally, it’s good to slow down when about to hit an obstacle, so speed could become a factor of how not-impeeded both directions are (ie if either is obstructed, slow down to a scale determined by the closer obstacle).