1
00:00:04,120 --> 00:00:07,640
Hi, welcome to this new video.

2
00:00:07,640 --> 00:00:11,330
This is going to be our third
video within Project 1,

3
00:00:11,330 --> 00:00:15,060
which is our particle
system simulation.

4
00:00:15,060 --> 00:00:18,180
This is a continuation
of those two videos so,

5
00:00:18,180 --> 00:00:20,070
by all means, just start there,

6
00:00:20,070 --> 00:00:22,460
and go through those videos
before as we're going to

7
00:00:22,460 --> 00:00:26,165
continue working our way
through the particle system.

8
00:00:26,165 --> 00:00:30,880
We're going to be talking
about a particle's lifespan.

9
00:00:31,310 --> 00:00:36,360
A lifespan refers to

10
00:00:36,360 --> 00:00:39,090
the duration of time by which

11
00:00:39,090 --> 00:00:41,740
a particle remains alive
within a simulation.

12
00:00:41,740 --> 00:00:43,590
At a particular point,
we might decide that

13
00:00:43,590 --> 00:00:47,170
that particle expired or died,

14
00:00:47,170 --> 00:00:49,700
and we want to remove
it from the system.

15
00:00:49,700 --> 00:00:53,510
Maybe that particle
will let's say,

16
00:00:53,510 --> 00:00:56,215
if you're recreating
something like flames,

17
00:00:56,215 --> 00:00:59,270
maybe they have a certain
radius of influence,

18
00:00:59,270 --> 00:01:01,650
and at a certain point,
they decay and die.

19
00:01:01,650 --> 00:01:04,240
Well, that's something that
we can certainly simulate.

20
00:01:04,240 --> 00:01:05,910
We really need to
start thinking of

21
00:01:05,910 --> 00:01:09,170
a variable that we will
call lifespan that

22
00:01:09,170 --> 00:01:13,870
will count up until
a desired amount

23
00:01:13,870 --> 00:01:16,890
of a particular number of

24
00:01:16,890 --> 00:01:18,270
an arbitrary number
that would be

25
00:01:18,270 --> 00:01:20,290
the desired lifespan
of the particle.

26
00:01:20,290 --> 00:01:21,790
At that point, that
particle would be

27
00:01:21,790 --> 00:01:23,470
removed from the system
so we also need to

28
00:01:23,470 --> 00:01:25,650
look at how do we remove it

29
00:01:25,650 --> 00:01:28,820
from the computation
of the collection.

30
00:01:28,820 --> 00:01:30,790
The great thing
about considering

31
00:01:30,790 --> 00:01:32,610
lifespan is that
we could actually

32
00:01:32,610 --> 00:01:35,350
continuously include
more and more particles

33
00:01:35,350 --> 00:01:37,010
into our simulation and not

34
00:01:37,010 --> 00:01:39,050
having the risk of

35
00:01:39,050 --> 00:01:42,670
flattering the simulation so
that it becomes really slow.

36
00:01:42,670 --> 00:01:45,660
If you actually keep
creating particles,

37
00:01:45,660 --> 00:01:47,630
and not killing them in any way,

38
00:01:47,630 --> 00:01:49,170
you're going to end up
with many many particles,

39
00:01:49,170 --> 00:01:51,210
and the simulation might
start running very slow.

40
00:01:51,210 --> 00:01:53,670
But if you actually kill
them over a few seconds,

41
00:01:53,670 --> 00:01:57,630
you might maintain a
manageable simulation that

42
00:01:57,630 --> 00:01:59,490
maintains a certain
number of particles that

43
00:01:59,490 --> 00:02:02,520
are manageable and
can compute fast.

44
00:02:02,520 --> 00:02:05,010
Let's see how we can
actually write all this

45
00:02:05,010 --> 00:02:08,670
within our simulation
in processing.

46
00:02:09,410 --> 00:02:11,250
I'm going to continue

47
00:02:11,250 --> 00:02:13,070
working with the
example that we have.

