AI Player Detection

Here’s a simple player detection algorithm.

  • 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 .5 and 1.0

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