Pathfinding problems

Hello
I I'm trying to implement basic pathfinding to my tile editor program. This is the first time I'm trying to implement pathfinding so I found it most easy to stick with the depth first search algorithm, because I found a great example of how to use that algorithm on the internet.

I have managed to find the path from one tile to an another tile. The problem is that the program run extremely slow when it try to check which tiles who's walkable and who's not.

In the code example that I have used as a starting point, the guy who wrote it have used recursion, and the performance drops drastically.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int pathFinding::dfs(int x, int y, vector<pair<int, int> > path)
{
  if(path.size() > max_size){
    cout << "Returning : path size too big" << endl;
    return 0;
  }

  if(x < 0 || x >= TILES_X || y < 0 || y >= TILES_Y)
  {
    cout << "Returning : bounds" << endl;
    return 0;
  }

  if(map[y][x] == blockedTile)
  {
    cout << "Returning : blocked" << endl;
    return 0;
  }

  if(visited[y][x])
  {
    cout << "Returning : visited" << endl;
    if (tileMap[x][y] != 3)
    {
        tileMap[x][y] = 5;
    }
    return 0;
  }

  visited[y][x] = true;

  cout << "Visiting " << x << " " << y << endl;

  path.push_back(make_pair(x, y));

  if(map[y][x] == goalTile)
  {
    final_path = path;
    visited[y][x] = false;
    return 0;
  }

  ///I think the problem lies here
  dfs(x, y - 1, path);
  dfs(x + 1, y, path);
  dfs(x, y + 1, path);
  dfs(x - 1, y, path);

  visited[y][x] = false;

  return 0;
}


I also would like to know how I can move the sprite after I have found the path. I have stored the coordinates in the vector final_path, but how can I move the sprite so it moves smooth step for step until the sprite reach its destination?

btw sorry for bad English :)

Last edited on
?
Topic archived. No new replies allowed.