48
00:02:13,070 --> 00:02:17,200
This is written between
the last two videos.

49
00:02:17,200 --> 00:02:19,210
We have our main tab where we

50
00:02:19,210 --> 00:02:22,890
created particles and we
have our Particle tab.

51
00:02:23,930 --> 00:02:26,490
The first thing I would
like to do is just

52
00:02:26,490 --> 00:02:29,410
let's go back to the
idea we had been

53
00:02:29,410 --> 00:02:31,590
tinkering a little bit
with the idea that

54
00:02:31,590 --> 00:02:38,000
the gravity could be in
any arbitrary direction.

55
00:02:38,000 --> 00:02:40,080
This is what we should have.

56
00:02:40,080 --> 00:02:42,770
What I would like
to start doing is

57
00:02:42,770 --> 00:02:45,700
changing the way in which
we emit the particles.

58
00:02:45,700 --> 00:02:49,470
Let's just consider what would
be a real world problem.

59
00:02:49,470 --> 00:02:50,715
Let's say we are creating

60
00:02:50,715 --> 00:02:55,800
100 particles at the
beginning of our script.

61
00:02:55,800 --> 00:02:57,695
But I would like to consider,

62
00:02:57,695 --> 00:02:59,230
what if I want to
create particles,

63
00:02:59,230 --> 00:03:01,325
let's say that follow the mouse,

64
00:03:01,325 --> 00:03:03,230
and I would like to say, well,

65
00:03:03,230 --> 00:03:08,390
I want to do all this
code, this four loop.

66
00:03:08,390 --> 00:03:10,150
I'm going to cut it, and I would

67
00:03:10,150 --> 00:03:12,100
like to do it here in Update.

68
00:03:12,100 --> 00:03:16,350
Every frame, I would

69
00:03:16,350 --> 00:03:20,520
like to make sure that I'm
creating new particles.

70
00:03:20,520 --> 00:03:22,790
Those particles,
they are x and y,

71
00:03:22,790 --> 00:03:26,290
it's going to be following
the mouse position.

72
00:03:28,520 --> 00:03:31,545
Mouse x and mouse y.

73
00:03:31,545 --> 00:03:33,250
Everything remains the same.

74
00:03:33,250 --> 00:03:34,730
Let's say, we're
going to do less,

75
00:03:34,730 --> 00:03:36,490
maybe 10 particles a frame,

76
00:03:36,490 --> 00:03:38,010
because this is
actually going to

77
00:03:38,010 --> 00:03:40,730
be continuously
building particles.

78
00:03:40,730 --> 00:03:43,415
Let's look at what we have.

79
00:03:43,415 --> 00:03:47,310
You can see that
these works well.

80
00:03:47,310 --> 00:03:50,370
But if we keep doing
this for too long,

81
00:03:50,370 --> 00:03:53,170
our simulation will
start slowing down,

82
00:03:53,170 --> 00:03:55,400
especially if you have a
number bigger than 10.

83
00:03:55,400 --> 00:03:57,770
This is because,
in every frame we

84
00:03:57,770 --> 00:04:00,130
are including more
and more particles,

85
00:04:00,130 --> 00:04:02,550
you can see that the
particles remain,

86
00:04:02,550 --> 00:04:05,265
and the more time you spend

87
00:04:05,265 --> 00:04:07,890
doing computation, you're
going to start lagging.

88
00:04:07,890 --> 00:04:10,650
This is where some of
this technical know how

89
00:04:10,650 --> 00:04:13,590
would really help you not only

90
00:04:13,590 --> 00:04:15,910
optimize but be able
to do a simulation

91
00:04:15,910 --> 00:04:19,140
that is sustainable,

92
00:04:19,140 --> 00:04:22,340
like dealing with the resources
that you have available.

93
00:04:22,340 --> 00:04:25,030
Computational resources.

94
00:04:25,030 --> 00:04:28,530
We don't want the size of

