AI Player Detection

Here’s a simple detection algorithm which determines if an enemy AI can see the player.

  • Take the normalized direction vector from the enemy to the player v1.
  • Take the normalized direction vector that the enemy is facing v2.
  v1 = VNormalize(VSubtract(PlayerPositionVector, EnemyPositionVector)) 
  v2 = VNormalize(VSubtract(EnemyLookAtVector, EnemyPositionVector)) 
  • cos(angle) = DotProduct(v1, v2)

The results will range from 1 to -1. If v1 and v2 are in the same direction (i.e. the player is directly in front of the enemy) the result will be 1. If v1 and v2 are in opposite directions (i.e. the player is directly behind the enemy) the return value will be -1. If the player is directly off to the side of the enemy, it will return 0.

So the enemy can see the player (within 45 degrees in your picture) if the value returned is between 0.7071 and 1.0. If you were using a 60 degree field of view, you would be looking for values between 0.5 and 1.0

 
tutorialsarticlesandexamples/simple_ai_for_player_detection.txt · Last modified: 2013/11/22 13:32