Hi. Welcome to this new video. We're going to continue working on our particle system simulation, and we're going to start addressing a calculation that is going to become very handy. How do we calculate a closest object out of a collection? Let's imagine that we have our particles scattered in the world, but we have two forces to attractors or two forces with an arbitrary vector. The closest object calculation will allow a particle to know, just by comparing distances, which is the object that is closest to them. We're going to be using this technique obviously, in a particle simulation, you might have influence from two forces at once, but we really want to bring forward this kind of algorithm that is very useful to determine what is the closest object in a collection. Let's look at the algorithm itself. When we look at a particular particle, we will be evaluating A, the distances of all the forces around us, and we'll also identify what is the ID. If we look through all the forces around us, we would be storing the ID of each one of those forces. As you can see in the algorithm on the left, we will start with two variables that are going to be key to understand this calculation. We start with the closest distance value, and we will start with a very large number that would gradually be reduced as we find closer objects, and the closest ID, which also defaults to a number that we could identify as not the correct number. I'm going to use a number minus one, but as we find an entity, let's say a force, that is at a particular distance, that is smaller than this very large number, then that's going to become our closest object. If we keep repeating this process over and over again, we will eventually be determining which is the force that is the closest to us, and it's associated ID number so that we can actually use it in the future. Out of that equation, we're going to be creating a closest ID, which is going to be the ID of the force that will be closest to the particle. This is something that we might need to look further in the code. This is where we left off. Let's just do a quick recap. We have a particle systems with a singular force that we display in the screen. The first thing we want to do in this tutorial is convert our force to a collection. As we have all particles, I'm going to do all forces and it's also going to be an empty list. Here, let's just do two forces. We're going to copy paste this new force line, and it's going to be our second force. We are going to give it a position of maybe 1,000 by 400, and then we're going to work with a force of 10 and -40. We also will just for good measure, we're going to add this new force here. But both of these forces, we're going to add them to the all forces list. Remember, we append. The new force that we created and also the second force , that is number 2. This time, instead of executing just the display function of one of the forces, we can do a very similar look at what we do for the particles for f in all forces. For a force object within the all forces list, f.display. At this point, let's see if we are running into any errors. The only thing that we're actually doing, and you could have many more forces. The first thing we're doing is trying to work with collections because that gives us the flexibility of having a singular item, maybe two, but maybe many more. That's all good. The second step is to pass the information. The particle currently doesn't know that there is a collection of forces out there, so in order for a particle to be able to understand the world around it, and understand that there are, in fact, many forces, we are going to include the collection here. We're going to pass the collection force list. We're also going to say self.force_list = force_list. Once you do these changes to the constructor, you're going to run into an error unless you pass all forces to the particle argument. When we construct a particle, we provide in the position, all the particles. Let's just make sure that all forces list is done here in the particles. The particles have already a reference to the particle list, so we're going to do that for the force list. We've done this already for the particles, but we're going to make sure that we're also doing it for the forces self.force_list = force_list. Let's make sure that we didn't break anything here because we were for some mistake. I could confuse. I actually was doing changes to the force. We don't want to do changes to the force at this point. We're only changing the constructor of the particle. And in the particle, when we actually construct it, we give it the position, all the other particles, and all the forces. There we go. Now we are passing the information of the forces to the particles. That's great. Let's go into the particle, and let's create a function, maybe just after run here, and we're going to define our closest force. Again, this is a function that whenever you feel like you need one single point to find the closest point out of a collection, it's something that you could do. Let's create a bit of space. We need two arbitrary variables. We're going to call the closest distance. Again, the idea here is that we start with something very large, larger than any distance that could be calculated between our particles and a force, and the closest ID. It's important to know that in Python, the ID minus 1, it's a valid ID. It's an ID that looks from the backwards of the list. But still, it's an arbitrary number that I know that I'm usually not using negative numbers, so if I wanted to create an exception to make sure let's say that this algorithm doesn't find the number, we would end do that the closest ID is minus one. We could check against this default value. Again, you could use a different value, but I'm going to use a minus one for now. Let's do our loop. The main important thing here is that we need to loop, so each particle will loop through each force. So for in range. I'm going to use a range. I could look just directly through the list of forces, but I would like to have the iterator, the i variable. You could do this in multiple ways. You can look through the list and add an iterator variable, so len.self.force_list. We're looping through the list, or as long as that list is, we're creating a loop that is as long as that list is. The first thing that we want to calculate is the distance. How we calculate the distance? Well, we can use the subtraction vector between the particles position and the force position, and that vector's magnitude should be the distance. The subtraction is something that we will call the difference vector, so I'm going to call it dif for difference. The difference is between the self.position, so the position of the particle. Let's make a copy of that because we don't want to alter the position itself, we want to create a new vector that is a copy of that vector. Then we subtract. This is where the the vector operation start getting slightly longer. This is one vector. It's a copy of the position. We are subtracting the position of the force, which is self. and here we have to access, force_list i. Because we're looping through the force list, the force_list i it's the index that we're evaluating currently,.position. This is the position of the force. Then we can say distance to force, just to give it. A bit more clarity is the the difference vector diff.magnitude. The magnitude of that difference vector. If you go back to vector math, you'll realize that substraction between two points will give us a vector that represents in its magnitude the distance between those two points. Substraction is very linked to a distance calculation. That's great. We are evaluating in the loop the distance from one particle to the force. What do we want to say? We want to say if the distance to the force. Let's imagine that we encounter a value of 100. Is the distance to the force smaller than this really big number that we started with? The first iteration is most likely to be yes. We want it to be a yes, a true condition at the beginning. That's going to be true. Then what happens? Well, the first thing we say is then the closest distance is going to become that distance. We are saying, let's update that the closest distance is now that 100. But also, the closest ID, it's going to become that I, meaning that in this iteration of the loop, this object that we're evaluating this force is currently closer than our closest force, and its associated ID is I. If we keep repeating that, now we have 100. The next particle, let's say it's 88. Is it closer? Yes, then the closest distance is going to become 88, and the ID is associated ID. If we encounter a force that is at 120, that is not going to be updated. As we look through all the entities in the list, we would obtain the closest ID. Here it's important that we exit the loop. We go back all the way to the fore loop here. We could specify what is the closest force. The closest force it's going to be self.force list. From the list and the index is the index that we obtained. What we're actually looking is this item here is the index of the closest force available. That's great. We could at this point, just do a small stroke visualization of this. Say let's do a line from the position. We have self in the position in x and y. Then we should have closes force.position because force is a class that in turn has a position x and there we go. What we're doing here yet again is make sure we're seeing that. So we're drawing a red line. If you want it to be too thick, we can just add some transparency between the position of the particle and the position of this closest object. If we don't have any errors, which we might, let's just calculate this. Let's just put it down here. Right now, we're not doing anything with those objects. We're trying to find what would be our closest force. But let's just see. Here we go. We actually end up with a white line. But you see that half of the particles draw the line to the left force, and I'm going to just reduce a little bit of the visibility of that line. It's getting in the way of the rest of the drawing. We still want something that is, so you can see here maybe it's a little bit too transparent. But as you can see, the particles are telling us, Oh, this would be the closest force to me, and we're going to do an exercise when we apply forces of a singular force around us, and that force will be the closest force around us. Each particle would be influenced by this closest force. That's it for this one. It's an algorithm that initially might sound a little bit complicated but as you start really using it, you realize that it's an incredibly useful algorithm that you could use in many different occasions. I'll see you in the next video.