Flash obstacle avoidance prototype

Obstacle avoidance can be very important in Flash gaming because allows designers to create smart enemies.

Obstacle avoidance behavior gives a character the ability to maneuver in a cluttered environment by dodging around obstacles.

In this prototype, I'll try to simulate everyday life.

In everyday life, you walk straight until an obstacle appears in your line of sight.

Then, you slow down, and choose a random direction to cross the obstacle. If necessary, you make two or three steps back and approach the obstacle again.

Well, I hope you don't act this way in your real life but this is what we are going to do in this prototype.

It's up to you to improve it and make a more realistic movement.

In the project there are 20 random obstacles placed in the same way as seen on Create a Flash Game like Nano War, and an object linked as runner running through them.

Runner's behavior is explained in the commented script

ACTIONSCRIPT:
  1. // movieclip that will host all obstacles
  2. createEmptyMovieClip("obstacles", 1);
  3. // placing 20 obstacles and the player
  4. for (x=1; x<=21; x++) {
  5.     // can_place = true: I can place the obstacle / false: I can't
  6.     // initializing it to false to force entering the while loop
  7.     can_place = false;
  8.     // while loop to look for an empty space where to place the obstacle
  9.     while (!can_place) {
  10.         // setting can_place to true
  11.         // you can always place an obstacle until you can't...
  12.         can_place = true;
  13.         // generating random x and y positions, and random width
  14.         px = Math.floor(Math.random()*400)+50;
  15.         py = Math.floor(Math.random()*400)+50;
  16.         // if I am at the end of the cycle, it's time to place the runner
  17.         if (x == 21) {
  18.             w = 20;
  19.         } else {
  20.             w = Math.floor(Math.random()*100)+10;
  21.         }
  22.         // scanning all already placed obstacles
  23.         for (y=x-1; y>=1; y--) {
  24.             // determining the distance between the y-th obstacle and the one I am trying to place
  25.             dist_x = px-obstacles["sphere_"+y]._x;
  26.             dist_y = py-obstacles["sphere_"+y]._y;
  27.             distance = Math.ceil(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
  28.             // this is the minimum distance I want between two obstacles:
  29.             // the sum of both radiuses plus 20 pixels
  30.             min_distance = (w+obstacles["sphere_"+y]._width)/2+20;
  31.             // if the new obstacle would be too close to an existing one...
  32.             if (distance<min_distance) {
  33.                 // set can_place to false
  34.                 can_place = false;
  35.             }
  36.         }
  37.     }
  38.     // I exited the while loop so I can place the unit...
  39.     // ... or the runner...
  40.     if (x == 21) {
  41.         _root.attachMovie("runner", "runner", x, {_x:px, _y:py, _width:w, _height:w});
  42.     } else {
  43.         obstacles.attachMovie("sphere", "sphere_"+x, x, {_x:px, _y:py, _width:w, _height:w});
  44.     }
  45. }
  46. // this movieclip is used only to visually trace the line of sight
  47. createEmptyMovieClip("collisions", _root.getNextHighestDepth());
  48. // random starting angle, in radians
  49. angle = Math.random(6.28318531);
  50. // starting speed
  51. speed = 2;
  52. // minimum speed, negative to allow runner to make some steps backward
  53. min_speed = -2;
  54. // maximum speed
  55. max_speed = 4;
  56. // this is the line of sight
  57. forecast = 50;
  58. // step, in radians, the runner will turn if it finds an obstacle
  59. turning_step = 0.5;
  60. // this is the acceleration/deceleration rate
  61. speed_step = 1;
  62. runner.onEnterFrame = function() {
  63.     // finding the end point of the line of sight
  64.     forecast_x = this._x+forecast*Math.cos(angle);
  65.     forecast_y = this._y+forecast*Math.sin(angle);
  66.     // drawing the line of sight
  67.     collisions.clear();
  68.     collisions.lineStyle(1, 0x00ff00);
  69.     collisions.moveTo(this._x, this._y);
  70.     collisions.lineTo(forecast_x, forecast_y);
  71.     // if an obstacle is in the line of sight
  72.     if (obstacles.hitTest(forecast_x, forecast_y, true)) {
  73.         // if the runner still has not turned
  74.         if (turn == 0) {
  75.             // decide where to turn
  76.             turn = Math.floor(Math.random()*2)+1;
  77.             switch (turn) {
  78.             case 1 :
  79.                 angle -= turning_step;
  80.                 break;
  81.             case 2 :
  82.                 angle += turning_step;
  83.                 break;
  84.             }
  85.         }
  86.         // decelerate
  87.         speed -= speed_step;
  88.         if (speed<min_speed) {
  89.             speed = min_speed;
  90.         }
  91.     } else {
  92.         // accelerate
  93.         speed += speed_step;
  94.         if (speed>max_speed) {
  95.             speed = max_speed;
  96.         }
  97.         // turn = 0 means "next time I will find an obstacle I will have to decide where to go"
  98.         turn = 0;
  99.     }
  100.     // updating speed
  101.     x_speed = speed*Math.cos(angle);
  102.     y_speed = speed*Math.sin(angle);
  103.     //updating position
  104.     this._x += x_speed;
  105.     this._y += y_speed;
  106.     if (this._x>500) {
  107.         this._x -= 500;
  108.     }
  109.     if (this._y>500) {
  110.         this._y -= 500;
  111.     }
  112.     if (this._x<0) {
  113.         this._x += 500;
  114.     }
  115.     if (this._y<0) {
  116.         this._y += 500;
  117.     }
  118. };

and here it is

As you can see, sometimes the runner is quite dumb, but it's a good start to develop a more complex AI script for obstacle avoidance

Download the source code and enjoy

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (13 votes, average: 4.92 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

8 Responses to “Flash obstacle avoidance prototype”

  1. Andy Cook on August 25th, 2008 4:57 pm

    This is pretty cool and I can already see a few games that could be developed around it. The simplicity of it is also great. Good idea

  2. Monkios on August 25th, 2008 5:39 pm

    You could always read about backtracking if you want your AI to navigate in a labyrinth.

  3. Emanuele Feronato on August 25th, 2008 5:58 pm
  4. Jon B on August 25th, 2008 6:22 pm

    If you want to make it look a bit slicker and remove some of the apparent dumbness you could make the current runner act as an invisible ’scout’ for another runner who can follow the correct path without the last minute decision making lol.

  5. Nathan on August 26th, 2008 12:00 am

    Just noticed something, theres an ad on your title, don’t know if you want that there?

    This is pretty cool, sometimes the runner gets stuck in a rut, you could say, and will never hit any obstacles again, but as long as it has an obstacle in front of it, its cool

  6. souled on August 26th, 2008 12:21 am

    A lot of code, but works well. Now how do you get the player to go from one place to another avoiding obstacles?

  7. npgames on August 26th, 2008 6:12 am

    emanuele i demand you GET OUT OF MY HEAD

    im making a zombie game and was pondering how to do this for the past few days, and this just “MAGICALY” appears when i need it, i think not!

  8. Wocker on August 27th, 2008 10:43 pm

    reminds me of the opensteer project. Take a look for some good (and opensource) sample code.

    http://www.red3d.com/cwr/steer/

Leave a Reply