Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / A* Pathfinding System

Author
Message
luke810
17
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 3rd Jun 2007 02:17 Edited at: 5th Jun 2007 07:00
I made an an A* pathfinding algorithm for a competition entry awhile ago and I thought some people might find it useful since I haven't seen any other A* systems for DarkBASIC Classic. It would work good with an RTS or RPG game but it definitely needs some modification. I suggest using it in conjunction with the matrix select system by Kevin Picone at the Underware Design website here. Just replace DestX=5 and DestZ=7 with DestX=MAtrixSelection_Xpos#(1) and DestZ=MAtrixSelection_Zpos#(1).

Edit: Look below for an edited version of the function. The DestX and Z in the new one have been changed to be input values for the function along with 2 new variables, MatrixTilesX and MatrixTilesZ. although the function doesnt require a matrix this specifies the width and height of your search area. I also forgot to note how important your closed list maximum value is because if the character isn't on an "island" you dont want it to appear as though the position you want to move to is inaccessible.

-The input value for the function is the object you are finding a path for

-To change the destination that an object is going for change the values DestX and DestZ in the function

-A value of 1 in the Grid array signifies that the sqaure is not walkable

-You can speed up the function by changing the way the arrays are setup and making them smaller but make sure they arent overloaded with data for the squares or the function will cut out automatically

-The second example in the file shows how to make a character that "learns" if the squares around it are walkable and remembers that

-The function doesn't require a matrix but it makes it easier

If you find any mistakes please tell me so i can edit the script in the download.

Edit2: Here is my sample of the pathfinding function that allows the use of fences along a node's edge. The fence sorrounds the wall in the center of the map. It isn't entirely complete as I have to make modifications to the grid array because it is 4 times larger in this function than in the function without fences and could bring down FPS for a large map.I just haven't found out how...

(It also lists all the variables and arrays and I'll be adding the rem statements and variable list to the download on this post.)


Attachments

Login to view attachments
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jun 2007 17:47 Edited at: 3rd Jun 2007 17:55
Looks pretty good, though without any REMarks it's hard to decifer -

Question, when looking at the distance between the current position and the destination position, are you checking the adjacent squares in all directions or just the most likely directions, then if it's possible to go in a more desirable direction, eliminating a check for any other adjacent squares? The less checks, the faster the algorithm.

When you're dimensioning the data list arrays, what are you basing their size on?



Also every time the function gets called, it DIM the arrays. This could be a bit of unnecessary overhead in terms of allocating memory - So if you wanted to find paths dynamically, the entire function is rerun to completion. is there a better way to handle this? How can the path be found one square at a time instead of the whole path ahead of time?

Enjoy your day.
luke810
17
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 3rd Jun 2007 18:33
Quote: "Looks pretty good, though without any REMarks it's hard to decifer "


I'll go through and add some rem statements to the function. To understand it completely you need to know how A* pathfinding works. There are alot of webpages about it if you check on google but i based mine off the one here.

Quote: "When you're dimensioning the data list arrays, what are you basing their size on?"


The openlist array should be the same as the f,g, and h value arrays. It is the array where the data for each square is stored. You would want it to be equal to the number of square in you map but for the best performance you should guess at around how many squares you would be checking to get from place to place on the map and go higher than that(The values in the example are alot higher than they need to be). The closed list array is the actual path you travel along so it can be smaller, but if you change that value you have to change the value at which the function cuts out to prevent you from overstoring the array.

Quote: "Also every time the function gets called, it DIM the arrays"


you could change that if you want but in examples 1 and 3 the function is only called once.

Quote: "How can the path be found one square at a time instead of the whole path ahead of time?"


You can't. If you tried to find one sqaure then you dont know if later on in the function that path would come to a dead end and it would have switched over to an entirely new path. I thought about this for a long time and couldn't find any way around it.


Quote: "when looking at the distance between the current position and the destination position, are you checking the adjacent squares in all directions or just the most likely directions"


I dont understand your question here. The only time the distance between the destination and current position is calculated is to determine the H value for a sqaure on the open list which is the number of squares to the destination sqaure mutliplied by ten ignoring any objects that might be in the way.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Jun 2007 19:03
Good Job! It'll be nice to see your additional comments. I think I'll have some fun messing around with this

Enjoy your day.
luke810
17
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 3rd Jun 2007 20:20
Here is a modified example 1. It includes rem statements and has some modifications to the input variables that make it easier to use.

luke810
17
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 4th Jun 2007 05:15
Is anyone else planning on trying this out? I'd like to get some input so I can improve on it. I'll be adding calculations for terrain effects and possibly something for creating smoother movement paths and fewer waypoints instead of just vertical horizontal or 45 degree lines.
dononeton
20
Years of Service
User Offline
Joined: 12th Jun 2004
Location: Tusaloosa, AL : USA
Posted: 5th Jun 2007 03:43
I tried it and if I moved the destination object the path doesn't update to the new position, but if I move the player it does update

AMD Athlon 64 x2 Dual Core Processor 3800+,MMX 3DNOW (2CPUs)
1024 MB RAM
GeForce 7300 GT 512 MB
luke810
17
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 5th Jun 2007 05:17 Edited at: 8th Jun 2007 06:40
I don't know why it wouldn't work. If you are using the script in the code box then you have to change the second and third values of the function to change the x and z coordinates of the destination sqaure. Are you just moving the actual objects? If so that would explain your problem. The function takes the grid coordinates of the character object and will try to find a path to the destination specified when the function is called, not the position of the destination object. I'm rewriting the scripts with better explanations of the parts of the function and a list of the variables so I'm sorry about any confusion, but here is how the modified function works:

Find_New_Path(obj,DestX,DestZ,MatrixTilesX,MatrixTilesZ)



Sorry about not adding rem statements and stuff again, I totally forgot about it when I posted and it was really stupid of me.
luke810
17
Years of Service
User Offline
Joined: 4th Sep 2006
Location: United States
Posted: 7th Aug 2007 17:38 Edited at: 7th Aug 2007 17:44
Alright, I rewrote this whole function so that its easier to use, but now after I place the initiation call the arrays dont dimension to the specified value. Can you not dimension them to a size retrieved directly from another array?



Edit: It doesn't work right yet so don't bother trying, im in the middle of modifying the pathfind function to run in different search modes.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 7th Aug 2007 19:08
Quote: "Can you not dimension them to a size retrieved directly from another array?"


You can, but you have to make sure you've set the array index to a value or it's value will be zero or undefined.

Dim NodesX(0)
Dim NodesZ(0)

Dim Astar_Grid(NodesX(0),NodesZ(0))

This isn't quite right because NodesX(0) has no value set to it nor does NodesZ(0). Just add some values: NodesX(0)=10 NodesZ(0)=10 and the other array will be dimensioned with those values.

Enjoy your day.

Login to post a reply

Server time is: 2024-07-01 03:36:17
Your offset time is: 2024-07-01 03:36:17