Little exploration here-- AI for cars

Hey guys, every now and then I think about doing a racing game. I was wondering how you would approach making AI for vehicles.

My idea (please critique) was to place nodes around the track that the vehicle would navigate to (roughly). I assume that the car would have to somehow measure the distance between the left side and right side and adjust itself to the center of the track.

Another alternative that I have considered is creating a bezier curve from the nodes I place around the track and somehow using that to guide the AI around.

What have been some of your approaches to handling car AI?

I think using a waypoint system like you describe is the standard way to do it. Pretty simple to implement. The only thing that really gets tricky is object/other car avoidance. The way I did this in the past(not Unity) was to make a simple vision system using a few raycasts that look to the sides, in front of, and a few places in between. If there’s not a clear path in front of the car, then it finds one and adds a temporary node to steer towards. I was also messing around with having tests based on the AI “driver’s” agressiveness that would decide to either steer towards cars comming up beside them, or to steer away to avoid them.

Of course you could just have the cars blindly plow through from node to node.

Oh, and add a minimum distance that the cars need to get to the node before they head for the next one. Otherwise they might start circling it.

That’s some great info JB–

is there anything else you might like to add? I’m only asking because for FPS shooters, or other arcade like games it is pretty simple to add in nodes and tell them to go to it-- but those kinds of things can turn extremely quick, stop really quick, etc.

So, I guess what I am getting at, is do you add in nodes for breaking / accelerating? For something like easy / medium / hard modes, did you add in maximum speeds or did the AI prediction get worse?

Also, I would assume that in a racing game there would be a TON of nodes to navigate the car around the various corners in the game.

I think there must be some optimal way of racing a track. I’m not quite sure just how to do it, but I think it almost sounds like a calculus issue (if someone could confirm, that would be awesome-- I don’t know calculus great, but I understand what it is for).

I would guess that the car Ideally would have to predict how far it is away from the next node, and then the amount it has to turn for several future nodes in front of it-- otherwise it cannot determine when to accelerate or when to brake.

JB, do you have a sample project I could look at? If I choose to make a game like F-Zero, or Wipe-Out, I’d like to see the approach you took before I attempt it myself.

And thanks for the reply!

P.S. I’ve had a few whiskeys, so I apologize for spelling mistakes.

A lot of it depends on how you model the physics of racing, and if you make the AI abide by the same rules as the player. Given a complex enough system the best bet may be to set up a node navigation system, with several possible paths for each turn. Then run the AI cars around the track with your physics and set up a simple learning system where each path through a turn is given a desirability based on outgoing speed - and a lookup based on incoming speed.

Essentially, your optimal course through a curve depends on your speed going in. Plus other factors like track conditions, tire-wear and traffic which you can probably ignore for now.

Take a look at a racing book such as Twitch of the Wrist or Speed Secrets… it;s way interesting stuff, sure to give you some good ideas.

Are the two books you mentioned related ot AI, or to actual racing? I have a list of 3 books I need to read / finish reading: Physics for Game Programmers, Design Patterns, and Refactoring.

I was just basically experemeting with making something more akin to Mario Kart and wasn’t using realistic physics, so the setup I used was fairly simple but worked rather well. Just a series of nodes(waypoints), once you get close to 1 node, start steering for the next. They were mainly added to the curves and beginning and ending of straightaways. Adding a value to nodes to help with making decisions does sound like a good idea though. So the node at the beginning of a straight path can tell the car to floor it and so on…

In my case though, my AI was more an attempt to give the cars different personalities and not so much about real physics, so I didn’t have to worry about the cars flying off the tracks. The waypoints were just there to actually get them around the track.

They are books about actual racing, though there is enough physics in Speed Secrets to make it useful along with Physics for Game Programming. Consider it optional though :wink:

Good case studies are obviously the GTA games (and their rip-offs), but also a plugin for 3dsmax called CityTraffic. It gives you a good idea of the minimum input required by you to then provide the AI with info on what to do. In terms of the AI itself, i’ve seen many examples of traffic using OpenSteer, therefore you would look in to UnitySteer (the Unity port).

On what you need:

  • Curves on centers of lanes, with direction encoded
  • Start and End point associations with things like traffic light timing, valid entries and exits to adjoining curves.
  • Valid parking spots, with entry/exit paths, and a timer to indicate average parking time. For a bus stop, this will be fairly short.
  • Additionally, for each lane curve you should include speed limit, and relative traffic density.

I think we are getting slightly off topic-- I was specifically referring to a racing game (not traffic)-- possibly something like Gran Turismo or F-Zero, or Wipeout or some sort of Nascar game where the physics of the vehicle around the turns is very important. Ideally, the vehicles would be driving at, or close to the optimal path around the track.

I think traffic would be much simpler to program in; I’d have nodes placed all over the place, and then have various things like stop light nodes, which would be attached to 4 sub nodes. If the light was yellow, it would tell anyone who reached a specific node to keep going, and any else to stop. You could even do something like have the nodes keep track of the vehicles and have the vehicles talk to eachother through the nodes when they reach things like 4 way intersections.

A game like Wipeout for example, (or even Mario Cart) gives the user of the game the ability to force the car into a skid, which can provide distinct advantages into aiming the vehicle into a certain direction-- and if you’ve watched Nascar, the cars look like the are just on the brink, or skidding ever so slightly around the track. However, I don’t think I’ve ever seen the AI in any of the above mentioned games utilize skidding for the AI (though, that would be pretty cool if they did).

I’m not sure how much you guys know about racing (and I claim to know very little) other than I know that you want to brake just before the turn and start accelerating at the apex. But that is a rule for only simple curves I guess, and for a track possibly F-Zero or Wipeout, where the track could be a cylinder or a half-pipe, have loops, etc, with various utilities to assist you like boost power or attacking, I can only imagine tough it must be to program the AI in as well.

I guess I am just trying to over complicate the AI though by mentioning the above games, because all I am really looking for is a way to calculate the ideal track route for a car (and I am glad you guys mentioned track traffic, because that definitely makes the need for multiple routes along a curve-- it might be more advantageous to be on the outside of a curve rather than the inside-- and that problem only gets worse if the curve is S shaped-- you might be able to pass a vehicle by being on the outside of the beginning curve, while being able to sneak in on the ending curve.

But to simplify, and set all things aside (such as missiles, various utilities to attack another vehicle, or some sort of boost power like Nitrous Oxide), has anyone tried to make a Nascar-esq racing style game where the way to win is to have a perfectly tuned vehicle looping perfect (or very close to perfect) routes around the track?

I’d like to know because if I were to end up making an F-Zero style game in the future, I’d like to know the technique for doing it rather than just trying to guess the track routes with various accelerate / brake nodes.

P.S. Thanks for the replies :slight_smile: