How does ai spot enemies
Members - Reputation: 99
Posted 18 April 2011 - 03:58 PM
I was wondering, and like as much information on it as possible, how does ai spot enemies in games, FPS for the most?
How does the general idea work, the code and mechanics?
Is it just by a set distance that the ai would automatically "see" you or spot you based on script or game engine (for the obvious reason it cannot actually "see", it has no physical eye)? Or does it generally use a "view cone" or anything such? And how do you combine those elements into a working functioning game or code part or engine part? How does it work down, what steps does it go through or cycle through in order to get the final result "it has spotted an enemy unit"? Something I can work with, that works. Something realistic.
Members - Reputation: 1315
Posted 18 April 2011 - 05:15 PM
Modern games can often incorporate other modeled senses like sound. You have obejcts that make sound which is modeled by a radius. Any AI within that radius will become aware of the sound maker. You can also have a few different vision cones (a long narrow far vision and a wide short near vision). Depending on other game mechanics it's also sometimes helpful to have a short-range proximity sense: i.e. if you're closer than a few meters to an AI, even if they can't "see" you then they will become aware of your position after a brief delay.
Members - Reputation: 99
Posted 18 April 2011 - 06:06 PM
How do games in general do it, outside of the raycasting as you mentioned? Is this the best way and most common way of doing it?
Is there some example code how to implement this into a game? Not just for raycasting but how ai spots enemies in general.
Members - Reputation: 1315
Posted 18 April 2011 - 06:14 PM
If you're unsure what raycasting is, it's basically a collision detection routine. You project a ray (line segment) into your physics world. You then collect every object that this ray intersects. You ignore the insitgator object (you don't want the trace hitting the AI doing the trace). So basically it's a series of "does this line intersect this collision geometry" checks. A good physics system optimizes this with spatial partitioning trees or whatever so you don't check the line against every object in the scene.
It is, as you might imagine, a very expensive operation. So you want to make sure you aren't evaluating LOS every frame. You'll have to play with the numbers for your actual game but in practice I find that AI doesn't really need to raytrace to it's targets more than 1-2 times a second. Generally visibility is stored in a "VisibilityManager" or some other meta object that does X number of raytraces per second and caches the results for the AI to look up whenever it needs to.
For more advanced behavior you want to make the AI remember the last place where it saw the target. That way, when LOS starts failing you have a position from which the AI can start some kind of search behavior.
Moderators - Reputation: 38652
Posted 18 April 2011 - 06:18 PM
To make it seem a bit more 'human' you can add lots of little tweaks, such as only triggering a "enemy spotted" event if the ray-casts continue to spot the target for several seconds, etc...
There are other techniques, such as using the GPU to rasterize a depth buffer from the AI's point-of-view and using occlusion queries for it's targets, or performing ray-casts against precomputed depth cube-maps, but that's pretty exotic stuff.
Moderators - Reputation: 2678
Posted 19 April 2011 - 04:40 PM
Dude... 3 people just told you that games use raycasting for this... why would you ask again? To answer your question about "other ways", though, any solution is going to involve 3D trig and the geometry of the level... something at which the GPU kinda has a pretty good handle on. Rolling your own solution would be sub-optimal in most cases.
And is there any other code, not GPU usage code, that is used for this? Or do all games use raycasting in general?
As for code, read the docs of whatever graphics library you are using. You will find a set of simple raycast calls -- very likely with examples of how to use it in these situations.
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play
"Reducing the world to mathematical equations!"
Members - Reputation: 158
Posted 27 May 2011 - 10:01 PM
The pseudo-code would be something like this:
for each mob within a given sight radius,
use the Bresenhan line drawing algorithm given the target mob x,y and the seeing mob x,y (Bresenham Wiki)
check each grid coordinate the line routine brings up to see if it obscures -> if yes, then unseen
loop until no more mobs in range
Even is your game is not grid based, it is generally much quicker to raycast only the mobs instead of the entire scene since there a generally alot less mobs then general area.
Hope this makes since.
and, of course, I could have misunderstood your question.
Bresenhams line drawing is my favorite and I am very familiar with how to cast both 2-D and 3-D scenes if you need additional help with the coding.