# 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.

• The player has free movement but he is always inside some tile(s). Use A* to get a path to the tile where the player is (or most of it is), and move to each tile using your current approach (except that collision detection is not necessary), but having a tile as a target (you may use its center coordinates). If the enemy is already in the same tile, simply use your current approach. – rcpinto Jul 3 '16 at 23:46
• Wikipedia has a great rundown of how to implement A*, the only modification I made was using a Node Object, instead of storing the weights in Maps. – Shaun Wild Jul 4 '16 at 11:53
• policyalmanac.org/games/aStarTutorial.htm This one helped me. Don't hard code A* into your characters logic but rather let them or some AI/System request a path to follow. – Sidar Jun 14 '17 at 1:03
• If you use A* for every bot, your performance will be much worse than to run Dijkstra just once out from the player to each bot. As you reach each bot it can immediately be assigined a next move, and the algorithm continues outwards until al bots have been assigned a next move. Since both algorithms actually run best backwards (ie from destination to source) this is a particularly fitting AND efficient approach. – Pieter Geerkens Jun 14 '17 at 2:34