Best Way to do LOS for AI.

Hi there.

I’m designing an FPS AI that can process a list of enemies. ( uploaded a demo on WIP section )

Just want some opinions on what’s the best way you know for an AI to process Line of Sight in
such a way that it can detect new enemy that enter line of sight and exit. It should also be able constantly check if unit is still in Line of Sight.

Few methods I thought of.

Straightforward way:
Gets all enemy at a certain distance and do a test based on facing angle at every interval.
This should be expensive as the number of characters increases.

Mesh Way
Slap a huge cone with collision in front of the AI thta is attached to the head.
Detect and Filter characters on collision. I’ve no idea what’s the implication of performance like for this once. Please do
shed some light on this.

These are the only 2 method i’m stuck with. Either one is elegant enough.
Please do share some of your experiences

For most games it always boil down to a raytrace because you don’t want the AI to attempt to shoot through walls (or aggro against someone on the other side of a wall).

UE4 has a function in AIController.cpp



bool AAIController::LineOfSightTo(const AActor* Other, FVector ViewPoint, bool bAlternateChecks)


I saw your AI video in WIP (the cover based AI). They look very good

Also, for advanced AI behaviour have a look at the UPawnSensingComponent:

https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPawnSensingComponent/index.html

Works pretty neat :wink:

There are quite a few optimizations you can use. For one, you dont have to get ALL agents, especially if you have a current target. Most of the time you want to focus on firing at a specific target until the threat is neutralized or a new higher priority threat is detected. That rate of detection is kind of the key to optimizing, in that you don’t have to detect all new agents every frame. So in essence you can maintain your current target and then infrequently check for new ones, bearing in mind that if no new threats have entered the scene, then no new target selection is required.

Have a look at Damian Isla’s stuff from GDC about Halo’s AI (its called “building a better battle”).

For the most part you want to avoid physics checks wherever you can. So you might even want to communicate with your enemy target to coordinate the visibility checks. For instance an enemy could have an “I am behind cover” flag that you can check so that he is only targetted whilst out of cover. Turn that into an event and have your BT respond such that you get the event and start firing at him, rather than checking visibility every update.

Then there’s the classic task-list approach, where you do the LOS checks over multiple frames, with a queue of checks to be carried out and some frequency of update control (i.e. you can do 3 checks per frame, so you have to have all total agents doing less than 3 requests per frame to be able to service the queue).

Its a funky problem to play with. Have a look at Matthew Jack’s stuff from CryEngine too (his classification for cover selection and the like is interesting).

Wow… wow… that’s a lot of learning…thanks for the good share.

I am using LineOfSightTo, but the problem is, if I were to use LineOfSightToto detect allActor all the time ( to determine visibility ) , it’ll be expensive even if it’s alternate.
I need a way to sort ‘possibly visible’ actors and then cast only on those.

I was totally unaware of SensingComponent. I’ll look into how to use it. But I too am afraid of the low-level cost of such functionality, because I’m planning to use it large scale.
I was watching the Paris Shooter Symposium video , a talk from Crytek talks about using approximation to figure out if they need to re-cast, and Exponential Queue for LOS.
I figure these optimization are neccesary on Crysis2 because of the large scale AI ( about 20-30 or more at times ) . Which is in line with what I’m planning.

That’s why I am not sure if Epic created the Sensing component with these scales of battle in mind.

I saw your AI video in WIP (the cover based AI). They look very good

I’m not worried about contextual targeting at this point. But without a visibility method, I cannot constantly check about the OTHER AIs in the map.
I have no qualms with skipping LOS check on enemy in cover, but it’s the majority of the rest and during movement I’m worried with.

I’m trying to work a method to do the queue. But haven’t got around it yet.

Can you link me to the stuffs you were talking about. Halo AIm and the Crytek stuffs.

You could turn up the SensingInterval to something like a second. Means all the logic only runs once a second, not every frame. Gives you quite nice performance, at least works fine for me :wink:

Furthermore just turn it off, if the AI has a target.

For the Crytek stuff, you’ll have to search for Mikko Mononen’s talk at the Paris Conf. But specifically I was talking about the tactical points system that Matthew Jack did for CryEngine which you should be able to see here: http://docs.cryengine.com/display/SDKDOC4/AI

You might need a login to that, I can’t remember.

Point being, its more complicated than just a bunch of LOS checks if you want really interesting combat encounters. Oh while I’m at it, search for a talk by one of the Insomniac designers about combat encounters to see some stuff from a designer perspective (it was a GDC talk, but I’m sure I saw it on the Insomniac site).

thanks all for this, really helped a lot… tonnes of new reading material.