Archived

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

Gammastrahler

how to test if an enemy can see the player?

Recommended Posts

Gammastrahler    150
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 this post


Link to post
Share on other sites
Rocket05    152
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 this post


Link to post
Share on other sites
Kylotan    10011
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 this post


Link to post
Share on other sites
DanG    122
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.

Share this post


Link to post
Share on other sites