# Sum of FoV enemies

so what I need is to calculate the sum of players' Field of view. Put simple, In a 2d space, I want to check how much they cover (in degrees).

So here are a couple of use cases to explain by example: Let's say I have two players (but can be scalable to more) with a FoV of 90°:

1. If they are opposite of each other, the result should be 180°.
2. If they are looking in the exact same direction, the result should be 90°.
3. If they are at 45° of each other, the result should be 135°.

As data, I have the List of players (list), the FoV (degrees), the Position of the players [x, y], the direction they are facing (degrees). I can't think of anything else the can be of use. (If there is some extra data that would help I will check if it is something that can be created).

BTW, I am using a custom engine, I think that is a quite important info. :)

Thank you in advance for anyone willing to help.

Degrees are a very unusual way to represent directions in game engines. If you really want to use angles, then it is usually more efficient to handle angles in radians. But many engines do not use angles internally. 2d rotations are often most efficiently represented with complex numbers or normalized 2d vectors while the most efficient way to encode a 3d rotation is often a quaternion. But I digress.

Assuming FOV1 is the field of view in degrees of the first player, FOV2 the field of view of the 2nd player and DIFFERENCE the difference in view direction in degree in the shortest direction, then the total field of view covered is either FOV1 plus FOV2 or FOV1 + DIFFERENCE, depending on which one is smaller. Or in pseudocode:

totalFOV = FOV1 + min(FOV2, abs(DIFFERENCE));


(the abs-function returns the absolute value, also known as the value without minus-sign. This is relevant if player2 looks to the left relative to player 1, because in that case DIFFERENCE would be a negative value).

If returns the correct result for all three of your test cases:

1. 90 + min(90, 180) = 90 + 90 = 180
2. 90 + min(90, 0) = 90 + 0 = 90
3. 90 + min(90, 45) = 90 + 45 = 135
• I see what you mean, that's quite interesting, I will check that out. I couldn't wrap my head around how to make it compatible with more than 2 players (3,4, ...?). And I can get vectors from the players, and I can normalize them too. – kishikaisei Feb 12 '19 at 20:02
• And I am curious as well on how to make it work when Player 1 and Player 2 have different FOV value? – kishikaisei Feb 13 '19 at 9:52
• @kishikaisei Different FOV values should be handable as long as the sum of the angles doesn't exceed 360°. Extending it to more players would be possible in the same way if you order their directions in ascending order and use the difference to the previous one, but there are a few edge cases which will require handling, like if the last one overlaps the first angle or if one wide angle completely overlaps a previous small one. If you have lots of players with lots of different angles, I would actually suggest a completely different solution. But that's too much to explain in a comment. – Philipp Feb 13 '19 at 11:51
• Well, I need to make the script modular in a way that it takes multiple players and multiple FOV (never even reaching 180° though). With your method, I could make a code that works perfectly in every use case for 2 players which is very helpful. But now I kind of am stuck with more players... – kishikaisei Feb 13 '19 at 11:58