Circular obstacle field of view

 from Red Blob Games
02 Feb 2019
B inside of AE obscured by AE visible from B

Problem: we have a player at B. Does circle A obscure the line of sight to E?

To figure this out we can calculate the tangent points C and D. A-D-B and A-C-B are right triangles. We know the radius of circle A, so that tells us how long AD and AC are. We know the distance from obstacle A to player B, so that tells us the length of the hypotenuse AB. Trigonometry tells us sin(φ) = AD/AB. That means we can calculate φ = arcsin(AD/AB).

The next step is to figure out the actual angles. B→P is atan2(P.y-B.y, P.x-B.x), so we can figure out the angles to A, E. C's angle is A's minus φ. D's is A's plus φ. Then we need to check if E's angle is in between C's and D's.

Angles are tricky to work with and I haven't checked that this works in all cases.

Alternatively, this might be cheaper and less error prone with vector operations. We want to know that A→E is to the left of A→C and to the right of A→D. This can be calculated with the perp product[1]. There might even be a way to calculate C, D directly with vector operations but I haven't tried.

I reused the circular obstacle pathfinding[2] diagram code for this page.

Email me , or tweet @redblobgames, or comment: