A* pathfinding....

Blitz3D Forums/Blitz3D Programming/A* pathfinding....

psychicbottle(Posted 2012) [#1]
so i tried this on another forum but i think i did not place it well so i will try it here.
I am having trouble with my A* code, ive basically written it from scratch but am at a dead halt now cause im having trouble finding then storing the child nodes to the parent nodes. heres my code,

Last edited 2012


RemiD(Posted 2012) [#2]
I don't know how to achieve the same results with "Type" but maybe this will give you some ideas :

I don't understand what you mean by node childs.
There are nodes, and there are links between some nodes and some others nodes.

If you use a grid based system, there are maximum 4 links per node to others nodes.

I store the others nodes Id like this :
NodeLinkToNode(NodeId%,LinkId%) ;NodeId% is the id of this node, LinkId% is the id of this link (from 0 to 4 links per node in this case), and the stored value is the ONodeId% the id of the other node.

Now see why i use Dim instead of Type to store my nodes :
This is my function to create the links between nodes for a grid system :
Function LinksCreate(ZId%)
 For NId% = 1 To NodesCount(ZId)
  For ONId% = 1 To NodesCount(ZId)
   If(ONId <> NId)
    VLength# = VLength2D#(NodeX(ZId,NId),NodeZ(ZId,NId),NodeX(ZId,ONId),NodeZ(ZId,ONId))
    If(VLength# < 1.001)
     NodeLinksCount(ZId,NId) = NodeLinksCount(ZId,NId) + 1
     LId% = NodeLinksCount(ZId,NId)
     NodeLinkToNode(ZId,NId,LId) = ONId     
    EndIf
   EndIf
  Next
 Next
End Function

Don't worry about ZId%, you can remove them if you want, this is to store Nodes per Zone instead of having all the Nodes of the Level involved in the path calculation.

For a system with nodes not positionned on a grid, you want to increase the distance check and add a linepick or moving collider check between the Node and the ONode in order to check if a path is possible.

Then when you use the AStar routine, you need to retrieve the ONodes connected to the current Node.
You can do it like this :
NId% = 3 ; the id of this node
For LId% = 1 to NodeLinksCount(ZId,NId)
 ONId% = NodeLinkToNode(ZId,NId,LId) ; With this you get the Id of the ONode connected to this node
 Distance# = VLength2D#(NodeX(ZId,NId),NodeZ(ZId,NId),NodeX(ZId,ONId),NodeZ(ZId,ONId)) ; With this you have the distance from Node to ONode
 ;Then you need to calculate the distance from ONode to Target
 ;Then you need to add the 2 previous distances and to compare it with the others distances calculated with the others nodes connected to this node.
 ;Then you know which other node connected to this node is the nearest from this node and to the Target
Next


Last edited 2012


psychicbottle(Posted 2012) [#3]
thank you so much, RemiD.
i haven't gotten a chance to throuroughly look through this yet but from what i have seen it looks understandable and useful. i'll let you know if i have any problems with it if you dont mind.
cheers


psychicbottle(Posted 2012) [#4]
i did want to ask your opinion on this http://www.blitzbasic.com/Community/posts.php?topic=39354#440007

it mentions the use of banks since they use minimal memory and a very fast, would you think this is a viable option? maybe not for a beginner but just in general blitz3d pathfinding.


RemiD(Posted 2012) [#5]
I think you can use Dim arrays or Types to store the infos of your nodes.

What is important is to organize your code in a way that it doesn't have to search one node in a list with all nodes.

In the example above, for each Node, i store the possible links from this node to others nodes.

Then when i need to retrieve the infos of another node connected to this node, i only have to search a list of 4 nodes instead of a list of 10x10 nodes or 100x100 nodes (depending on your grid size.)

Another advantage of nodes compared to using Cell(X,Z) is that once you have created the walls, the doors, the columns, the furnitures, the plants, the rocks, you can create nodes only where the cells are empty, so even if you have a 100x100 grid, you will have less than 100x100 nodes. Hence, the list will be smaller.

If you want to go one step further, you forget about the grid based pathfinding, and you use a "node around obstacles" pathfinding which is, in my opinion, more realistic and faster. (there are nodes only around obstacles, and up to x links from each node to others nodes)