# Patterns for racing AI behaviour

So let's say you've got a spline-based racing AI. Assume it can already handle the basics of braking and steering around the track.

How would you structure and implement collision avoidance, overtaking, drafting, blocking and other behaviours so your cars remain competitive but make interesting races?

Links to papers/implementations would be awesome.

• what kind of engine you are using, – Harshal Benake Aug 18 '16 at 16:12

Have you seen Steering Behaviors For Autonomous Characters? I would consider a "CrowdPathFollowing" algorithm for staying on the track and avoiding collisions. You might be able to mix in a "Flow Field Following" behavior for drafting, passing and blocking - make the flow field follow the the player's past and projected path, but split around his current location.

      /-------------\
----<  [playerCar]  >-----
\-------------/


That should cause the AI to tend to follow directly behind him and move directly in front to block, yet encourage clean passes.

• I'm aware of boids, but it's good reference. I'm a bit scared of many many behaviours (stay on racing line, avoid cars, block, draft, etc) interacting in ways that are too complex to tune, and ending up with the occasional edge case collision. This is fine for pedestrians, but catastrophic for race cars that need to keep moving. – tenpn Aug 5 '10 at 7:50
• I've worked with the OpenSteer API which implements a lot of this stuff. I've found it pretty easy to tune. In your case I'd make the "SteerToAvoidCollision" weight very high. I don't know what vehicles you are racing, but I'd think that if you get it right for the vast majority of cases, then the remaining occasional bump or bash due to an edge case is probably pretty realistic.... – AShelly Aug 5 '10 at 23:07
• Was that steering vehicles or something else? I'm scared of situations causing problems such as two vehicles ahead to your left and one ahead to your right. The average vector would result in a movement right, even though that might bring you into contact with the right-hand vehicle. – tenpn Aug 9 '10 at 13:04
• I was working mostly with pedestrians, so I can't swear it will work. One thing to remember is it doesn't have to be a blend of behaviors - it can be a heirarchy. So you can have collision avoidance take a higher priority to anything else. In the case you describe the car would only go right until the predicted path intersected the right-hand car's path, at which time avoiding that collion becomes the overriding force. You can also add special conditions: for instance, if no safe path is found in N frames, add hard braking. – AShelly Aug 9 '10 at 22:57

There was a session partly about that at this year's AI Game Dev conference in Paris. You can find the slides and a summary here:

http://aigamedev.com/open/coverage/paris10-report/#session9

• The link to the ppt is 404. >_< – tenpn Jul 15 '10 at 12:04
• @tenpn: It's a pptx: files.aigamedev.com/coverage/GAIC10_PaoloManinetti.pptx – Firas Assaad Jul 15 '10 at 13:29
• ta -- his approach is interesting but the code I'm currently working with is actually more advanced! – tenpn Jul 16 '10 at 11:37

### Anti-Pattern: Opponents adapting their speed

Unfortunately I'm not able to give You patterns, but I would like to name an anti pattern. Please do not try to adapt the speed of the AI drivers to the one of the human player. The idea behind this is to create fair racing conditions for amateurs as well as pros and to abandon the need of multiple difficulty levels (and their implementation).

However, what sounds good on paper turns out to be awful in practice and the MotoGP series failed miserably at implementing this behavior. It ended up in the opponents driving at 5mp/h when you stood still for a couple of seconds and the impossibility to tell whether you've improved in the game (very bad feedback wise)

• Thanks. I'm talking specifically about interesting race behaviours here rather than balancing or difficulty levels, but I'll bear it in mind. – tenpn Jul 15 '10 at 14:12
• Nearly all racing games include some form of rubber-banding to keep the player in the action, or keep the AI close to good player. The trick is to be subtle with it and know when to turn it off. Once the player notices they feel cheated. – wkerslake Jul 15 '10 at 19:42
• The MotoGP situation sounds like a problem of taking it too far, rather than a problem with rubber-banding in general. It is not unreasonable for drivers at the front to drive with more caution and cars at the back to try more desperately to close the gap, and I think there's value in modelling it that way and presenting that transparently to the player. – Jibb Smart Aug 19 '16 at 1:58
• This is an interesting post on the topic: gamasutra.com/view/feature/3920/… Basically they put you in 3 groups of AI racers and let you advance from group to group during the course of the race. – Felsir Aug 19 '16 at 16:47