Building Logic for AI in Games – Part 2: Vision

In my previous article, Building Logic for AI in Games – Part 1: Simple Movement, I discussed how I made enemies follow certain paths in my HTML5 game, Infiltration. I then had to figure out how to make enemies see the player.

In order to check if an enemy sees the player, I first perform a simple distance check. Indeed, if an enemy is too far from the player, I consider that he shouldn’t be able to see him.

Then, I perform the angle check. I have two angles: the direction towards which the enemy looks, and the angle from the enemy directly to the player. Considering that the enemy has a 90° field of view, I have to check if the difference between these two angles is below or above 45°.

schema-coneCalculating the difference between two angles is more difficult than it sounds. It is more difficult than simply calculating the difference between two numbers because of the sign. This article explains it in further detail. Now that I am sure that the player is within sight of the enemy, and in its field of view, I have to check if there are no walls between them. This is the trickiest part.

Read more →