95
00:04:28,530 --> 00:04:31,800
this particle list
to be infinite,

96
00:04:31,800 --> 00:04:33,090
we want it to maintain

97
00:04:33,090 --> 00:04:35,010
a certain number
that we can manage.

98
00:04:35,010 --> 00:04:38,525
We're going to use the
lifespan as a way of

99
00:04:38,525 --> 00:04:45,190
maintaining the number of
particles that we can manage.

100
00:04:45,190 --> 00:04:47,410
Let's start in our
particle class.

101
00:04:47,410 --> 00:04:51,165
Including a variable called D

102
00:04:51,165 --> 00:04:56,830
is going to be our
self.lifespan.

103
00:04:59,560 --> 00:05:03,565
Let's just create an
arbitrary number for now.

104
00:05:03,565 --> 00:05:06,290
We could say 100.

105
00:05:06,290 --> 00:05:07,890
It's going to be 100 frames.

106
00:05:07,890 --> 00:05:09,850
It's going to be a
representation in frames.

107
00:05:09,850 --> 00:05:11,270
It's a representation of time,

108
00:05:11,270 --> 00:05:14,520
but right now we're going
to be using frames.

109
00:05:15,340 --> 00:05:19,700
We're going to do
also a self.count=0.

110
00:05:23,380 --> 00:05:25,490
This variable, it's going to

111
00:05:25,490 --> 00:05:29,685
count upwards, once every frame.

112
00:05:29,685 --> 00:05:33,825
Once we reach a certain number,

113
00:05:33,825 --> 00:05:35,880
we will kill the particle.

114
00:05:35,880 --> 00:05:39,735
Let's write this function.

115
00:05:39,735 --> 00:05:43,660
Somewhere here below
Compute forces.

116
00:05:44,960 --> 00:05:47,950
The function is going
to be called Die.

117
00:05:47,950 --> 00:05:50,080
You can give it a
different name if you feel

118
00:05:50,080 --> 00:05:52,675
like that's maybe too dramatic.

119
00:05:52,675 --> 00:05:58,100
Self. What are we doing here?

120
00:05:58,100 --> 00:05:59,795
Well, we're trying to have

121
00:05:59,795 --> 00:06:03,180
self.count increase the counter.

122
00:06:04,040 --> 00:06:08,505
The counter will increase
one every frame.

123
00:06:08,505 --> 00:06:17,780
If elf.count variable reaches,

124
00:06:19,790 --> 00:06:25,215
sorry, that's self,
not elf,if it

125
00:06:25,215 --> 00:06:33,630
reaches more or equal
than the self.lifespan.

126
00:06:33,630 --> 00:06:36,060
The lifespan is the
arbitrary number

127
00:06:36,060 --> 00:06:37,680
that we gave for the number

128
00:06:37,680 --> 00:06:40,170
of let's call it

129
00:06:40,170 --> 00:06:43,335
frames that the particle
has available to leave.

130
00:06:43,335 --> 00:06:46,020
We will do something.

131
00:06:46,020 --> 00:06:49,560
Here, what we technically
want to do is remove

132
00:06:49,560 --> 00:06:57,645
the particle How do we do that?

133
00:06:57,645 --> 00:07:01,260
Right now, we don't have a
way for this particle to

134
00:07:01,260 --> 00:07:06,010
interact with the list
that is containing it.

135
00:07:07,790 --> 00:07:10,260
This list here,

136
00:07:10,260 --> 00:07:13,410
all_particles, is the list
that contains the particles.

137
00:07:13,410 --> 00:07:15,630
You see here, we're running

138
00:07:15,630 --> 00:07:17,460
only the particles that
are within the list.

139
00:07:17,460 --> 00:07:20,350
If this particle is no
longer in the list,

140
00:07:20,690 --> 00:07:22,935
we wouldn't compute it,

141
00:07:22,935 --> 00:07:27,555
we wouldn't execute
it. Let's just pass.

