I am working on a game. I know the agents postion (x,y) and its orientation in range (-PI PI) and the position (x,y) of the object of interest. How can I tell if agent is looking at the object?
2 Answers
Take the vector from your agent to the target (tx - ax, ty - ay)
and find its orientation (usually atan2(ty - ay, tx - ax)
, but you didn't specify, see[1]). Then compare the two orientations.
If you have the facing direction of your player (which you can also compute from your orientation[1]) you can use the dot product:
c := dot(dir, normalized(t - a))
let a
be the position of your agent and t
the position of your target, then c
is the cosine of the inner angle between them.
[1] If your orientation is oriented counter-clockwise and the x-axis is oriented at 0, then the facing direction will be dir := (cos(angle), sin(angle))
.

- 4,243
- 19
- 38
You need some quantitative criterion to decide "looking at" or not. It can be expressed in terms of angle (less than 5° on either side) or distance (less than 50 cm on either side).
In the first case, compute the relative orientation of the agent and the point (by atan2(δy, δx)) and take the difference. In the second case, multiply the distance between the agent and the point by the tangent of the angle difference.
Beware that the differences must be computed modulo 360°, and differences larger than 90° must be rejected. Also make sure to use radians/degrees consistently.