Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Path Finder AI

Rate me:
Please Sign up or sign in to vote.
4.37/5 (36 votes)
14 Jun 2004CPOL4 min read 97.9K   4.2K   54   19
A class to define a 2D space, put walls, set a start & end point, and return the best path.

Sample screenshot

Introduction

I started thinking the way to build a game AI that allows to define a path from a start point to an end point, through some walls. I created a C# library (path.dll) that allows to define a 2D space (MAXX, MAXY) and to add some "Walls". Walls are nothing else than rectangles. After I add all the walls, the path class computes the AI nodes that allows to turn around the walls. The AI nodes that are "visible" (no wall between them) are linked. So, the class implements a path find algorithm (based on C# delegates used to communicate from AI node instances). The result of this O_O algorithm is a sub class that represents a collection of AI NODE destinations from each AI node. In the sample image, we can see the walls (orange), the AI NODES (red), the start point (blue), and the end point (blue).

I propose a test window to run the path.dll library.

The idea

The idea is to define a 2D space. This is done by initialization of the Cartesio class. This class permits to add walls as rectangles in the 2D space.

Sample screenshot

After creating the walls, the Cartesio class creates (Create_ai_nodes() method) the AI nodes to "walk around" the walls. In the example, we can see 2 walls (in red) and the associated AI nodes.

Sample screenshot

Creating AI nodes, Cartesio automatically creates the "visibility arcs" that links nodes with each other. A visibility arc can be seen as a line that links 2 nodes without "touch"ing a wall. (See example.)

Sample screenshot

Finally, Cartesio creates for each node, a list of destinations, and the adjacent node to walk through to reach the destination (I call it AI_star). So, when we are in a node (or we can see it), we know the next node to go to reach each other node.

Sample screenshot

Finally, the class Super_path is initialized by passing a Cartesio object, and a starting point P1 and an end point P2. The Next() method "moves" P1 to P2 step by step.

While (!(supe_path.Next())) { //render 2d schene }

Using the code

You can see in the demo code I submitted, how the use of the Cartesio Super_path classes is really easy:

// initialize Cartesio with a 2d space of 300 X 300
  cart = new Cartesio(300,300);
  // add  walls to the 2d space
  cart.AddWall(56,56,100,10);
  cart.AddWall(156,15,11,231);
  cart.AddWall(10,135,114,26);
  // creates ai nodes and ai_stars for each node
  cart.Create_ai_nodes();

It might take some time depending on the number of nodes (with 80-90 nodes, we have acceptable time, remember that this operation has to be done only once). The number of visibility arcs influences creation time too.

 // initialize Super_path with start and end point , and Cartesio object.
  Np = new super_path(P1,P2,cart);
  
   // Next() consent to move the point Np.Pa on the path from P1 to P2
   while (!Np.Next())
   {
    P = Np.Pa;
    // render scene..

Optimization

When you put one wall over anther wall (or another set of walls), then the Create_ai_nodes method discards the non-useful nodes that are positioned into a wall. See example:

Sample screenshot

Delegates and the find path algorithm

I assume that the reader knows how delegates/events work in C#.

I tried to explain how to find out the best choice from the adjacent nodes for a node S to reach a node E.

First of all, during the creation of AI nodes, we create a delegate for each node, and we add to the "listener" list represented by that delegate, all the adjacent nodes.

To go from S to E, let's start from E (backwards). E "casts" a message that contains a reference to E (To), a reference to S (From), and a reference to the node that casts it (Through) (in this case, E); the message contains also a distance D (0 in this case).

When a node receives a message M:

  • if it's the first message, it stores the distance D + dist(this, M.Throu), and it casts a new message changing D with D + dist(this,M.Throu) and M.Throu with itself;
  • if it's not the first message, if D + dist(this,M.Throu) < the distance stored in the node, then it stores the distance D + dist(this,M.Throu), and it casts a new message changing D with D + dist(this,M.Throu) and M.Throu with itself;

For each message M received by S, S considers the one with min M.D, and so S knows that it can reach E passing from M.Trou.

In the next figure, we se an example of message propagation form E to S (blue arrows). The example shows only some messages. When S receives messages 9 and 10, it evaluates (considering the distance carried by the messages) which path is the best.

Sample screenshot

How To Use Test Window

Test window's interface is very simple. You can draw Walls (move mouse on screen while left click). After that, remember to click "Create AI Nodes". Then you can set the start and end points (right and left click) and see the origin point reach the destination. (It's a kind of click and go interface). You can also click on Simulation to see an automation on 2000 paths to strongly test my work. Last but not least, you can clear the walls, so you can redraw them.

Hope you enjoy it and return me some good suggestions.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Blagoje11-Mar-16 15:00
Blagoje11-Mar-16 15:00 
QuestionWhat heuristic function is implemented here within the A* algorithm?? Pin
Member 964004518-Jul-13 10:28
Member 964004518-Jul-13 10:28 
Questionmore info about AI Pin
Member 964004516-May-13 4:48
Member 964004516-May-13 4:48 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey21-Feb-12 23:48
professionalManoj Kumar Choubey21-Feb-12 23:48 
GeneralRTS Pin
nkka27-May-08 17:26
nkka27-May-08 17:26 
General[Message Deleted] Pin
Danny Rodriguez27-Jan-08 9:11
Danny Rodriguez27-Jan-08 9:11 
Generalmy page Pin
andrea contoli20-Sep-07 2:13
andrea contoli20-Sep-07 2:13 
GeneralThanks Pin
bumper2-Oct-04 11:23
bumper2-Oct-04 11:23 
GeneralRe: Thanks Pin
andrea contoli3-Oct-04 23:31
andrea contoli3-Oct-04 23:31 
GeneralRe: Thanks Pin
bumper4-Oct-04 3:49
bumper4-Oct-04 3:49 
Generalgood Pin
cntdnk6-Jul-04 1:03
cntdnk6-Jul-04 1:03 
GeneralArticle improved Pin
andrea contoli30-Jun-04 2:21
andrea contoli30-Jun-04 2:21 
GeneralCool Concept Pin
Labrat00216-Jun-04 20:08
Labrat00216-Jun-04 20:08 
GeneralRe: Cool Concept Pin
andrea contoli17-Jun-04 5:43
andrea contoli17-Jun-04 5:43 
Generalarticle... Pin
(Steven Hicks)n+115-Jun-04 16:10
(Steven Hicks)n+115-Jun-04 16:10 
GeneralRe: article... Pin
andrea contoli16-Jun-04 0:06
andrea contoli16-Jun-04 0:06 
GeneralRe: article... Pin
(Steven Hicks)n+116-Jun-04 6:09
(Steven Hicks)n+116-Jun-04 6:09 
QuestionWhere's the article? Pin
Wackatronic15-Jun-04 11:17
Wackatronic15-Jun-04 11:17 
You should really expand on your article because it contains nothing. All you have submitted is downloadable code. Most people will give you a low vote on this because they have to download the code to see what you're talking about.

Regards,
Eric C. Tomlinson

P.S. I did the same on my first article, which I have since deleted after getting the same response.

Yes, I program in VB, but only to feed my addiction to a warm place to sleep and food to eat!

Visit my Code Project blog (Mobile Audio project)[^]
AnswerRe: Where's the article? Pin
andrea contoli15-Jun-04 21:08
andrea contoli15-Jun-04 21:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.