#### Archived

This topic is now archived and is closed to further replies.

# simple math question

This topic is 6055 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''m starting to code my own ai system and was wondering how to find if an AI can see the player. Basically, I have the AI''s position, rotation and field of view as well as the players location and distance and need need to be able to find if the AI can see the player. I know this is going to need trig., but I''m not sure how. Any ideas. *Any* help is appreciated.

##### Share on other sites
I''m not a mathmatican but if the field of view is conic then check for a segment (polygon, bounding box) of the player in that cone. if the field of view is between two planes then check for the player in that area.

,Jay

##### Share on other sites
Ok. The ai only has to see in 2d. I was kinda looking for a more mathimatic way of determining weather the AI can see the player.

##### Share on other sites
raytracing is the answer. i can''t give you details without seeing your engine, but basically you "look" along many lines that radiate out from the enemy (either 360 degrees, or a "wedge" shape FOV), and if the lines hit the character before they hit a wall (or other obstacle) or "run out" (they can only see a certain distance, right?) then they can see you. the number of lines you have to check depends on if the game is tile-based or not.

--- krez (krezisback@aol.com)

##### Share on other sites
ok, I was kinda thinking about that, but isn''t there an easier solution, like a formula that I can use? The player and the AI are both points, so I don''t have to figure out if the player if only half visible.

Thanks, though
Kevin

##### Share on other sites
If you really want, you can check the distance of two points by:
sqrt( (x1 - x2) + (y1 - y2) )
it might be the two things inside squared, i can''t remember which anymore, too much stuff with conic sections cramed in my head!

||--------------------------||
Black Hole Productions
http://bhp.e-chrono.net/
Resident expert on stuff
max621@barrysworld.com
||--------------------------||

##### Share on other sites
Actually, in addition to the whole distance thing (which, incidentally can be done much more efficiently by defining the max as distance squared and forgoing the sqrt), you can find whether the person is within a given field of view by getting the dot product of two vectors:

-Direction in which the NPC is looking
-The vector between the player and the NPC

If both these vectors are then normalized, the dot product will then return the cosine of the angle between the two vectors, and if that value is within the boundaries of the field of view the NPC can then "see" the player. This does not take into account the possibility that there is an object in the way, but that''s a much bigger problem to do efficiently. If you need to explore that as well, I would suggest doing a Google serach on "quadtrees."

The wind shear alone from a pink golfball can take the head off a 90-pound midget from 300 yards.

-Six String Samurai

##### Share on other sites
Thanks a ton, Mordoch Bob. Thats excactally what I was looking for. Thanks again to all.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 12
• ### Forum Statistics

• Total Topics
632569
• Total Posts
3007121

×