1
00:00:05,680 --> 00:00:07,678
Hi, welcome to this new video.

2
00:00:07,678 --> 00:00:12,100
We're going to continue working on project
1, which is a particle system, and

3
00:00:12,100 --> 00:00:14,046
we're going to be adding forces.

4
00:00:14,046 --> 00:00:19,190
So let's talk a little bit about forces
that we can apply to a particle, right?

5
00:00:19,190 --> 00:00:25,057
So it will happen that we might want to
control a particle in some particular way,

6
00:00:25,057 --> 00:00:30,487
and we will do that by including
certain forces, forces such as gravity,

7
00:00:30,487 --> 00:00:34,186
wind, turbulence,
drag and so forth, right.

8
00:00:34,186 --> 00:00:38,744
So these forces are external influences
that make the particles change, and

9
00:00:38,744 --> 00:00:42,234
they're mostly represented
as form of vectors, right.

10
00:00:42,234 --> 00:00:46,474
So the vectors will, in some cases,
might have a position, right.

11
00:00:46,474 --> 00:00:51,244
If you think of a fan, like it might
have a position, but in other cases,

12
00:00:51,244 --> 00:00:54,295
it would be just something
like gravity that is

13
00:00:54,295 --> 00:00:59,514
basically applying a force that is
a downward force at all times, right.

14
00:00:59,514 --> 00:01:04,154
So, let's just evaluate how we would
actually do the computation of forces.

15
00:01:04,154 --> 00:01:08,798
In order to do kind of a physics based
computation of forces, we're going to use

16
00:01:08,798 --> 00:01:13,786
three variables, we've been using two so
far, which is position and velocity.

17
00:01:13,786 --> 00:01:17,566
And this is a simple way of updating
the position of a particle, but

18
00:01:17,566 --> 00:01:19,556
we're going to add acceleration.

19
00:01:19,556 --> 00:01:22,822
And acceleration, if you really
go into what acceleration is,

20
00:01:22,822 --> 00:01:24,652
is the rate of change of velocity.

21
00:01:24,652 --> 00:01:28,212
So velocity is the rate of
change of position, right.

22
00:01:28,212 --> 00:01:32,385
So think of the particle is at any
time is moving with its velocity, but

23
00:01:32,385 --> 00:01:34,716
that velocity might change, right.

24
00:01:34,716 --> 00:01:37,154
It starts like,
maybe like you start getting drag and

25
00:01:37,154 --> 00:01:39,468
that velocity starts getting slower,
right.

26
00:01:39,468 --> 00:01:41,423
Or you get a gust of wind and you start,

27
00:01:41,423 --> 00:01:44,484
the velocity starts getting stronger,
right.

28
00:01:44,484 --> 00:01:49,634
The way in which velocity changes
is affected by the acceleration and

29
00:01:49,634 --> 00:01:55,164
the velocity, in turn, passes,
updates the position, right.

30
00:01:55,164 --> 00:01:57,716
So how do we do that in code?

31
00:01:57,716 --> 00:01:59,712
Well, we will need a velocity, sorry,

32
00:01:59,712 --> 00:02:02,468
an acceleration variable
that will also be a vector.

33
00:02:02,468 --> 00:02:05,588
A vector that we will refresh every frame,
right.

34
00:02:05,588 --> 00:02:09,844
We're going to be recalculating
that velocity vector every frame.

35
00:02:09,844 --> 00:02:13,822
But the velocity vector won't be
refreshed every time, every turn, right.

36
00:02:13,822 --> 00:02:17,248
We're going to keep that
velocity vector what it is, and

37
00:02:17,248 --> 00:02:22,774
either the acceleration will subtract or
add to that velocity, right.

38
00:02:22,774 --> 00:02:27,641
The important thing is that we do this
sequence of, we pass the acceleration,

39
00:02:27,641 --> 00:02:32,494
it's added to the velocity, the velocity
is added to the position, right.

40
00:02:32,494 --> 00:02:34,382
And then we reset the acceleration.

41
00:02:34,382 --> 00:02:38,450
Those are the third, the first three
lines of the update particle and

42
00:02:38,450 --> 00:02:43,076
then when we compute forces, we do any
force over the acceleration, right.

43
00:02:43,076 --> 00:02:48,658
So we might encounter we're close to
a proximity of, again, like a fan,

44
00:02:48,658 --> 00:02:53,521
and the particle might start
flying in a particular direction,

45
00:02:53,521 --> 00:02:58,356
or we just basically add gravity
to the acceleration, right.

46
00:02:58,356 --> 00:02:59,540
Or something like that.

47
00:02:59,540 --> 00:03:03,910
So let's see how that kind of plays
out when we start adding this

48
00:03:03,910 --> 00:03:05,908
information to our code.

49
00:03:05,908 --> 00:03:08,424
Let's jump into our script.

50
00:03:10,044 --> 00:03:14,684
So I'm here back at our script
where we left it from last video,

51
00:03:14,684 --> 00:03:18,764
if you haven't covered that video,
start there.

52
00:03:18,764 --> 00:03:23,224
So this is the second video
in the project 1 series.

53
00:03:24,564 --> 00:03:28,554
So let's see, we're going to be doing most
of our work, I mean, just as a recap,

54
00:03:28,554 --> 00:03:29,652
where we should be.

55
00:03:29,652 --> 00:03:32,241
It's like we have an emitter, right,

56
00:03:32,241 --> 00:03:35,384
spreading particles from the center,
right.

57
00:03:36,394 --> 00:03:41,444
And if we go into our particle tab,
we will see that our

58
00:03:41,444 --> 00:03:47,396
update calculation at the moment
is rather straightforward,

59
00:03:47,396 --> 00:03:51,154
it uses self position.addvelocity.

60
00:03:51,154 --> 00:03:54,554
So to the position we add the velocity.

61
00:03:54,554 --> 00:03:58,923
We need to do an extra step here, so

62
00:03:58,923 --> 00:04:02,388
we need to add a new vector,

63
00:04:02,388 --> 00:04:07,684
self.acceleration, so let's do ack.

64
00:04:10,544 --> 00:04:16,464
So this is going to be a new Pvector,
0,0, so it's going to be empty.

65
00:04:16,464 --> 00:04:20,404
So we're going to allow forces
to interact with this vector.

66
00:04:21,424 --> 00:04:24,717
And now in the update, we're going to say,

67
00:04:24,717 --> 00:04:29,474
let's just add the acceleration
to the velocity, right?

68
00:04:29,474 --> 00:04:33,927
So self.velocity.add

69
00:04:33,927 --> 00:04:40,234
self.acceleration, right?

70
00:04:40,234 --> 00:04:43,914
Right now the acceleration is zero, so
there shouldn't be any difference, right.

71
00:04:43,914 --> 00:04:49,272
We should be able to run this and
nothing should change,

72
00:04:49,272 --> 00:04:54,046
but we want to start
changing this acceleration.

73
00:04:54,046 --> 00:04:59,179
The other thing that I want to do,
as I mentioned before,

74
00:04:59,179 --> 00:05:04,640
we want to reset the acceleration
at the end of every frame,

75
00:05:04,640 --> 00:05:09,994
every update,
basically we calculate the new position,

76
00:05:09,994 --> 00:05:14,006
but then also we reset the acceleration.

77
00:05:14,006 --> 00:05:19,454
So the acceleration goes back to zero
after the update function, right.

78
00:05:19,454 --> 00:05:24,086
So what we're going to do is
include a new function and again,

79
00:05:24,086 --> 00:05:28,811
remember that we're kind of
trying to be modular in terms of,

80
00:05:28,811 --> 00:05:32,854
how we write our code and
so that we can maintain it.

81
00:05:32,854 --> 00:05:36,220
And often if we find mistakes,

82
00:05:36,220 --> 00:05:41,140
it will be just within
one little chunk of it,

83
00:05:41,140 --> 00:05:45,034
compute_forces, self, right.

84
00:05:46,194 --> 00:05:52,734
Here, what we're going to do is add,
so in the compute forces function,

85
00:05:52,734 --> 00:05:58,146
we're going to add one force
to the acceleration, right?

86
00:05:58,146 --> 00:06:05,362
So let's create a force here,
we could say g for gravity, right?

87
00:06:05,362 --> 00:06:07,734
Or we could even call it gravity.

88
00:06:08,954 --> 00:06:15,210
It would be a Pvector

89
00:06:15,210 --> 00:06:23,766
of 0,0.01, right.

90
00:06:23,766 --> 00:06:27,571
So I want to just give it a very small
push, remember that this is something that

91
00:06:27,571 --> 00:06:30,566
is going to be affecting
the velocity every frame, right?

92
00:06:30,566 --> 00:06:33,454
So we're going to put
a very small number here,

93
00:06:33,454 --> 00:06:37,006
it's going to be our kind of
synthetic gravity, right.

94
00:06:37,006 --> 00:06:40,592
Something that would represent
a form of like a downward motion,

95
00:06:40,592 --> 00:06:43,282
we know in processing
that the y-axis is down.

96
00:06:43,282 --> 00:06:46,431
So we're going to be assuming that the
particles will have gravity falling down,

97
00:06:46,431 --> 00:06:49,534
but you could actually create an arbitrary
gravity in any direction, right.

98
00:06:51,514 --> 00:06:58,357
So that's the gravity vector or yeah,
the gravity vector going down and

99
00:06:58,357 --> 00:07:04,654
let's just add that
self.acceleration.add gravity, right.

100
00:07:08,454 --> 00:07:12,411
So this is kind of,
we're using these compute forces here,

101
00:07:12,411 --> 00:07:14,606
this is kind of a global force.

102
00:07:14,606 --> 00:07:18,316
Later, we're going to see how to
create forces that it's a class and

103
00:07:18,316 --> 00:07:21,414
it can interact with our
particles in different ways.

104
00:07:21,414 --> 00:07:25,978
But right now we're just creating kind
of a general understanding of how forces

105
00:07:25,978 --> 00:07:28,034
would be computed into the system.

106
00:07:29,294 --> 00:07:32,254
So we have that, so
we have compute forces.

107
00:07:32,254 --> 00:07:36,500
The last thing we need is actually
to invoke this line of code.

108
00:07:36,500 --> 00:07:42,010
So our function computeforces
self.com put forces here,

109
00:07:42,010 --> 00:07:45,044
we'll do it at the beginning.

110
00:07:45,044 --> 00:07:48,100
So before the update,
it needs to be done before the update.

111
00:07:48,100 --> 00:07:53,329
If you remember any,
the update will actually delete or

112
00:07:53,329 --> 00:07:57,860
make sure that
the acceleration is zero here.

113
00:07:57,860 --> 00:07:59,628
So let's see what we get at this point.

114
00:07:59,628 --> 00:08:04,929
Let's see, so we have,
you see the same particles now go out,

115
00:08:04,929 --> 00:08:10,784
but they actually fall and
they actually fall based on this gravity.

116
00:08:10,784 --> 00:08:14,769
If you want this gravity to be stronger,

117
00:08:14,769 --> 00:08:20,171
you could see that the particles
fall weaker, right.

118
00:08:20,171 --> 00:08:25,891
So that's kind of neat,
we could actually start including,

119
00:08:25,891 --> 00:08:32,844
as I said, we could actually
include that 0.502 in the x-axis.

120
00:08:32,844 --> 00:08:38,284
And you would see that you can create
a force in this direction, right.

121
00:08:38,284 --> 00:08:42,196
And because we're starting the particles
with a velocity that is random, right.

122
00:08:42,196 --> 00:08:47,128
Some of them will start in an upward
direction, downwards, left and

123
00:08:47,128 --> 00:08:50,756
right, so
you get this kind of explosion effect.

124
00:08:50,756 --> 00:08:54,414
But if you want to manipulate
the initial velocity,

125
00:08:54,414 --> 00:08:57,634
you would go back to the this definition.

126
00:08:57,634 --> 00:09:00,906
How is the initial velocity being defined?

127
00:09:00,906 --> 00:09:03,930
If you don't want to see any
difference in the particles,

128
00:09:03,930 --> 00:09:07,242
you might just put a zero there and
the velocity to start with.

129
00:09:07,242 --> 00:09:12,356
And you will depend primarily on
the acceleration provided by the force,

130
00:09:12,356 --> 00:09:15,954
that's something that you can also try.

131
00:09:15,954 --> 00:09:18,645
But at this point, we have a good setup,

132
00:09:18,645 --> 00:09:23,076
we know how to apply forces to our
particles, so we can really start

133
00:09:23,076 --> 00:09:28,354
tinkering on how to create a system that
is more expressive and more complex.

134
00:09:28,354 --> 00:09:29,304
So I'll see you in the next video.