Progblog post #14 – Race Game AI

Race Game AI

This time I have a whole post for myself. I wil write about the race game AI that I am developing in my graduation project.

The AI

When developing the race game AI there were a few basic things that it needs to do:

  • Follow a path.
  • Avoid obstacles.
  • Make decisions based on its skill.


Path Creation and Following:

The path following is done by dividing the track up into sections. To do this the lazy programmer’s way I created an editor, as seen below:

Race Game AI - SectionEditor

The sections are created by section interfaces. Each interface has at least one raceline node in it, but can have up to three in this case. These raceline nodes hold data such as its positional offset on the sections plane, but also the preferred speed for the AI. Al the nodes have a index and all nodes with the same index are connected into a race line. Race lines can also be merged into another line as you can see in the image, this automatically handled by the editor. The editor visualises these race lines and all its values so they can be easily adjusted, added or removed.

Now the AI only has to move to the currently selected node for it. The movement is exactly the same as that from the player’s vehicle. But with the AI the inputs are based on the track and obstacles in the general vicinity of the AI vehicle.

Obstacle avoidance and spacial awareness:

Now that the race game AI vehicles can follow a path it is likely that at one point it will encounter an obstacle on or of the track. These obstacles need to be avoided if possible so that the AI can finish the race. In this project I went about it by using triggers that are attached to the vehicle, 4 triggers in total.

Race Game AI - AI Triggers

When an obstacle is in one of these triggers it will let the AI know. When the AI is driving along and one of the triggers send a signal it will start a procedure to avoid the obstacle. The large trigger means that a threat is coming but doesn’t yet need full adjustment from the AI vehicle, such as another vehicle driving in front of it at the same speed.

The smaller trigger in front requires the AI to immediately adjust its speed. When the obstacle isn’t that deep into the trigger the AI will brake a bit, but if the obstacle is really close the AI will use its handbrake. When this happens there will also be running another part of the to calculate a way to move back on track so it can continue the race.

All triggers are responsible for the AI’s decision if it can change lanes. But the triggers on the side also handle the vehicle steering away from other vehicles when they come to close. This way they won’t constantly run into each other.

Race Game AI - Vehicle Recovery

Game Loop:

Now that I have an AI that can finish a full race and avoid obstacles it was time to create a loop for the game, to make it feel more like a complete project. To start I created a visual menu so that a new game can be started and the game can be exited.

Race Game AI - Start Menu

I also added a countdown and lap counter. To ‘finish’ it off I am going to add a end screen with the players race time, position and speed on finish.

To make the whole project complete for my graduation presentation I will be adding the following things:

  • End screen
  • Better crash recovery
  • Best line
  • AI adjusting itself to the player
  • Making the world more pretty
  • Add sound effects

So as you can see a lot has to be done in the coming two weeks and that is what I will be doing. So keep your eyes out for the next blogpost on the final parts of my graduation. Hopefully by then we can start on Survive & Thrive again.