142
00:07:27,555 --> 00:07:28,680
We need to figure out a way of

143
00:07:28,680 --> 00:07:30,870
passing this information off

144
00:07:30,870 --> 00:07:32,820
of the list itself to

145
00:07:32,820 --> 00:07:33,960
the particle so the particle can

146
00:07:33,960 --> 00:07:35,475
remove itself from that list.

147
00:07:35,475 --> 00:07:37,755
This is the way we're
going to do it.

148
00:07:37,755 --> 00:07:40,680
In the constructor
here of the particle,

149
00:07:40,680 --> 00:07:42,855
we're going to create

150
00:07:42,855 --> 00:07:46,380
a reference or here
in the argument,

151
00:07:46,380 --> 00:07:49,110
we're going to say, give

152
00:07:49,110 --> 00:07:52,095
me a reference to
the particle list,

153
00:07:52,095 --> 00:07:55,875
so the list of particles
or other particles.

154
00:07:55,875 --> 00:07:57,600
Basically, it will
include all particles,

155
00:07:57,600 --> 00:08:01,660
including the particle that
we are currently evaluating.

156
00:08:03,230 --> 00:08:06,045
Let's just make that

157
00:08:06,045 --> 00:08:07,830
an internal variable
for our system,

158
00:08:07,830 --> 00:08:16,920
so self.particle_list
= particle_list.

159
00:08:16,920 --> 00:08:19,710
Now the particle
should know that it's

160
00:08:19,710 --> 00:08:23,835
part of this collection.

161
00:08:23,835 --> 00:08:27,075
It has a reference to
that full collection.

162
00:08:27,075 --> 00:08:29,115
So when we say remove particle,

163
00:08:29,115 --> 00:08:34,030
we could say specifically,

164
00:08:36,530 --> 00:08:43,230
from the list,
self.particle_list.remove(self)

165
00:08:43,230 --> 00:08:46,950
because self is a variable
that really tells

166
00:08:46,950 --> 00:08:50,415
us which is the current
instance of the class.

167
00:08:50,415 --> 00:08:53,640
We're saying from
the entire list,

168
00:08:53,640 --> 00:08:56,700
find yourself and remove
yourself from the list.

169
00:08:56,700 --> 00:08:57,975
This is only going
to happen once

170
00:08:57,975 --> 00:08:59,565
because once it happens,

171
00:08:59,565 --> 00:09:02,550
this particle is no longer
going to be computed.

172
00:09:02,550 --> 00:09:06,555
It's not going to be
evaluated into the run loop,

173
00:09:06,555 --> 00:09:09,525
and it's not going to be
included in the simulation.

174
00:09:09,525 --> 00:09:12,990
In all effects, we might

175
00:09:12,990 --> 00:09:15,585
still have a memory
footprint of this particle,

176
00:09:15,585 --> 00:09:18,210
which is something that
we could address later.

177
00:09:18,210 --> 00:09:19,815
But at the moment,

178
00:09:19,815 --> 00:09:21,420
this should actually work by

179
00:09:21,420 --> 00:09:23,040
removing the particle
from the system.

180
00:09:23,040 --> 00:09:28,080
Let's just call this
die function now,

181
00:09:28,080 --> 00:09:31,305
self.die in our run.

182
00:09:31,305 --> 00:09:39,885
Remember that our run
function is t. In a way,

183
00:09:39,885 --> 00:09:42,360
our index, it

184
00:09:42,360 --> 00:09:43,890
shows us all the different

185
00:09:43,890 --> 00:09:45,495
functions that we're executing.

186
00:09:45,495 --> 00:09:49,290
We will include that at the
very end. This is a check.

187
00:09:49,290 --> 00:09:58,245
Have you spend more than your
lifetime into the world.

188
00:09:58,245 --> 00:09:59,955
Let's see what we have.

189
00:09:59,955 --> 00:10:02,565
We're currently
running into an error.

190
00:10:02,565 --> 00:10:05,740
Let's see what we're missing.

191
00:10:06,170 --> 00:10:11,080
We did change the
constructor of the particle.

192
00:10:11,780 --> 00:10:14,595
When we change the
constructor of the particle,

193
00:10:14,595 --> 00:10:15,990
the particle system now needs

194
00:10:15,990 --> 00:10:18,580
to take two arguments,
not just one.

195
00:10:19,730 --> 00:10:23,070
To the particle, we
are giving a position,

196
00:10:23,070 --> 00:10:28,500
but we also need to give a
reference to the list here.

197
00:10:28,500 --> 00:10:31,065
Notice that we were

198
00:10:31,065 --> 00:10:34,815
initially only requiring
to provide a position.

199
00:10:34,815 --> 00:10:36,300
Now this new version
of a particle

200
00:10:36,300 --> 00:10:38,130
requires also a collection.

201
00:10:38,130 --> 00:10:40,140
It's becoming a little bit more

202
00:10:40,140 --> 00:10:42,240
specific to be a particle system

203
00:10:42,240 --> 00:10:46,960
that particles know that
they're one out of many.

204
00:10:47,270 --> 00:10:50,805
You can see here it's working.

205
00:10:50,805 --> 00:10:54,360
We have our particles, but you
can see how particles die.

206
00:10:54,360 --> 00:10:58,470
You would never go too far

207
00:10:58,470 --> 00:11:03,450
between the population of
particles that you have.

208
00:11:03,450 --> 00:11:05,880
If the arbitrary number

209
00:11:05,880 --> 00:11:09,190
that we gave for the
lifespan is too high,

210
00:11:09,800 --> 00:11:13,660
maybe you could do
something like 40,

211
00:11:14,420 --> 00:11:18,525
and you'll see that the
particles die much quicker.

212
00:11:18,525 --> 00:11:22,260
This is a way of controlling,

213
00:11:22,260 --> 00:11:26,700
like if you want this to
be like, I don't know,

214
00:11:26,700 --> 00:11:28,515
some smoke that is

215
00:11:28,515 --> 00:11:31,125
emerging out of an engine
or something like that,

216
00:11:31,125 --> 00:11:32,925
and it's associated to

217
00:11:32,925 --> 00:11:34,050
the position of a vehicle

218
00:11:34,050 --> 00:11:35,340
or something along those lines,

219
00:11:35,340 --> 00:11:37,990
you probably don't want
them to go forever.

220
00:11:38,080 --> 00:11:42,720
The final thing is that you
could also do a random.

221
00:11:46,120 --> 00:11:49,280
If you want to mix a little bit,

222
00:11:49,280 --> 00:11:51,930
like some particles
have a certain amount,

223
00:11:52,780 --> 00:11:56,700
we actually want
an integer here.

224
00:11:57,950 --> 00:12:04,245
But let's say we want some
particles to live maybe 40,

225
00:12:04,245 --> 00:12:08,910
but others up to 100.
There's some variance.

226
00:12:08,910 --> 00:12:10,590
Some particles live
more than others,

227
00:12:10,590 --> 00:12:13,020
but they'll have a
specific lifespan.

228
00:12:13,020 --> 00:12:14,610
You will create a
little bit more of

229
00:12:14,610 --> 00:12:17,070
an organic feeling of
some particles living,

230
00:12:17,070 --> 00:12:20,100
but most of them
dying rather shortly.

231
00:12:20,100 --> 00:12:23,895
Again, any of these
variables that we include,

232
00:12:23,895 --> 00:12:26,340
you can use any of the
techniques that we have been

233
00:12:26,340 --> 00:12:28,515
covering so far to

234
00:12:28,515 --> 00:12:30,810
give it some expression to
make it your own and to

235
00:12:30,810 --> 00:12:33,435
start doing design
decisions with them.

236
00:12:33,435 --> 00:12:34,995
We're going to leave
this video here,

237
00:12:34,995 --> 00:12:37,600
and I'll see you
in the next one.