14
\$\begingroup\$

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.

\$\endgroup\$
  • 11
    \$\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 Nov 20 '14 at 20:54
  • \$\begingroup\$ most simplistic answer award goes to... \$\endgroup\$ – OganM Nov 20 '14 at 23:33
  • 3
    \$\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\$ – R.. Nov 21 '14 at 5:13
  • 5
    \$\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 Nov 21 '14 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\$ – R.. Nov 21 '14 at 14:22
17
\$\begingroup\$

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.

https://www.youtube.com/watch?v=Q9FZllr6-wY

\$\endgroup\$
  • \$\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 Nov 20 '14 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 Nov 21 '14 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 Nov 21 '14 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.