Targeting AI
While trying to program an AI system for the enemies in my game I ran into a bit of a problem with having the enemies target your player. I was planning on having the program use a raycast to see if an enemy can see your player. Obviously this is not the best idea for a couple of reasons. First, raycasting is slow and performing one on every frame for each character would be improbable. Second, a ray only goes in one direction, so the enemies would only see what is directly in front of them. There must be a good way to do this but I can't think of what to do. What should I use? Thanks.
first I would use distance, then I would calculate the angle between the direction the enemy is facing and the player. This will give you a nice field of view approach. from there you could cast a ray to see if there is an object between enemy and player. As far as raycasting being slow it is only slow if you are doing a few hundred thousand a frame. The original wolf3d, and doom engines were reaycasting engines they cast several thousand rays a frame on ancient hardware so depending on how many enemies you have it isn't slow enough to matter.
That's what I would use- distance. Find the distance between the player and the enemy. If that distance is equal to or less than your "MaximumAggroDistance" (const) variable, or somesuch, then your enemy will target the player. If not, they won't.
Then you can do the raycasting to determin if your enemy can "see" the player. This only has to be done once a target is within range. Also, you can limit how often this runs, as well. You don't have to run it every frame. You could easily add in a timing check to run the raycasting every X miliseconds instead of every frame. Limiting how often something runs can save quite a bit of CPU time- not everything has to be checked every frame.
Then you can do the raycasting to determin if your enemy can "see" the player. This only has to be done once a target is within range. Also, you can limit how often this runs, as well. You don't have to run it every frame. You could easily add in a timing check to run the raycasting every X miliseconds instead of every frame. Limiting how often something runs can save quite a bit of CPU time- not everything has to be checked every frame.
I would have to agree with the last bit about staggering them. Typical human reaction time to a visual stimulus tends to run around a half a second anyway so it's not that crazy to come around a corner and have someone not react for 500 milliseconds. Now, you shouldn't need to wait that long for the ray cast, but waiting a few frames isn't going to kill you.
Another thing is to have it strobe through multiple ray cast "feelers". Some places use 6, Christian Gyrling (Naughty Dog) said that on "Uncharted: Drake's Fortune" they used 20 and cycled through them (can't remember if he said randomly). That way you are checking various parts of the body - but not every frame.
Another thing is to have it strobe through multiple ray cast "feelers". Some places use 6, Christian Gyrling (Naughty Dog) said that on "Uncharted: Drake's Fortune" they used 20 and cycled through them (can't remember if he said randomly). That way you are checking various parts of the body - but not every frame.
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!"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement