0
\$\begingroup\$

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

enter image description here

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:

https://www.youtube.com/watch?annotation_id=annotation_654919&feature=iv&src_vid=-A2HYXj186c&v=NJOf_MYGrYs

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.

\$\endgroup\$
2
  • 5
    \$\begingroup\$ What generally really sucks abouy these tutorials is that they don't teach you the basics of programming: how to debug your code. Narrow down the potentital issue to the smallest scope, put 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\$
    – Vaillancourt
    Sep 27, 2017 at 9:47
  • \$\begingroup\$ I have been debugging, and my conclusion is that the problem is somewhere in the FOUND_GOAL state when the AI is moving towards the target. Possibly in the nextPathPos function, possibly the issue is the radius. Since the radius determines if the AI has touched a tile in the path, having a radius that's too big makes it so the AI touches many of the required tiles at once. The problem is that having a radius of 1.0f makes the AI just stay in place, while a radius of 5.0f seems to be too big. \$\endgroup\$ Sep 28, 2017 at 7:04

1 Answer 1

1
\$\begingroup\$

I found the issue, apperantly the angle calculation was incorrect along with the distance calculation in nextPathPos. The pathfinding works beautifully now.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Please accept own your answer with checkmark to the left. \$\endgroup\$
    – Engineer
    Sep 29, 2017 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .