1
General / Re: [SFML 2.0]Problem with A* algorithm
« on: August 18, 2016, 07:33:32 pm »
Ok, got it to be working properly. Well, kinda.
This's how it's working so far. Green rects are "open", red "closed", black are impassable. It's not slow, I've just set a 1s timer between findPath() function calls.
Now I know the algorithm ends when it should be, however I have a problem with filling PathSet. Algorithm stops when "currentNode" is equal to "EndNode" (i've got == an != operator overloads with object.position comparision)
I don't think so, but should I disable checking from ClosedSet? How am I able to get my ParentNode as Node class object? AFAIK class can't contain it's own objects.
bool aStar::findPath(sf::Vector2i start, sf::Vector2i end, int tmap[10][10], int collisionMap[10][10])
{
cout << "START" << endl;
cout <<end.x<<" " << end.y <<endl;
StartNode.position = start;
EndNode.position = end;
OpenSet.push_back(StartNode);
while (OpenSet[minfCost(OpenSet)] != EndNode )
{
Node CurrentNode;
CurrentNode = OpenSet[minfCost(OpenSet)];
OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
ClosedSet.push_back(CurrentNode);
tmap[CurrentNode.position.y][CurrentNode.position.x] = 2;
if(CurrentNode == EndNode) {break; }
// neighbors
for (int y=-1; y<=1; y++)
{
for (int x=-1; x<=1; x++)
{
int checkX=CurrentNode.position.x+x, checkY=CurrentNode.position.y+y;
if(x==0 && y==0) { continue; }
if(checkX < 0 || checkX > 9 || checkY < 0 || checkY > 9) {continue; }
if(collisionMap[checkY][checkX] == 0)
{
Node newNode;
newNode.position = sf::Vector2i(checkX, checkY);
newNode.parentPosition = CurrentNode.position;
if((newNode.position.x==CurrentNode.position.x && newNode.position.y!=CurrentNode.position.y) ||
(newNode.position.y==CurrentNode.position.y && newNode.position.x!=CurrentNode.position.x))
{ newNode.gCost = 10 + CurrentNode.gCost; }
else
{ newNode.gCost = 14 + CurrentNode.gCost; }
newNode.hCost = heuristic(newNode.position, EndNode.position);
newNode.fCost = newNode.gCost + newNode.hCost;
OpenSet.push_back(newNode);
tmap[checkY][checkX]=1;
OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
ClosedSet.push_back(CurrentNode);
}
}
}
cout << CurrentNode.parentPosition.x << " " << CurrentNode.parentPosition.y<< endl;
}
/*
int tmpx=OpenSet[minfCost(OpenSet)].position.x, tmpy=OpenSet[minfCost(OpenSet)].position.y;
tmap[tmpy][tmpx] = 5;
cout <<"BUILD PATH" << endl;
*/
cout << "EXIT FINDING" << endl;
OpenSet.clear();
ClosedSet.clear();
return true;
}
{
cout << "START" << endl;
cout <<end.x<<" " << end.y <<endl;
StartNode.position = start;
EndNode.position = end;
OpenSet.push_back(StartNode);
while (OpenSet[minfCost(OpenSet)] != EndNode )
{
Node CurrentNode;
CurrentNode = OpenSet[minfCost(OpenSet)];
OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
ClosedSet.push_back(CurrentNode);
tmap[CurrentNode.position.y][CurrentNode.position.x] = 2;
if(CurrentNode == EndNode) {break; }
// neighbors
for (int y=-1; y<=1; y++)
{
for (int x=-1; x<=1; x++)
{
int checkX=CurrentNode.position.x+x, checkY=CurrentNode.position.y+y;
if(x==0 && y==0) { continue; }
if(checkX < 0 || checkX > 9 || checkY < 0 || checkY > 9) {continue; }
if(collisionMap[checkY][checkX] == 0)
{
Node newNode;
newNode.position = sf::Vector2i(checkX, checkY);
newNode.parentPosition = CurrentNode.position;
if((newNode.position.x==CurrentNode.position.x && newNode.position.y!=CurrentNode.position.y) ||
(newNode.position.y==CurrentNode.position.y && newNode.position.x!=CurrentNode.position.x))
{ newNode.gCost = 10 + CurrentNode.gCost; }
else
{ newNode.gCost = 14 + CurrentNode.gCost; }
newNode.hCost = heuristic(newNode.position, EndNode.position);
newNode.fCost = newNode.gCost + newNode.hCost;
OpenSet.push_back(newNode);
tmap[checkY][checkX]=1;
OpenSet.erase( remove(OpenSet.begin(), OpenSet.end(), CurrentNode), OpenSet.end() );
ClosedSet.push_back(CurrentNode);
}
}
}
cout << CurrentNode.parentPosition.x << " " << CurrentNode.parentPosition.y<< endl;
}
/*
int tmpx=OpenSet[minfCost(OpenSet)].position.x, tmpy=OpenSet[minfCost(OpenSet)].position.y;
tmap[tmpy][tmpx] = 5;
cout <<"BUILD PATH" << endl;
*/
cout << "EXIT FINDING" << endl;
OpenSet.clear();
ClosedSet.clear();
return true;
}
This's how it's working so far. Green rects are "open", red "closed", black are impassable. It's not slow, I've just set a 1s timer between findPath() function calls.
Now I know the algorithm ends when it should be, however I have a problem with filling PathSet. Algorithm stops when "currentNode" is equal to "EndNode" (i've got == an != operator overloads with object.position comparision)
I don't think so, but should I disable checking from ClosedSet? How am I able to get my ParentNode as Node class object? AFAIK class can't contain it's own objects.