Find Adjacent Nodes A Star Path-Finding C++

Posted by Infinity James on Stack Overflow See other posts from Stack Overflow or by Infinity James
Published on 2012-06-28T14:37:50Z Indexed on 2012/06/28 15:16 UTC
Read the original article Hit count: 216

Filed under:
|
|

Is there a better way to handle my FindAdjacent() function for my A Star algorithm? It's awfully messy, and it doesn't set the parent node correctly. When it tries to find the path, it loops infinitely because the parent of the node has a pent of the node and the parents are always each other.

Any help would be amazing. This is my function:

void AStarImpl::FindAdjacent(Node* pNode)
{
    for (int i = -1; i <= 1; i++)
    {
        for (int j = -1; j <= 1; j++)
        {
            if (pNode->mX != Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mX 
                || pNode->mY != Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mY)
            {
                if (pNode->mX + i <= 14 && pNode->mY + j <= 14)
                {
                    if (pNode->mX + i >= 0 && pNode->mY + j >= 0)
                    {
                        if (Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mTypeID != NODE_TYPE_SOLID)
                        {
                            if (find(mOpenList.begin(), mOpenList.end(), &Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j]) == mOpenList.end())
                            {

                                Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j].mParent = &Map::GetInstance()->mMap[pNode->mX][pNode->mY];
                                mOpenList.push_back(&Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j]);
                            }
                        }
                    }
                }

            }
        }
    }

    mClosedList.push_back(&Map::GetInstance()->mMap[pNode->mX][pNode->mY]);
}

If you'd like any more code, just ask and I can post it.

© Stack Overflow or respective owner

Related posts about c++

Related posts about path-finding