Game Development Community

npc ai line of sight[obstacle avoidance]

by Dan Pascal · in Torque X 2D · 12/24/2009 (11:58 am) · 9 replies

what's the perfered way of implimenting line of sight for an ai npc?
make an invisible object and shoot it out?

#1
12/24/2009 (3:42 pm)
Hm, good question. Are you looking for a cone of view? You could do a distance check between the player and the AI, then check the angle between them and see if it's within some pre-defined amount.

Although, I imagine this would yield some funny results when you are standing right next to the AI and slightly to the side. In this case, you may want to add a check for some auto-detect distance when the player is 'in-front' of the AI. So, if the player is in front of the AI and the distance in less than a small amount, then the AI can see.
#2
12/24/2009 (4:15 pm)
that's pretty good,
but what about when there's an object in the way?
#3
12/24/2009 (4:28 pm)
Good point, that would likely call for a ray cast, which can be slow.

Your idea of shooting out an object could work. Maybe after checking the angle and distance you could place a line object from the AI to the player, then next frame if there is a collision on the line that isn't the player or the AI, you can assume something is in the way. It might be best to always have that 'line' object connected to the AI and just alter it's size and collision when needed.

I'm just talking off the top of my head here so if there is anybody out there that has already implemented or knows a great algorithm for this please chime in :)
#4
12/28/2009 (12:02 pm)
the line object sounds good; a long and narrow blank scene object attached to the npc

i didn't think tx did the raycast thing
#5
01/26/2010 (11:43 am)
i've spent the last few days trying to impliment line of sight...
the "thinking" behind it gets exponentially complex and rips my simplistic/elegant code into shreds.

oh, i've got ai, like patrol/chase/return etc,
simple to implement.. a joy almost

but line of sight, whoa
you need to set up goals and sub goals, or something similar
too much

#6
01/26/2010 (12:23 pm)
line of sight:
-get facing direction of npc.
-define field of view for npc.
-define max distance npc can see.
-check if player is close enough for npc to see.
-get angle from npc to player.
-if angle from npc is within npc facing direction (+/- half of FOV), then npc can see player.
#7
01/26/2010 (12:30 pm)
Maybe you're overcomplicating it? The 'colliding line' idea sounds like it would work fine - just update a line-of-sight variable in the AI each game tick.
#8
01/26/2010 (10:20 pm)
SORRY:
I was meaning obstacle avoidance
the line of site is not too compicated

obstacle avoidance after the line of sight detection

that requires the goals and sub goals... i would think
#9
01/26/2010 (10:37 pm)
If your environment is static then that helps simplify things alot. You can build a map for the AI to use - might be as simple as some nodes they can move between (so to get from A to B they just plot a path across the nodes).

If obstacles in your scenery are moving about (i.e. not just doors opening/closing and such like, which is in a sense still kind of static) then it's a wee bit more complicated certainly as the AI will need the ability to navigate around arbitrary local obstacles.