Given a player and an enemy with position, and given the direction vector of each, how can I tell if one can "see" the other?

In other words, how do I test if one position with direction is in front or behind another position with direction?

For my purposes, 'in front of' means directly in front of, or 90 degrees to either side.

  • 12
    \$\begingroup\$ Clearly the solution is to create another camera and re-render the scene from the enemy's point of view, then perform image recognition on the result for the player character. \$\endgroup\$
    – Riking
    Commented Nov 20, 2014 at 20:54
  • \$\begingroup\$ most simplistic answer award goes to... \$\endgroup\$
    – OganM
    Commented Nov 20, 2014 at 23:33
  • 4
    \$\begingroup\$ While Riking's comment was likely intended as a joke, this is the actual fair way to do it -- giving NPCs the same access to the game state that a player would have rather than allowing them to access the internal state of the game. \$\endgroup\$ Commented Nov 21, 2014 at 5:13
  • 6
    \$\begingroup\$ @R.. Yes, but where do you draw the line? Taking this reasoning to its logical endpoint, NPC's eventually become sentient with the ability to play and think like a human being... at which point they would probably rationally decide to begin wallhacking again anyway :p \$\endgroup\$
    – Thomas
    Commented Nov 21, 2014 at 10:51
  • \$\begingroup\$ @Thomas: In practice I think you can make some sort of compromise, approximating the level of information they would have as a player without going to the trouble of implementing it that way. Simple measures like correct testing for line of sight, limiting the ability to identify an object beyond a certain distance, etc. could go a long way. \$\endgroup\$ Commented Nov 21, 2014 at 14:22

1 Answer 1


The dot product of two vectors can tell you if they face each other or not. First vector can probably be the enemies view direction the second one should be a vector pointing from player's position to the enemies position.


  • \$\begingroup\$ Ah yeah, I was overthinking it. I was juggling 4 vectors, but really I want the vector from one player to the other. The video was very helpful, thanks. \$\endgroup\$
    – izb
    Commented Nov 20, 2014 at 19:29
  • \$\begingroup\$ One crucial detail I think should be added to this answer is the game should also do a raycast or something to ensure that they can in fact see the player (not blocked by walls etc.) \$\endgroup\$
    – T. Kiley
    Commented Nov 21, 2014 at 14:12
  • 1
    \$\begingroup\$ @T.Kiley: that has actually been suggested by Riking in the comment to the question. No, I don't think that that's a joke. If your game is 3D, raycasting is actually a fair way to do it. Just rerender the scene with all objects in black texture, from the point of view of the enemy, except the players and other important objects should be rendered in some other colors. Make sure to disable lighting and shading. If the resulting buffer is not entirely black, then you know that someone is in the line of sight of the enemy and you can use the color to identify what objects they are. \$\endgroup\$
    – Lie Ryan
    Commented Nov 21, 2014 at 15:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .