﻿1
00:00:04,390 --> 00:00:05,410
‫Hello and welcome.

2
00:00:05,420 --> 00:00:11,230
‫In this lecture we're going to be introducing member functions and this get safe and normal function

3
00:00:11,230 --> 00:00:17,680
‫that will allow us to get hold of the direction of a particular vector.

4
00:00:17,680 --> 00:00:19,900
‫Let's dive in and see how this works.

5
00:00:20,690 --> 00:00:26,480
‫So our simple method for sending the platform back in the opposite direction is working at the speeds

6
00:00:26,480 --> 00:00:28,200
‫we've tested it out at.

7
00:00:28,220 --> 00:00:34,130
‫But what happens if I say, Hey, let's make this platform go ten times faster?

8
00:00:34,160 --> 00:00:36,570
‫Let's go ahead and play and see what happens.

9
00:00:36,590 --> 00:00:42,410
‫Well, suddenly that platforms jump forward quite a lot and it's not hovering around the same location

10
00:00:42,410 --> 00:00:43,710
‫as it was before.

11
00:00:43,730 --> 00:00:53,330
‫And the reason for this is because the current location, when we set it as the start location, isn't

12
00:00:53,330 --> 00:00:58,910
‫necessarily exactly the distance moved, we could have overshot.

13
00:00:58,910 --> 00:01:01,490
‫And that is captured here in the if statement.

14
00:01:01,490 --> 00:01:06,890
‫We're saying that the distance moved is greater than the move distance, which means that we will have

15
00:01:06,890 --> 00:01:07,310
‫overshot.

16
00:01:07,310 --> 00:01:11,680
‫We have to have overshot in order to have triggered this if statement.

17
00:01:11,690 --> 00:01:17,840
‫But the question is by how much, and that's going to depend very much on how large the Delta time is.

18
00:01:17,840 --> 00:01:23,690
‫We can overshoot by quite large amounts, especially if the platform velocity is large, which means

19
00:01:23,690 --> 00:01:26,990
‫that our start location may drift over time.

20
00:01:26,990 --> 00:01:28,550
‫It may not stay constant.

21
00:01:28,550 --> 00:01:35,570
‫So the better way to do this would be to calculate how far we need to move from the original start location

22
00:01:35,570 --> 00:01:39,770
‫to get to the new one using the exact move distance.

23
00:01:39,770 --> 00:01:43,040
‫So let's try and visualize this back and forth calculation.

24
00:01:43,040 --> 00:01:46,430
‫We're saying we've got a platform at its start location over here.

25
00:01:46,520 --> 00:01:53,090
‫We say it's got a velocity which we can represent with an arrow that is a direction and a size, and

26
00:01:53,090 --> 00:01:55,840
‫it's also got a distance that we want it to move.

27
00:01:55,850 --> 00:01:58,160
‫We'll just represent it by this brace here.

28
00:01:58,700 --> 00:02:05,720
‫Now, what we're saying is we would like to figure out the exact point where the platform would be at

29
00:02:05,720 --> 00:02:09,960
‫the end, not overshot, but just exactly the end location.

30
00:02:09,980 --> 00:02:14,960
‫Now, the way we can do this is by taking the direction of that velocity arrow.

31
00:02:14,960 --> 00:02:21,380
‫So in vector parlance, this is called the normal of the arrow, and it basically takes it down to being

32
00:02:21,380 --> 00:02:22,610
‫of size one.

33
00:02:22,610 --> 00:02:25,970
‫So we get the direction but a size of one.

34
00:02:25,970 --> 00:02:31,700
‫So that allows us to know this count, essentially the size of the velocity, how fast we're going.

35
00:02:32,090 --> 00:02:39,080
‫And then what we can do is we can multiply that direction by the quantity of our distance to give us

36
00:02:39,080 --> 00:02:45,230
‫an arrow that gives us the whole movement from the start location right to the end location.

37
00:02:45,230 --> 00:02:46,310
‫Exactly.

38
00:02:46,310 --> 00:02:49,760
‫So let's have a look at how we can do this in C++.

39
00:02:49,760 --> 00:02:52,460
‫What we're looking for is a new start location.

40
00:02:52,460 --> 00:02:58,670
‫So let's remove how we're currently doing this and we're going to have a start location equal to, well,

41
00:02:58,670 --> 00:02:59,780
‫what is it going to be equal to?

