Follow Grid Search

Hi -

I came across a path finding example and got it implanted into my project and I think that part is working. It finds the path, (column/row ) to the player object . That part I haven’t been able to figure is how to get how to get the enemy to actually follow the path.

I’ve tried using the values from lastNode.mCol and lastNode.mRow in FoundSolution but haven’t been able to get it to work right.

I think I’m close but can’t get my head wrapped around the coding for this.

So in the canvas paint event I have (yeah some of the code is old )

if enemy.Ubound > 0 then // right now just 1 enemy.
end if

now in updateEnemy(g)

dim hitPlayer as Boolean
dim i as Integer

for i = 1 to Enemy.Ubound
  hitPlayer = Enemy(i).hitPlayer(HeroObject)
  if hitPlayer then
    PlayerDied = true
  end if

  Enemy(i).DoOneSearchStep(HeroObject) // start path finding. This is part of the path finding example. 

// ***************** Dumb AI **************************
// bad guys are really dumb, just wonder around aimlessly... not using path finding here yet. 
  if Enemy(i).PathCheck(enemy(1)) then
  end if
// *******************************************************
  Enemy(i).DirectionCheck // check for correct directional picture are used for animation. 

As part of the enemy class the path finding is part of it and when a solution is found it calls

FoundSolution(node As GridSearchNode)

Dim node, lastNode As GridSearchNode

Dim cw, ch As Integer   // cell width/height

 cw =32
  ch = 32

 //enemy x,y

  lastNode = endNode
 while lastNode.mParent <> nil
 node = lastNode.mParent

 'g.DrawLine node.mCol*cw+cw\2, node.mRow*ch+ch\2, lastNode.mCol*cw+cw\2, lastNode.mRow*ch+ch\2 // left over from example. 

 lastNode = node


It’s not really possible to provide advice without more information about how your application and data structures are set up. If your playing field is a simple two dimensional grid where the characters can move one position up/down/left/right on each move, and there are no obstructions anywhere, then it’s simply a matter of calculating the differences between the x/y coordinates of the good guy and the bad guy. If the absolute value of the x differential is greater than the absolute value of the y differential, then make a move in the x direction (positive or negative according to the sign of the differential). If the y differential is greater, then move in the y direction, the same way. That’s just about the simplest way to do things. However, if the playing field has obstructions or other topological peculiarities, then it quickly becomes much more complicated, in order to account for them, and you’ll need to build a node interconnection matrix to keep track of how things can move from one node to another.

Yep its 2d, charters can move in the up, down, left, right directions . Yes there are obstructions and I did implement a path find thing form an example I found, as partially shown in the code . Once a path has been found it calls the FoundSolution. I’ve been trying to get my mind wrapped around how to have the enemy follow the path found, setting the logic/code for the enemy x/y.


You mentioned that the path finding code appears to be working, and you just need to figure out how to follow the path. What kind of data does the path finder produce?

The path finder gives the rows & columns. So node.mcol, node.mrow has these values. Column width and height are 32x32.