3
votes

I have decided to pick up my old 2D game engine from back in the day and continue work on it This is my first attempt at trying to implement some basic AI for enemies and I am having some issues. Here is a run down on how I am treating the AI for enemies.

  • If the player is within a certain range of the enemy, the enemy moves towards the player by just checking to see if he is up or down, left or right of the player and adjusting it's Coordinates accordingly.

  • If the enemy hits an obstacle that is blocking him from moving in the direction of the player then I call My A* algorithm for detecting the shortest path to the player and moving around the obstacles.

  • I am checking to see if the enemy is no longer blocked every frame and if it is then calling the A* algorithm again to adjust for the moving position of the player.

The way I have implemented A* is I am checking adjacent squares based on the dimensions of the Enemy. So for example if I have a 60X60 Enemy I will be checking adjacent tiles within these dimension, also enemies can be different sizes. The problem I ran into is as follows:

enter image description here


Say the enemy is the black square. It checks it's adjacent tiles and can move to the square in front of it since it is not colliding with any objects in this square. Once it is in the top square it can fit through to the right square without a collision also. Now for this scenario:

enter image description here


Say the A* algorithm is called when the enemy is in the position of the black box. Now because there is not the size required for the enemy to move between the top wall there will be a collision and based on my algorithm this will cause the enemy to ignore this block.


So My question is, what would be the most common way around this issue. There is probably something silly I am missing but I thought I would ask. I hope I explained the problem I am having well enough, if you need any clarification just ask. Thanks in advance.

1
I personnaly didn't understand where is the problem, however, I think the way you are using the algorithm can lead to strange behave since you only use it when you hit an obstacle, imagine a vertical tube, the enemy is on top of it and he trying to reach the player who is under the tube, the enemy will then go down since he hasn't hit any obstacle yet, and when he reaches the bottom of the tube, which is an obstacle, the algorithm will then calculate the path which will be to go backwards (up the tube),but once he go up, there will be no obstacle, he will again do down. Tell me if i am wrongOthman Benchekroun

1 Answers

0
votes

I assume you are filling the A* by the enemy size sized blocks

that is wrong you need to feed by single cell/pixel what ever

So when filling fill the adjacent line (not whole box)

if not free the whole line then do not fill it at all

A* with block filling

you will need to encode some additional info to A* map like:

  • it is Horizontal,Vertical or both lines
  • if it is corner or box position

to know which way you can grow

[Notes]

A* filling should always match your movement capabilities