Create a Flash racing game tutorial – Artificial intelligence

Finally it’s time to start talking about a real Artificial Intelligence algorithm. As said, I won’t use waypoints because I want to focus on “real” artificial intelligence, I want to make cars drive like if they were controlled by a player.

The idea

Let’s imagine you are driving a car, you aren’t blind so you can see the track.

If you see the track has a left turn, you turn left.

If you see the track has a right turn, you turn right.

You always accelerate, unless you see the turn is sharp

The code

This is how I made: the car has a line of sight like the one developed for the survival horror prototype.

This means is have i lines of sight subdivided in j segments.

Every line of sight can be done with all j segments, if it never hits the track boundaries, or with a number n from 0 and j-1 if it hits the track boundaries while drawing the n-th segment.

When I have a line of sight made by j or n segments, I have to determine if this line is on the left or on the right side of the car. This can be easily done because if I have ilines, all lines < i/2 will be on the left and the remaining ones will be on the right.

Now I have the number of segments before I hit track boundaries and I know if it’s on the left or on the right side of the car.

I increase a variable called turn_left by the number of segments if the line is on the left side, or I decrease it if the line is on the right side.

In the ideal situation, when no lines hit track boundaries, turn_left is zero and the car continues straight.

If turn_left is positive, I should turn left, if it’s negative I should turn right, but only if the absolute value is greater than a certain tolerance.

I don’t want my car to turn left and right just because a single line of sight hit the track boundaries.

Same thing for the acceleration: I do not accelerate only if the absolute value of turn_left is greater than a certain tolerance.

Let’s translate it into AS3:

And this is the result:

As you can see, playing with rotation_step, angle_of_sight, sight_accuracy, sight_lenght, sight_steps and turn_tolerance you can have the car run by itself along the track.

The code is way to be perfect, and I know there are some cases it won’t work (but I won’t tell you when :))… anyway if you want to try it by yourself, just copy this new script into car_mc class you can find at step 1.

  • Kaustav

    That is quite awesome.

    • Kaustav

      If a little CPU expensive…=)

      • jose

        too perfect the car’s trayectory no errors no little desviations look like it had a train track

  • Majority of AI algorithms use some extra data, like waypoints or terrain map. The fact you are not using it doesn’t mean you do it better or more real. As a matter of fact your approach creates only limits.

    It is much more cpu consuming.
    You can’t determine wheter you’re going wrong way.
    You can’t tell how much distance you have left to the finish line or other cars.
    You can’t store any extra data like sensors, weapon pickup points, different types of terrain, shortcuts and so on.

    Artificial Intelligence NEED that kind of stuff. All you can do with this approach is car blindly going forward. If you would like to implement anything more complex and sophisticated you’ll get stuck.


    • Emanuele Feronato

      terrain map is the next feature I am going to develop. With line of sight, I can avoid other cars too.

    • pablopp

      szataniol, why are you so concerned about this? As far as I understand, those experiments are not meant to end up as a real game, Emanuele is just trying out some interesting approaches, or am I missing the point here?

      • I’m sorry if you percive my post like this. I just wanted to point that waypoints are inevitable if you are thinking about more complex AI. Sorry if anyone feel offended.

    • Kaustav

      On the other hand, waypoints have their problems too.
      What if the terrain gets changed (new road/blockages)? If there are oil slicks or mines or any other fancy traps?
      Waypoints are predictable, which causes problems with playability. If weapons are added, AIs have to move out of their path to shoot the player, then what happens?

      • @Kaustav – Waypoints doesn’t necessarily have to be x,y points. For example in a racing game the best approach I’ve seen is using convex sectors which consists of few x,y points, that allow you to precisely tell where is the car and what steps should it take next. You should check out this book:
        I can honestly recommend it to anyone who’s interested in artificial intelligence for games.


  • Ozren

    You rule !!

  • @pablopp Exactly!

    I like the experiment and idea, I’d use getObjectsUnderPoint instead of using hitTest and physical lines though. That way it could be used in conjunction with different tiles (terrain map approach).

    Keep it going Emanuele!

  • Szataniol, it looks like you’re little bit jealous. Try to stick to your own job. Maybe use some glue ;)

  • In intelligence there is the possibility of error.

    This is awesome but it’s not AI.

  • Jacob

    I gotta say your a pretty smart cookie. That was a genius idea. So it’s not the AI we usually see. It def is a form of artificial intelligence.

  • Emanuele Feronato

    Anyway Szataniol is right, you MUST use waypoints in some cases (just think about a track with a “8” shape) unless you don’t want to flag (and permanently save) every mistake CPU car makes in order to have a more and more accurate driving model… too much for just a Flash game.

    I could try a waypoint approach in conjunction with line of sight.

  • Thomas

    I’d like to see this optimised to take the fastest line. Can it be done in any way so it always hits the apex of the curve? Then you’ve got the perfect AI – so, it’s a challenge for you, to prove you can beat waypoints! (Then you have to add imperfections, hooray.)

  • Robert

    hey how is the road created..i cant find insid the movieclip or code that creates it

  • Robert

    and how can i change the graphics in the movieclips??

  • Robert

    nevermind…library was jsut messing up

  • How are you calling your packages? I’ve tried to look at the source fla file, but I get an “Unexpected file error” when I attempt to open it.

  • Straffehond

    Love this. I wonder what the car does if the road splits ip. Will it go lefty, righty, or will it crash?

  • Robert

    jsut tested can do figure 8 works great to…they dont get mixed up

  • Pingback: Weekly Digest for July 11th — Hello. My name is Václav Van?ura.()

  • suresh

    hi admin,

    i like this script, i want to create racing car game for user view the track into the car , please give tutorials.


  • Finally, Something that might will work as a heat tracing/targeting/homing missile!!! It was not what i where looking for right now. But this will work on my other project that i almost gave up, thanks!… I might be able to do some more stuff with this too :D …External code,AS 3.0 …Everything that i need ;D

    Thank you so much!, i have no idea how much time i’ve used trying to find a code that is in AS 3.0 and that works!

  • Ivan

    Looks intelligent alrighty! :D

  • HI Emanuele, nice test, I really liked how it looks in the end, I made a very similar test a couple of months ago and it turned out pretty nice as well, in the end I had to add a couple of points so that the car would know which way to go in case he crashed or something, you can check it out here if you want:

    anyways, thanks for the blog, it is really cool


  • amarnath


  • amarnath

    fantastic….simply superb

  • Jay

    How do I use this I dont know, When I download the step 1, then try to open the .fla file it wont open it says ‘unexpected file format’. Therefore how can i use the AI on my other racing game. Please help me I really need it.

  • Cássio

    The aproach used here is interesting, but you should try out something like neural networks, I developed one that could navigate well with only 3 “lines of sight” (I call them sensors)

  • Johnny

    This helped me in creating my own for a school project.

  • This paper can be a further reading:

    Game AI: Simulating Car Racing Game by Applying Pathfinding Algorithms

  • Super article. There’s another good article I found relating to how Flash games enemy artificial intelligence works: