AI for basic 2D racing game

Started by
4 comments, last by Timkin 17 years, 4 months ago
Hey... here's my current situation...I have the basic setup for a simple 2D racing game up and running. All it is at the moment is a user controlled car going around a track. The track is defined by a 2D array. I was wondering if anyone could help me with some sort of AI..ideally when its finished i want 4 cars going around a track (2D Array tile-based), one of which is user controlled and the other three are computer controlled. Any help at all would be fantastic, thanks
Advertisement
The simplest thing you could do is assign a direction for each tile in the array. Then the ai cars could just ask which way they should be going, see if they're going that way. If they are, good, if not, turn in the appropriate direction. You could then add stuff to it, like checking to see if they're going to run into other cars and stuff, and turn in the 'best' direction to go around.

Basically most racing AI is some form of finite state machine, meaning it checks various attributes to see what the current state is and makes decisions based on that. Like I said, could just be checking what direction it should be going vs the direction it is going. More compilicated things would be checking how close to each side of the track it is vs other cars and if a turn is comming up how fast it's going and blah blah. Depends entirely on your game how much information is necessary to make the cars drive around how you want them to.
going along with the finite state machine idea. you can have a few states. one for each place that it is currently in. for example, if the current AI car is in first place, it would want to be more conservative and not take risky shortcuts and save up an powerups or nitro or whatever just incase it will need it later.

on the other hand, if the car is in dead last, you could determine how much it is losing and define a scalar for it. if it is in 4th and all 4 cars are neck-and-neck, there is no need to use its whole supply of nitro (or whatever is in the game). but if it is losing by a quarter of a lap on the last lap, it would want to use up whatever powerups, risk shortcuts, and go all out to make a desparate comeback.

you can create a scalar for any variable that you wish and adjust it for different cars to get different results for each car. for example:

if (currentState==fourthPlace)
{
if (distToNextCar<10)
{
nitroScalar=0
}
else
{
nitroScalar=distToNextCar/5
}
useNitro(100*nitroScalar)

if (distToNextCar>50)
{
searchForShortCut()
}
}

this is obviously and very over-simplified and stupid example. but you get the just of it. the hardest part will be steering and control. but the direction tiles are a good idea.
If you want to be able to create arbitrary tracks, a good bet would be to implement steering behaviors for your AI cars: http://www.red3d.com/cwr/steer/.

To make sure they go around the track in the right direction, you can use Path Following or Flow Following.
Quote:Original post by Rixter
The simplest thing you could do is assign a direction for each tile in the array. ...


So basically a vector field to control car's motion.

This gives me an idea, what if initially the tiles all have null vectors.(or some simple default)
but as the player races across them, they are adjusted to more closely match the player's path.(might use some weighting factors... how well he's doing compared to last laptime, etc)
In this way, the AI cars would adapt to more closely match what the player(assuemd to be smart) is doing.
Quote:Original post by Anonymous Poster
This gives me an idea, what if initially the tiles all have null vectors.(or some simple default)
but as the player races across them, they are adjusted to more closely match the player's path.(might use some weighting factors... how well he's doing compared to last laptime, etc)
In this way, the AI cars would adapt to more closely match what the player(assuemd to be smart) is doing.


This is just a form of supervised learning. The problem you'll face in using a human driver as an example is that they won't cover all of the state space. Typically they'll drive on only a narrow strip of track and when they're off this strip, it's very much context dependent (and usually involves other vehicles).

If you want to implement a reinforcement learning scheme, I'd recommend temporal difference learning applied on a non-uniform tiling of the track... you can find heaps of information on TD Learning online. As for the discretisation, I'd use a course resolution on straight sections and a fine resolution on curves.

Cheers,

Timkin

This topic is closed to new replies.

Advertisement