Skip to main content
Restructured Code
Source Link
Gnemlock
  • 5.3k
  • 5
  • 28
  • 59

How todo I implement A* pathfinding for enemies to follow the player?

The enemies in my game are supposed to follow the player in order to cause damage to it. Right now, I have it where the enemy just goes in the direction that will bring it closer to the player, and this works okfine, but the enemy gets caught on obstacles/solid and solid tiles. theThe Collision.tileCollision methods check to see if there is/will or will be a collision with any solid tiles, by adding the speed of the enemy to its current coordinate, in order to look ahead for collisions. If there is no collision, and the direction its checking will bring the enemy closer to the player, then it goes in that direction.

//Should/Can go right
if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) 
    == false &&
   (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed()) 
    - this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
{
    //Should/Can go right
    this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
}
//Should/Can go left
else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
     == false && (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed()) 
    - this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
{
    //Should/Can go left
    this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
}
//Should/Can go down
if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) 
    == false &&
   (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed()) 
    - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    //Should/Can go down
    this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
}
//Should/Can go up
else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
     == false && (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed()) 
    - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    //Should/Can go up
    this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
}

I have heard that the A* search algorithmA* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So howHow would I go about doing such a thing? By the way, myMy program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And thisThis is where I'm confused,confused; I understand how it would work, if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path, because that would take forever, and be extremely inefficient.

How to implement A* pathfinding for enemies to follow player

The enemies in my game are supposed to follow the player in order to cause damage to it. Right now I have it where the enemy just goes in the direction that will bring it closer to the player, and this works ok, but the enemy gets caught on obstacles/solid tiles. the Collision.tileCollision methods check to see if there is/will be a collision with any solid tiles by adding the speed of the enemy to its current coordinate in order to look ahead for collisions. If there is no collision and the direction its checking will bring the enemy closer to the player, then it goes in that direction.

//Should/Can go right
if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
   (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed()) - this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
{
    this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
}
//Should/Can go left
else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
        (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed()) - this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
{
    this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
}
//Should/Can go down
if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
   (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
}
//Should/Can go up
else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
        (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
}

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And this where I'm confused, I understand how it would work if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path because that would take forever and be extremely inefficient.

How do I implement A* pathfinding for enemies to follow the player?

The enemies in my game are supposed to follow the player in order to cause damage to it. Right now, I have it where the enemy just goes in the direction that will bring it closer to the player, and this works fine, but the enemy gets caught on obstacles and solid tiles. The Collision.tileCollision methods check to see if there is or will be a collision with any solid tiles, by adding the speed of the enemy to its current coordinate, in order to look ahead for collisions. If there is no collision, and the direction its checking will bring the enemy closer to the player, then it goes in that direction.

if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) 
    == false && (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed()) 
    - this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
{
    //Should/Can go right
    this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
}
else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel)
    == false && (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed()) 
    - this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
{
    //Should/Can go left
    this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
}

if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) 
    == false && (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed()) 
    - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    //Should/Can go down
    this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
}
else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) 
    == false && (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed()) 
    - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    //Should/Can go up
    this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
}

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. How would I go about doing such a thing? My program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. This is where I'm confused; I understand how it would work, if the entities also did tile based movements, but that is not how my game is set up. I don't want to check every pixel on the screen for a path, because that would take forever, and be extremely inefficient.

           //Should/Can go right
            if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed()) - this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
            {
                this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
            }
            //Should/Can go left
            else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed()) - this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
            {
                this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
            }
            //Should/Can go down
            if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
            {
                this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
            }
            //Should/Can go up
            else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
            {
                this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
            }

I have heard that the A* search algorithmA* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And this where I'm confused, I understand how it would work if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path because that would take forever and be extremely inefficient.

           //Should/Can go right
            if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed()) - this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
            {
                this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
            }
            //Should/Can go left
            else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed()) - this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
            {
                this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
            }
            //Should/Can go down
            if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
            {
                this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
            }
            //Should/Can go up
            else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
                    (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
            {
                this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
            }

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And this where I'm confused, I understand how it would work if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path because that would take forever and be extremely inefficient.

//Should/Can go right
if (Collision.tileCollisionRight(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
   (Math.abs((this.thisEnemy.getX() + this.thisEnemy.getSpeed()) - this.player.getX()) < Collision.getXDist(this.thisEnemy, this.player)))
{
    this.thisEnemy.setX(this.thisEnemy.getX() + this.thisEnemy.getSpeed());
}
//Should/Can go left
else if (Collision.tileCollisionLeft(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
        (Math.abs((this.thisEnemy.getX() - this.thisEnemy.getSpeed()) - this.player.getX())) < Collision.getXDist(this.thisEnemy, this.player))
{
    this.thisEnemy.setX(this.thisEnemy.getX() - this.thisEnemy.getSpeed());
}
//Should/Can go down
if (Collision.tileCollisionDown(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
   (Math.abs((this.thisEnemy.getY() + this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    this.thisEnemy.setY(this.thisEnemy.getY() + this.thisEnemy.getSpeed());
}
//Should/Can go up
else if (Collision.tileCollisionUp(this.thisEnemy, this.thisEnemy.getSpeed(), currentLevel) == false &&
        (Math.abs((this.thisEnemy.getY() - this.thisEnemy.getSpeed()) - this.player.getY()) < Collision.getYDist(this.thisEnemy, this.player)))
{
    this.thisEnemy.setY(this.thisEnemy.getY() - this.thisEnemy.getSpeed());
}

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And this where I'm confused, I understand how it would work if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path because that would take forever and be extremely inefficient.

Added how I'm really confused how to implement A* path finding if the enemie move freely but the obstacles are tile based
Source Link

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And this where I'm confused, I understand how it would work if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path because that would take forever and be extremely inefficient.

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement.

I have heard that the A* search algorithm is good for path finding, but I have no idea how to implement the algorithm into my program in order for the enemies to not get stuck on walls when following the player. So how would I go about doing such a thing? By the way, my program is tiles based for obstacles such as walls, but entities like the player and enemies have free movement. And this where I'm confused, I understand how it would work if the entities also did tile based movements, but that is not how my game is set up. And I don't want to check every pixel on the screen for a path because that would take forever and be extremely inefficient.

Source Link
Loading