我每帧运行此功能大约30次并且经历了大幅减速。这个功能是它的原因,任何想法为什么?
bool Pathfinding::takeStep(
std::vector<std::vector<int>>* vec, int px, int py, int sx, int sy, int r)
{
if (px == sx && py == sy) return true; // destination reached
bool clear = true;
// step up
if ((*vec)[px][py - 1] == -1) // location not tested yet
{
for (int i = 0; i < r * 2; ++i)
{
int tx = px - r + i;
int ty = py - r - 1;
// Make sure test coordinates are on the grid
if (tx > 0 && ty > 0 && tx < width && ty < height)
{
// already stuck
if (grid[tx][ty] == 1) clear = false;
}
else
clear = false; // not on grid
}
if (clear)
{
// keep moving
(*vec)[px][py - 1] = (*vec)[px][py] + 1;
// add one to step count and confirm this way is clear
if (takeStep(vec, px, py - 1, sx, sy, r)) return true;
}
else
{
(*vec)[px][py - 1] = -2; // mark as already tried
}
}
return false;
}