How does ai spot enemies

Started by
6 comments, last by leopardpm 12 years, 10 months ago
Hi,

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.



Thanks.
Advertisement
Do a line of sight check: raycast generally through your physics system. If it doesn't hit anything between the AI and the target, then the AI has potential line of sight. You can constrain that with cheaper tests like a vision cone, a sight radius, etc.


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.

-me
Ok. So the raycasting is like "pinging" the range or view it is looking at and if there is a unit and nothing blocks it, it will return a potential line of sight?

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.
Raycasting is how we do it.

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.

-me
Yeah as above, you traditionally use a raycast system. Pick a few points on the target's body (e.g. head, chest, knees) is if those points lie within the AI's view-cone, perform a ray-cast to determine if they're visible.
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.
Ok cool.

Do you have or is example code I can use? Basically to try it out?

And is there any other code, not GPU usage code, that is used for this? Or do all games use raycasting in general? Just to be sure.
And is there any other code, not GPU usage code, that is used for this? Or do all games use raycasting in general?

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.

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.


Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
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!"

If its a game in which everything is divided up into a grid or hexes or sectors, then a more efficient way to do the raycasting is just do a cast from the center of the character (agent in question) to each of the mobs within a given radius - no need to do a full LOS raycast as if you were rendering the scene in 3D. You could do additional casts if you wanted a more precise model (like if a mob is 3/4 obscured by a wall, the single cast would not show them as seen, but if you do 3 casts (leftside, center, rightside) then you will see a mob if any part of it sticks out from a wall).

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.

This topic is closed to new replies.

Advertisement