Login

Fancade Wiki

Updated Dot Product.md (markdown)

... ...
@@ -21,10 +21,11 @@ Moving on to another example involving a stealth game, there is a guard facing a
21 21
22 22
[[/uploads/Screenshot_20210106-131627_Fancade.jpg]]
23 23
24
The first input for the dot product is the direction that the guard is facing, the second input is the vector from the guard's position to the ninja's position. Then we compare the cosine of the angle formed by those two vector with the cosine of half of FOV:
24
The first input for the dot product is the direction that the guard is facing, the second input is the vector from the guard's position to the ninja's position. Then we compare the cosine of the angle formed by those two vector with the cosine of half the FOV:
25 25
26 26
[[/uploads/Screenshot_20210106-135840_Fancade.jpg]]
27 27
28 28
Let me explain the script:
29 29
- First we subtract the ninja's position by the guard's position (if it's the other way round, we get the vector from the ninja's position to the guard's position, that's the opposite of what we want). Then we normalize the vector before the dot product.
30
- For the guard's direction, we get the guard's current rotation and rotate the vector (0, 0, 1) by that rotation. This is only correct if we're assuming that the guard faces in that direction (0, 0, 1) if his current rotation is 0°, otherwise we use a different vector value. We don't have to normalize because we rotated the vector (0, 0, 1) which already has a length of one.
... ...
\ No newline at end of file
0
- For the guard's direction, we get the guard's current rotation and rotate the vector (0, 0, 1) by that rotation. This is only correct if we're assuming that the guard faces in that direction (0, 0, 1) if his current rotation is 0°, otherwise we use a different vector value. We don't have to normalize because we rotated the vector (0, 0, 1) which already has a length of one.
1
- Finally after we get the dot product (which should be the cosine of the angle formed by those two vectors), we can check if its less than the cosine of half the FOV (half because the direction of the guard is the sits between the two edges of the FOV in the middle, we would use 90° if the FOV is 180°). If its true, then the ninja is in sight, otherwise the ninja is still safe.
... ...
\ No newline at end of file
Fancade Wiki