Heuristic Value weird calculation

  a-star, algorithm, c++

I’m currently working on A* algorithm small project but I think there is a problem with manhattan distance. I mean, when the code equals to:

inline int32 manhattanDistance(Node* startNode, Node* endNode)
{
    int32 deltaX = std::abs(startNode->position.x - endNode->position.x); 
    int32 deltaY = std::abs(startNode->position.y - endNode->position.y);

    return deltaX + deltaY;
}

It’s returning the path as:
Valid Manhattan

but when I made a mistake before and the code was:

inline int32 manhattanDistance(Node* startNode, Node* endNode)
{
    int32 deltaX = std::abs(startNode->position.x - endNode->position.y); // there is a difference, startpos.x - endpos.y
    int32 deltaY = std::abs(startNode->position.y - endNode->position.y);

    return deltaX + deltaY;
}

and it’s returning the path as:
Invalid Value

and the invalid’s way looks better and more legit but slower, did I make any mistake or why the second one looks better?

And the diagonal distance works fine, so the problem is only in manhattan one.

Thanks for any answer

Source: Windows Questions C++

LEAVE A COMMENT