It’s been another busy month and once again I’ve been focussing my efforts on obstacle avoidance… it’s been some time now since I’ve looked at anything else!
However, I’m starting to make progress and, although there are still a few collisions, the AI behaviour is getting to the point where some accidents are avoided and the cars are more challenging to race against. The key to the improvement was some time spent tweaking the values that control when an AI car will choose to avoid another car and how the AI cars detect other cars. On this last point I’d primarily been using three raycasts at the front of the car. These can be seen in this screenshot (from last month’s devblog):-
I was using a central raycast with another on either side angled out from the car. The result was inconsistent as, initially, AI cars would turn to avoid another car but but often wouldn’t turn enough and, as a result, would clip the edge of another car.
By setting up scenarios where cars needed to take avoiding action and stepping through the action I could see the issue. Initially the cars would detect an obstacle and turn to avoid it. This change of direction meant the obstacle would not be directly in front of the car. At this point the left or right angled raycast would detect the obstacle. The car would then turn some more to avoid the obstacle. However, as the car got closer to the obstacle then the angle of the side raycasts meant that the obstacle wouldn’t be detected when close to the car. This meant that the cars would move to avoid the obstacle but stop detecting it when it was close and therefore not move enough.
The solution was to “toe-in” the raycasts rather than have them “toe-out” as was the case originally. This means that the AI cars are more ‘concerned’ with objects that are directly in front of them at long range but less concerned about objects that are in front but off to one side. As the AI car approaches the obstacle then the side raycasts are increasingly likely to detect a side obstacle as well as one in front. The result is a more consistent move to avoid an obstacle.
The AI cars won’t always avoid an obstacle but their behaviour is more consistent and they are generally “doing the right thing” now. One other benefit has been that AI car behaviour when racing wheel-to-wheel is also more challenging now – as can be seen by this clip of the red AI car racing against the player (blue and white car).