Hi, welcome to this new video. This is going to be our third video within Project 1, which is our particle system simulation. This is a continuation of those two videos so, by all means, just start there, and go through those videos before as we're going to continue working our way through the particle system. We're going to be talking about a particle's lifespan. A lifespan refers to the duration of time by which a particle remains alive within a simulation. At a particular point, we might decide that that particle expired or died, and we want to remove it from the system. Maybe that particle will let's say, if you're recreating something like flames, maybe they have a certain radius of influence, and at a certain point, they decay and die. Well, that's something that we can certainly simulate. We really need to start thinking of a variable that we will call lifespan that will count up until a desired amount of a particular number of an arbitrary number that would be the desired lifespan of the particle. At that point, that particle would be removed from the system so we also need to look at how do we remove it from the computation of the collection. The great thing about considering lifespan is that we could actually continuously include more and more particles into our simulation and not having the risk of flattering the simulation so that it becomes really slow. If you actually keep creating particles, and not killing them in any way, you're going to end up with many many particles, and the simulation might start running very slow. But if you actually kill them over a few seconds, you might maintain a manageable simulation that maintains a certain number of particles that are manageable and can compute fast. Let's see how we can actually write all this within our simulation in processing. I'm going to continue working with the example that we have. This is written between the last two videos. We have our main tab where we created particles and we have our Particle tab. The first thing I would like to do is just let's go back to the idea we had been tinkering a little bit with the idea that the gravity could be in any arbitrary direction. This is what we should have. What I would like to start doing is changing the way in which we emit the particles. Let's just consider what would be a real world problem. Let's say we are creating 100 particles at the beginning of our script. But I would like to consider, what if I want to create particles, let's say that follow the mouse, and I would like to say, well, I want to do all this code, this four loop. I'm going to cut it, and I would like to do it here in Update. Every frame, I would like to make sure that I'm creating new particles. Those particles, they are x and y, it's going to be following the mouse position. Mouse x and mouse y. Everything remains the same. Let's say, we're going to do less, maybe 10 particles a frame, because this is actually going to be continuously building particles. Let's look at what we have. You can see that these works well. But if we keep doing this for too long, our simulation will start slowing down, especially if you have a number bigger than 10. This is because, in every frame we are including more and more particles, you can see that the particles remain, and the more time you spend doing computation, you're going to start lagging. This is where some of this technical know how would really help you not only optimize but be able to do a simulation that is sustainable, like dealing with the resources that you have available. Computational resources. We don't want the size of this particle list to be infinite, we want it to maintain a certain number that we can manage. We're going to use the lifespan as a way of maintaining the number of particles that we can manage. Let's start in our particle class. Including a variable called D is going to be our self.lifespan. Let's just create an arbitrary number for now. We could say 100. It's going to be 100 frames. It's going to be a representation in frames. It's a representation of time, but right now we're going to be using frames. We're going to do also a self.count=0. This variable, it's going to count upwards, once every frame. Once we reach a certain number, we will kill the particle. Let's write this function. Somewhere here below Compute forces. The function is going to be called Die. You can give it a different name if you feel like that's maybe too dramatic. Self. What are we doing here? Well, we're trying to have self.count increase the counter. The counter will increase one every frame. If elf.count variable reaches, sorry, that's self, not elf,if it reaches more or equal than the self.lifespan. The lifespan is the arbitrary number that we gave for the number of let's call it frames that the particle has available to leave. We will do something. Here, what we technically want to do is remove the particle How do we do that? Right now, we don't have a way for this particle to interact with the list that is containing it. This list here, all_particles, is the list that contains the particles. You see here, we're running only the particles that are within the list. If this particle is no longer in the list, we wouldn't compute it, we wouldn't execute it. Let's just pass. We need to figure out a way of passing this information off of the list itself to the particle so the particle can remove itself from that list. This is the way we're going to do it. In the constructor here of the particle, we're going to create a reference or here in the argument, we're going to say, give me a reference to the particle list, so the list of particles or other particles. Basically, it will include all particles, including the particle that we are currently evaluating. Let's just make that an internal variable for our system, so self.particle_list = particle_list. Now the particle should know that it's part of this collection. It has a reference to that full collection. So when we say remove particle, we could say specifically, from the list, self.particle_list.remove(self) because self is a variable that really tells us which is the current instance of the class. We're saying from the entire list, find yourself and remove yourself from the list. This is only going to happen once because once it happens, this particle is no longer going to be computed. It's not going to be evaluated into the run loop, and it's not going to be included in the simulation. In all effects, we might still have a memory footprint of this particle, which is something that we could address later. But at the moment, this should actually work by removing the particle from the system. Let's just call this die function now, self.die in our run. Remember that our run function is t. In a way, our index, it shows us all the different functions that we're executing. We will include that at the very end. This is a check. Have you spend more than your lifetime into the world. Let's see what we have. We're currently running into an error. Let's see what we're missing. We did change the constructor of the particle. When we change the constructor of the particle, the particle system now needs to take two arguments, not just one. To the particle, we are giving a position, but we also need to give a reference to the list here. Notice that we were initially only requiring to provide a position. Now this new version of a particle requires also a collection. It's becoming a little bit more specific to be a particle system that particles know that they're one out of many. You can see here it's working. We have our particles, but you can see how particles die. You would never go too far between the population of particles that you have. If the arbitrary number that we gave for the lifespan is too high, maybe you could do something like 40, and you'll see that the particles die much quicker. This is a way of controlling, like if you want this to be like, I don't know, some smoke that is emerging out of an engine or something like that, and it's associated to the position of a vehicle or something along those lines, you probably don't want them to go forever. The final thing is that you could also do a random. If you want to mix a little bit, like some particles have a certain amount, we actually want an integer here. But let's say we want some particles to live maybe 40, but others up to 100. There's some variance. Some particles live more than others, but they'll have a specific lifespan. You will create a little bit more of an organic feeling of some particles living, but most of them dying rather shortly. Again, any of these variables that we include, you can use any of the techniques that we have been covering so far to give it some expression to make it your own and to start doing design decisions with them. We're going to leave this video here, and I'll see you in the next one.