Results 1 to 12 of 12

Thread: VB: A* Pathfinding [Source]

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    VB: A* Pathfinding [Source]

    Here's a nice example of A* pathfinding. Includes:
    -Three tiles (wow! three!), grass, mud and tree.
    Mud is harder to walk through than grass, and trees are unwalkable.
    -Ability to change "spread factor" (the value the estimated cost is divided by, increasing the "spread factor" increases search time etc...)
    -Ability to change terrain settings... basicly chance stuff
    -Resize terrain from 8x8 to 256x256
    -Show the pathing while it happens
    -A heap class (basically a very fast list for getting the best element)
    -A functionals module with most of the functions I use normally
    -A DX7 module with (very) simple drawing functions

    Comments and suggestions are welcome.


    The zip file is attached to post #4
    Last edited by alkatran; Aug 21st, 2004 at 10:35 AM.
    Don't pay attention to this signature, it's contradictory.

  2. #2

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    And here's a screenshot.


    Red squares are obviously ones that have been "touched"
    Green squares are areas to which a better path was found
    *This is only possible because diagonal movement has a higher cost
    The numbers are the total cost of each square
    The lines show which squares were found from which square (duh)

    Oh, and the reason the last search timei is 2.32 seconds on a 16x16 map is because the option "search slowly" is selected. I wanted to get a screenshot in mid-search alright??
    Attached Images Attached Images  
    Last edited by Electroman; Aug 21st, 2004 at 07:01 AM.
    Don't pay attention to this signature, it's contradictory.

  3. #3

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    And another screenshot, with a larger map and large spread factor. (a 6.2 factor has the effect of turning this into a floodfill algorythm)
    Attached Images Attached Images  
    Don't pay attention to this signature, it's contradictory.

  4. #4

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Oh. No wonder. When I edited my message it must have deleted the attachment.

    Or maybe it was the fact that it was 2 am...

    Anyways here's the code.
    Attached Files Attached Files
    Don't pay attention to this signature, it's contradictory.

  5. #5
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    Looks really good. Mind I think there is some kind of bug? it doesn't go diagonal always? Like the screen shot shows it could get to the end position by going diagonal but doesn't, instead it goes off the side of the screen .






    EDIT: Testing it a bit more I see it only goes diagonal if the adjacent ones are not blocked. Guess does kinda make sense.
    Attached Images Attached Images  
    Last edited by Electroman; Aug 21st, 2004 at 10:13 AM.
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    That's because you can't walk through a tree. (The path must be clear on boths sides to move diagonally, it just makes sense)

    Here, look at this picture:

    As you can see, instead of passing diagonally by the tree, it went around it. Since to go diagonal there you would have to shave off an arm...


    *edit* The end is always highlighted in blue when the search is finished. It doesn't mean a path was found.
    Attached Images Attached Images  
    Don't pay attention to this signature, it's contradictory.

  7. #7

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    Actually, for proof that it is working, look at your own picture. Blocks which are just before corners only check the adjacent blocks but not the block "around the corner" (there is no line in between them).

    Note that a lack of a line doesn't always mean unreachable from that tile... lines usually 'go outwards' from the center and not sideways.
    Don't pay attention to this signature, it's contradictory.

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787
    ok thats pretty cool, but could you explain how a* path finding works? maybe a guide?

  9. #9

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    A* pathfinding works by:

    -Add the starting spot to the "Open" list
    -calculate its cost
    (loop starting here until end)
    -take the lowest cost spot in the open list
    -remove this spot from the open list and place it in the closed list
    -add all adjacent spots which are not in the closed list by:
    (a) if an adjacent spot is on the the open list, see if you've found a better path to it (this is show by a green highlight in this program)
    (b) if an adjacent spot is not in the open list, just add it
    Calculate the cost of all adjacent spots as you add them
    (repeat)

    Calculate the cost of a spot by adding the cost it took to get there, and estimating the cost to reach the end (I sum the X and Y tile position differences, for an estimate which is slightly too large)

    The cost of each spot is shown in this program by the white text.
    Don't pay attention to this signature, it's contradictory.

  10. #10
    Fanatic Member
    Join Date
    Dec 2002
    Location
    North Carolina
    Posts
    734
    That sounds kind of like the djistrkas (sp?) algorithm I saw in a math book once.

  11. #11

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860
    http://encyclopedia.thefreedictionar...ch%20algorithm

    I guess A* comes from it. There's a much better description of what the algorythm does there, too.
    Don't pay attention to this signature, it's contradictory.

  12. #12
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Israel
    Posts
    636
    Very cool stuff!!!
    Can be used for AI in games!

    Arie.
    Tip Of The Day: Fake it 'till you make it !

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width