I have implemented A* pathfinding to use in a game that im making for a project. The issue is that the AI that's using the pathfinding does not seem to wanna follow the path correctly. as displayed in the picture
blue dots = AI's path
yellow dots = path to take according to the pathfinding
red dots = impassable wall
As you can see the AI completly ignores the path to follow according to the pathfinding. I have been twisting my head multiple times around and have been unable to figure out the issue.
Below is the code where the pathfinding is used by the AI and shows all the states it cant be in. Position and rotation is applied later down in the code which is not shown here.
if (_pathState == SEARCHING)
{
if (glm::length(enemy->getPosition() - player->getPosition()) < 10.0f)
{
_isAtGoal = true;
_pathState = ATTACKING;
}
_pathFinding->findPath(enemy->getPosition(), player->getPosition(), map);
_isAtGoal = false;
if (_pathFinding->foundGoal)
{
_targetPos = player->getPosition();
_pathState = FOUND_GOAL;
}
}
if (_pathState == FOUND_GOAL)
{
if (!_isAtGoal)
{
glm::vec3 targetDistance = _pathFinding->nextPathPos(enemy->getPosition(), getRadius()) - enemy->getPosition();
angle = glm::degrees(atan2(targetDistance.x, targetDistance.z));
rotation = glm::angleAxis(glm::radians(angle), glm::vec3(0, 1, 0));
_velocityX += 0.1f * cosf(angle);
_velocityZ -= 0.1f * sinf(angle);
/*if (glm::length(_targetPos - enemy->getPosition()) < 10.0f)
{
_pathFinding->intializedStartGoal = false;
_pathFinding->foundGoal = false;
_pathFinding->clearPathToGoal();
_pathState = SEARCHING;
}
if (glm::length(_targetPos - player->getPosition()) > 10.0f)
{
_pathFinding->intializedStartGoal = false;
_pathFinding->foundGoal = false;
_pathFinding->clearPathToGoal();
_pathState = SEARCHING;
}*/
if (glm::length(enemy->getPosition() - player->getPosition()) < 1.0f)
{
_isAtGoal = true;
_pathState = ATTACKING;
}
}
}
if (_pathState == ATTACKING)
{
if (glm::length(enemy->getPosition() - player->getPosition()) > 10.0f)
{
_pathFinding->intializedStartGoal = false;
_pathFinding->foundGoal = false;
_pathFinding->clearPathToGoal();
_pathState = SEARCHING;
}
else
{
glm::vec3 playerDir = enemy->getPosition() - player->getPosition();
angle = glm::degrees(atan2(playerDir.x, playerDir.z));
rotation = glm::angleAxis(glm::radians(angle), glm::vec3(0, 1, 0));
}
}
The tutorial that I followed when implementing the pathfinding is from here:
And my pathfinding is pretty much as follows. My own suspicion is that the radius is whats causing the problem, since the radius is just a set number that I didn't put any thought into when I set it.
cout
everywhere where the issue could come from, use the step by step debugger to trace what your code ia doing... This turns "suspicions" into clear facts. \$\endgroup\$