Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,030 --> 00:00:02,670
Welcome to the Field of View example.
1
2
00:00:02,790 --> 00:00:09,540
If in the first sensor implementation was the range where it detected around the enemy, the field of
2
3
00:00:09,540 --> 00:00:16,920
view is actually helpful when we want to detect in front of the enemy specifically by a given angle.
3
4
00:00:16,950 --> 00:00:19,890
Let's give this scene a test and see how it looks.
4
5
00:00:19,950 --> 00:00:27,780
So if I press F6, then we end up with this and the field of view is 60 degrees, so something like
5
6
00:00:27,780 --> 00:00:28,110
this.
6
7
00:00:28,560 --> 00:00:35,040
So of course, right now I am in the field of view, but if I move out of the field of view, then it's
7
8
00:00:35,160 --> 00:00:35,700
false.
8
9
00:00:35,790 --> 00:00:40,830
So basically, this is what we want to achieve in this sensor.
9
10
00:00:40,950 --> 00:00:47,610
By the way, please keep in mind that if we use just the field of view, this will actually propagate
10
11
00:00:47,610 --> 00:00:48,400
infinitely.
11
12
00:00:48,420 --> 00:00:55,950
So basically, if we enlarge these segments to infinity and have the player here, it will be true for
12
13
00:00:55,950 --> 00:00:56,760
every distance.
13
14
00:00:56,820 --> 00:01:03,810
The solution for this is actually to combine both range, because range will give the list of the objects
14
15
00:01:03,810 --> 00:01:09,810
around in a specific distance and then the field of view which will limit it from the front view.
15
16
00:01:09,870 --> 00:01:13,920
Please note that this is really important if you want to use this field of view implementation.
16
17
00:01:13,920 --> 00:01:15,850
But now let's look at the code.
17
18
00:01:15,870 --> 00:01:19,710
First we have the AI, which has the field of view detector this time.
18
19
00:01:20,370 --> 00:01:25,350
And the terrain, the target, the camera and the control are similar to the previous range scene.
19
20
00:01:25,350 --> 00:01:29,680
Let's quickly give this a look because it might be interesting.
20
21
00:01:29,700 --> 00:01:36,240
The main change here is actually the fact that the field of view detector does not use a list, but
21
22
00:01:36,240 --> 00:01:37,800
basically is a query.
22
23
00:01:37,830 --> 00:01:41,210
Is the target inside of field of view, yes or not.
23
24
00:01:41,250 --> 00:01:44,790
So if it is, then it returns true, not false.
24
25
00:01:45,180 --> 00:01:47,460
The target being the target given here.
25
26
00:01:47,640 --> 00:01:51,360
So let's see now what does the field of view detector look like?
26
27
00:01:51,390 --> 00:01:53,810
So I'm going to open this, by the way, it's a new scene.
27
28
00:01:53,820 --> 00:01:56,760
So in the field of view detector, there is just the field of view.
28
29
00:01:57,000 --> 00:02:03,660
So I'm going to head into the script and here the code is quite simple, but there is a little math,
29
30
00:02:03,810 --> 00:02:05,630
especially vector math involved.
30
31
00:02:05,640 --> 00:02:10,530
But don't worry, we'll quickly check it and look into it to see how it's done.
31
32
00:02:11,010 --> 00:02:14,280
So of course we need to specify the field of view angle.
32
33
00:02:14,340 --> 00:02:16,890
This is really important and it's 60 degrees.
33
34
00:02:16,950 --> 00:02:20,430
By the way, please note that Godot uses radians, not degrees.
34
35
00:02:20,430 --> 00:02:25,140
So we need to be careful to convert them to radians when using them.
35
36
00:02:25,290 --> 00:02:27,840
So let's quickly discuss what we have here.
36
37
00:02:27,850 --> 00:02:31,920
First is the enemy position, which is the red circle here.
37
38
00:02:32,780 --> 00:02:39,290
We have the direction which is the forward of the enemy, which is here. Please note that this direction
38
39
00:02:39,290 --> 00:02:39,830
is normalized.
39
40
00:02:39,830 --> 00:02:41,540
It's of length 1.
40
41
00:02:42,110 --> 00:02:48,020
And the target position, which is the target position point, which is this green one and if we run
41
42
00:02:48,020 --> 00:02:53,780
is inside cone, then we need to compute the difference vector, which is the
42
43
00:02:55,250 --> 00:03:02,270
difference between the target point and the enemy position, which is this vector, but it needs to
43
44
00:03:02,270 --> 00:03:07,550
be normalized and normalizing this will mean that we need to put this length to 1.
44
45
00:03:07,550 --> 00:03:09,650
So in this case it will be the small one.
45
46
00:03:10,010 --> 00:03:13,670
Please note that the direction is the same though the origin is the same.
46
47
00:03:13,670 --> 00:03:16,340
I just put it parallel so you can see the small one.
47
48
00:03:16,430 --> 00:03:23,750
Basically this is the same is just normalized and then we need to compute the dot product of these two
48
49
00:03:23,750 --> 00:03:27,050
vectors and then compare it to this cosine.
49
50
00:03:27,050 --> 00:03:34,310
What this means is that if we do the dot product between the two vectors and stating by the Godot
50
51
00:03:34,370 --> 00:03:35,270
documentation.
51
52
00:03:35,870 --> 00:03:44,540
So basically if the angle between them is straight, so that means 90 degrees, then it'll be 0.
52
53
00:03:44,540 --> 00:03:50,240
If it is greater than 0, then the angle is narrower than 90 degrees and if it's more than 0,
53
54
00:03:50,270 --> 00:03:52,970
then the angle will be wider than 90 degrees.
54
55
00:03:53,180 --> 00:04:00,950
But what this means for us is that it will give a value between 0 and 1, and then we can compare
55
56
00:04:00,950 --> 00:04:02,540
it, of course, not directly.
56
57
00:04:02,540 --> 00:04:08,060
We need to compare it to the cosine this of the actual field of view, but it needs to be divided.
57
58
00:04:08,570 --> 00:04:13,880
And of course, as I mentioned, this is specified in degrees and you need to have it in radians.
58
59
00:04:13,880 --> 00:04:19,670
So we need to convert it and then we apply cosine on this. If it's done right
59
60
00:04:19,670 --> 00:04:25,460
let me quickly print this out because it will be much more clear how this works.
60
61
00:04:26,590 --> 00:04:28,540
putting the difference vector.
61
62
00:04:31,910 --> 00:04:37,480
Here, as you can see, it's almost 1 when they're perpendicular.
62
63
00:04:37,500 --> 00:04:41,990
That means the angle between them is basically the same.
63
64
00:04:42,000 --> 00:04:48,920
But if the angle gets changed, then this value is actually the cosine is between these two angles.
64
65
00:04:49,610 --> 00:04:56,390
So as I can move and if I go on the other side is the back, the values will be with minus, but that
65
66
00:04:56,450 --> 00:04:57,710
is not important for us.
66
67
00:04:57,860 --> 00:04:59,950
The important part is the plus values.
67
68
00:04:59,960 --> 00:05:04,260
And I'm going to quickly also put the cosine for reference.
68
69
00:05:04,430 --> 00:05:07,390
So the cosine is for this is 0.86.
69
70
00:05:07,430 --> 00:05:09,350
So basically by using this
70
71
00:05:10,690 --> 00:05:11,890
field of view detector
71
72
00:05:11,980 --> 00:05:19,180
we can detect if the enemy has direct vision of a target in a specific line of sight.
72
73
00:05:19,210 --> 00:05:24,280
What happens, though, if there is an obstacle between the enemy and the player?
73
74
00:05:24,310 --> 00:05:27,340
Well, in this case, we need to use raycasts.
74
75
00:05:27,580 --> 00:05:31,180
Let's find out how to implement that in the next session.
7945
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.