42
00:02:59,780 --> 00:03:05,150
‫Well, it's going to be equal to, first of all, the start location plus something, because we're

43
00:03:05,150 --> 00:03:10,850
‫starting from here and we're adding this green vector to get to the end.

44
00:03:10,850 --> 00:03:13,250
‫So what's that green vector there?

45
00:03:13,670 --> 00:03:18,770
‫Well, that's where we need to get the normal of the platform velocity.

46
00:03:18,770 --> 00:03:24,740
‫And just to make it easier for us to understand, I'm going to split that into its own variable.

47
00:03:24,740 --> 00:03:32,630
‫So we're going to create a new variable of type F vector, which I am going to call the move direction,

48
00:03:32,630 --> 00:03:38,060
‫just to make it super clear what it is that we're trying to store in it and then we're going to get

49
00:03:38,060 --> 00:03:47,780
‫this from the platform velocity dot, get safe, normal and then some parentheses around the end.

50
00:03:47,780 --> 00:03:48,950
‫So what's going on here?

51
00:03:48,950 --> 00:03:50,660
‫We haven't seen this before.

52
00:03:50,660 --> 00:03:54,770
‫We've used Dot to get hold of the components of the vector, the X, Y and Z.

53
00:03:54,770 --> 00:03:55,970
‫Those were variables.

54
00:03:56,060 --> 00:04:04,580
‫But in C++ and in object oriented programming in general, we also have functions that exist on classes,

55
00:04:04,580 --> 00:04:08,510
‫so we can use the DOT operator to get hold of one of those.

56
00:04:08,510 --> 00:04:13,250
‫We've used the colon colon when we've been trying to get it out of the class itself.

57
00:04:13,250 --> 00:04:20,480
‫But when we want to do an operation with the actual vector itself, so platform velocities vector,

58
00:04:20,480 --> 00:04:25,520
‫we can use the dot operator to get the normal of that vector.

59
00:04:25,520 --> 00:04:30,830
‫So that's what we're doing here, using a member function instead of a member variable.

60
00:04:31,070 --> 00:04:36,770
‫So we've got the safe normal of that platform velocity and we'll put semicolon at the end of the line

61
00:04:36,770 --> 00:04:41,360
‫because here we've got a variable that's essentially we're saying that's the move direction.

62
00:04:41,360 --> 00:04:50,330
‫So now we can use that in the subsequent line to do a move direction multiplied by the move distance

63
00:04:50,330 --> 00:04:56,870
‫that's configured because we want to move exactly to the end, not overshoot.

64
00:04:56,870 --> 00:05:01,850
‫And then after we've done that, we should actually move our platform to the end location.

65
00:05:01,850 --> 00:05:08,630
‫So we're going to do a set actor location to be the start location, because otherwise our platform

66
00:05:08,630 --> 00:05:14,780
‫will have overshot and gone past that end location because we'll have updated it with the current location,

67
00:05:14,780 --> 00:05:16,460
‫which is the overshot location.

68
00:05:16,700 --> 00:05:20,000
‫So that allows us to move exactly to the end point.

69
00:05:20,050 --> 00:05:23,710
‫And then start from that new starting location.

70
00:05:23,740 --> 00:05:29,020
‫Now it's important that we update the platform velocity after we do all of this because we're using

71
00:05:29,020 --> 00:05:31,420
‫it to get hold of the move direction.

72
00:05:31,420 --> 00:05:36,230
‫So I'm going to update the platform velocity after we set the actor location.

73
00:05:36,250 --> 00:05:40,690
‫Now let's go back into one reel and hit compile and see if this is fixed.

74
00:05:40,690 --> 00:05:46,690
‫The problem with our platform moving backwards and forwards, let's close down this terminal and hit

75
00:05:46,690 --> 00:05:47,230
‫play.

76
00:05:47,230 --> 00:05:53,800
‫And now if we have a look, the platform is moving quickly, but it is moving reliably around the start

77
00:05:53,800 --> 00:05:56,020
‫and end location we would expect.

78
00:05:56,020 --> 00:05:58,300
‫So that's all I wanted to cover in this lecture.

79
00:05:58,300 --> 00:06:03,880
‫We just corrected a little bit of a bug that could have cropped up for some of our platform configurations.

80
00:06:03,880 --> 00:06:05,380
‫I'll see you in the next one.

