A* Algorithm 2D Game Pathing issues

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-24 11:22:06

问题


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:


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:


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

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



来源:https://stackoverflow.com/questions/29749599/a-algorithm-2d-game-pathing-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!