Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate if something lies in someone's field of vision

I have a subject that has a position in a 2D space and a velocity, both represented by a vector. The subject has a field of vision of 135 degree to each side. It looks in the same direction as it is moving (the velocity vector).

I have objects with a position in a 2D space represented by a vector.

In the drawing the objects on blue background are visible, the objects on red background are not visible to the subject.

How can i calculate which objects lie in the subject's field of vision at a given moment?

Which objects lie in field of vision

like image 351
MartinW Avatar asked Mar 20 '23 03:03

MartinW


1 Answers

You just need to find the distance to the objects and the angle towards them. Then, check that the distance is not greater than the radius of that light blue circle, and the angle between the speed vector and the vector to the object is not too large.

The Euclidean distance is simply sqrt ((x2 - x1)^2 + (y2 - y1)^2) (link).

As for the angle, represent the vector (x2 - x1, y2 - y1) and the speed (vx, vy) in polar coordinate system, and then check that the absolute difference between the angles is no more than 135 degrees. In programming languages, there is often a convenient function atan2 (y, x) to find the polar angle of a single vector (link).


The polar angle of a point is measured from vector Ox in counterclockwise direction. Consider we have two points: endpoint of the velocity vector (vx, vy) (left pic) and endpoint of a vector from our object (x1, y1) to the object in question (x2, y2): it's the vector (x2 - x1, y2 - y1) (center pic). Let us say the polar angles are alpha and beta, respectively. Then the angle between (vx, vy) and (x2 - x1, y2 - y1) is the difference of these polar angles (right pic).

Figure 1Figure 2Figure 3

There's a trick however. The alpha and beta values you get are typically either from -PI to +PI, or from 0 to 2PI. So, the difference beta - alpha will be between -2PI and +2PI, and you'll want it be between -PI (180 degree clockwise from current direction) and +PI (180 degree counterclockwise from current direction). For that, some simple transformation will suffice, such as this pseudocode:

if angle > +PI:
    angle := angle - 2PI
if angle < -PI:
    angle := angle + 2PI
like image 162
Gassa Avatar answered Apr 26 '23 22:04

Gassa