#### Archived

This topic is now archived and is closed to further replies.

# how to test if an enemy can see the player?

This topic is 5981 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi, i want to check whether enemies that have a certain position and that look in a certain direction can see the player. how can i implement this, i thought i could setup a frustum for each enemy and then test if the player´s position lies in it.. but that would be not preferrable i think. is there an easier way

##### Share on other sites
What sort of environment? There are different methods depending on that.

##### Share on other sites
your on the right track. saying this is a 3d environment, each enemy would have a position along with a direction. for most enemies, you would give them field of views, max distance they can see, minimum distance they can see, and from that you can construct a frustrum for the enemy's vision, then simply check if the player is in that space. if your including a world or environment around this, you might also want to cast a ray from the enemy to the player's bounding box vertex's (or a single one, which has the chance of error if the player is partially visible) so if there was anything that might occulde the enemy's sight of the player, they would still not be able to see the player.

hope this helped

a quick optimization would be to pre calculate the enemy's view frustrum, then simply rotate & translate it along with the enemy

'nother way would be to just do proximaty & ray checking. this would be ideal for immobile enemy's such as turrets.

Edited by - Rocket05 on February 5, 2002 7:51:46 PM

Edited by - Rocket05 on February 5, 2002 7:54:46 PM

##### Share on other sites
Yes. To put it another way... in a 2D environment, calculate the vector that represents the left-most side of the enemy''s field of vision. If the player is to the left of this line/plane (whichever way you look at it), then the player is invisible. Otherwise, calculate the vector for the right-most side of the field of vision. If the player is to the right of this line, he or she is invisible. If not... then you know the player is in the field of vision. You might want to use another line, perpendicular to the enemy''s facing direction, to determine if the player is within visible range. However you could just calculate the distance (use pythagoras) and class any distance beyond a chosen value as being out of sight. In a 3D environment, you could add 2 lines/planes for the vertical dimension too.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

##### Share on other sites
Cull the object space with a cone aligned with the facing of the enemy of the appro. field and depth. Perform an aggregate ray collision test between the enemy and the resultant set - a hit means the enemy sees it.

##### Share on other sites
knowing the position of the enemy and player you could cast a ray from the enemy to the player. Pass this ray through collision detection. if it doesn''t hit anything than there''s no obstructions. You then could the angle between the enemy''s straight ahead and ray to player with a dot product. If the angle is less that 1/2 enemy view width, then the player is in the view. Then just check the distance of the ray to make sure its not too far.

He who said money was the root of all evil knew little of the nature of money and less about the nature of man.

• 11
• 20
• 12
• 10
• 38
• ### Forum Statistics

• Total Topics
631401
• Total Posts
2999865
×