How can I check if one game object can see another?

artificial-intelligence, visibility

Solution

Compute the angle between your view direction (understood as a vector) and the vector that starts at you and ends at the object. If it falls under FieldOfView/2, you can view the object.

That angle is:

arccos(scalarProduct(viewDirection, (object - you)) / (norm(viewDirection)*norm(object - you))).

Problem

I have an object, that is facing a particular direction with (for instance) a 45 degree field of view, and a limit view range. I have done all the initial checks (Quadtree node, and distance), but now I need to check if a particular object is within that view cone, (In this case to decide only to follow that object if we can see it). Apart from casting a ray for each degree from `Direction - (FieldOfView / 2)` to `Direction + (FieldOfView / 2)` (I am doing that at the moment and it is horrible), what is the best way to do this visibility check?

Original source