Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

I need help with A* pathfinding searching the neighbours for lowest GCost

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
3 replies to this topic

#1 SlashC++Programmer   Members   

139
Like
0Likes
Like

Posted 06 August 2014 - 03:33 AM

I need help with A* pathfinding searching the neighbours for lowest GCost

Guys I got the a* pathfinder almost down but my search neighbors for the lowest GCost code isn't working.

That code below should compare the last closeList index closeListNum with the the openList -7 index. It searches 8 moves back for the lowest GCost. I hope someone can help me out with this. :)

 

 // search neighbours
        lowestGCost = closeList[i-7].GCost;
        for (int i = openListNum-6; i < openListNum; i++)
        {    
            if (openList(i).GCost < lowestGCost)
            {    
                lowestGCost = openList[i].GCost;
                if (lowestGCost < closeList[closeListNum].GCost)
                {  
         
                    closeList[closeListNum].Num = openList[i].Num;
                    closeList[closeListNum].GridX = openList[i].GridX;
                    closeList[closeListNum].GridY = openList[i].GridY;
                    closeList[closeListNum].Heuristic = openList[i].Heuristic;
                    closeList[closeListNum].Parent = openList[i].Parent;
                    closeList[closeListNum].GCost = openList[i].GCost;
                    closeList[closeListNum].FCost = openList[i].FCost;
                      
                }

               
            }          
         }



#2 SlashC++Programmer   Members   

139
Like
0Likes
Like

Posted 06 August 2014 - 04:08 AM

oops the code was wrong a little bit here is the new code update.

 

 // search neighbours
        lowestGCost = openList[i-7].GCost;
        for (int i = openListNum-6; i < openListNum; i++)
        {    
            if (openList(i).GCost < lowestGCost)
            {    
                lowestGCost = openList[i].GCost;
                if (lowestGCost < closeList[closeListNum].GCost)
                {  
         
                    closeList[closeListNum].Num = openList[i].Num;
                    closeList[closeListNum].GridX = openList[i].GridX;
                    closeList[closeListNum].GridY = openList[i].GridY;
                    closeList[closeListNum].Heuristic = openList[i].Heuristic;
                    closeList[closeListNum].Parent = openList[i].Parent;
                    closeList[closeListNum].GCost = openList[i].GCost;
                    closeList[closeListNum].FCost = openList[i].FCost;
                      
                }

               
            }          
         }



#3 Andy Gainey   Members   

3452
Like
1Likes
Like

Posted 06 August 2014 - 07:28 AM

Is openListNum guaranteed to be greater than or equal to 7 every single time you reach this code?  Strange things would likely happen if openListNum were 3, for example, since you'd be reading incorrect memory.  But that's just a guess; you never mentioned what actually happens, other than that presumably you don't get the path you were hoping for.

 

Have you tried stepping through the code with a debugger?  If not, now is the perfect to learn how.  The end-result behavior of a misbehaving pathfinding algorithm can be difficult to interpret.  Watching the code execute line by line and observing the values it produces can make the error very obvious in many cases, though.



"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke

#4 ferrous   Members   

6034
Like
0Likes
Like

Posted 06 August 2014 - 09:19 AM

Yeah, that -6 and -7 both look really weird to me, but maybe fine depending on implementation?  I just am thinking of say, the corner of a 2d grid, it only has 3 neighbors.






Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.