Alright, I'll try to be as concise as possible:
In 2D space, a vector is a combination of two numbers, usually x and y. It represents a direction or movement along the x and y axis. The length of the vector is the square root of the sum of the squares of x and y:
v.Length = Math.Sqrt( v.x^2 + v.y^2 );
To normalize a vector, both its x and y are divided by its length. The result is called a unit vector, its length is 1. Usually, directions are represented by unit or normalized vectors.
Vector (x;y) turned 90 degrees
- to the left: (-y;x)
- to the right: (y;-x)
180 degrees: (-x;-y)
On to the enemy's field of view:
Viewing direction (dx;dy) (normalized)
For my example, I'm going to use a field of view where A_B = E_L2. This is pretty similar to e.g. Commandos.
First we need to decide how long E_L2 is going to be. I'll say l = 100.
Given an angle a, the direction vector d is simply ( cos(a);sin(a) ).
So in order to get A and B, we need to move from E in direction d by 100, then 90 degrees from that by 50 to either side.
L2 = E + l*(dx;dy)
A = L2 + (l/2)*(-dy;dx)
B = L2 + (l/2)*(dy;-dx) Now that we have the three corners of the triangle, we can check whether the player is inside it.
To do that, we pick a side of the triangle, then check whether the player is on the same side as the third corner. Only if this is true for all three sides, the player is inside.
E.g., is the player P on the same side as E in respect to AB?I'm sure there's a proper, vector-based way to do this, maybe someone can help out here?
Until then, I'll calculate the function that produces the line going through A and B, then put px and ex in that and check if f(ex)-ey and f(px)-py have the same sign. The downside is that I have to check for the special case of ax = bx to avoid a division by zero error.ax != bx:
f = m*x + t
m = (by-ay)/(bx-ax) // m
ay = m*ax + t
t = ay - m*ax // t
r1 = m*ex + t - ey
r2 = m*px + t - pxax = bx:
r1 = ax-ex;
r2 = ax-px;
Do r1 and r2 have the same sign?
r1*r2 >= 0: yes
r1*r2 < 0: no
If you have problems with turning all that into code, I can help you out there, but atm I'm still curing my hangover