Subtitles section Play video Print subtitles [BELL DINGS] Hello, and welcome back to another video in chapter 2, the Nature of Code, about forces. So the last video, I looked at friction and modeling friction. And in this video I want to look at a drag force, which is quite similar to friction, but also different. Isn't that-- that's why I'm looking at it. But what I'm really doing here in these wrap-up videos to chapter 2 is doing some case studies. What are formulas that you might find in a textbook or Wikipedia that you just sort of feel like what do those mean, how do I even use that? Try to unpack those formulas and apply them in code. So the case study that this video will examine is drag-- drag force, something that's called air resistance. It's a kind of friction. And it's part of the field of study of fluid dynamics. And there's all sorts of interesting fluid simulation crazy stuff you could do, and there's lift and all sorts of-- there's like a lot you could get into. It's so crazy! But I'm going to boil things down and try to look at this particular formula for calculating a drag force. I brought that formula right here into the Nature of Code book, and this is what I'm going to use. So let's come over to the white board and replace the friction formula. This is the formula we want to implement, and the context we want to implement it in is a two-dimensional P5 canvas, where we have a body that's moving with a current velocity pointed down. Once again, we're calculating a force. So we need to both determine the direction of the force and the magnitude. Let's start with direction. Identical to friction is the direction of drag. We have the velocity unit vector and negative 1/2. So the drag force points in the opposite direction of velocity. So that's something we already know how to do. It's scaled with this negative 1/2 because, you know, science. But to us, in our P5 world, whether this is negative 1/2 or 5, it's going to be less important because this is made up units of measurement anyway. Then we have to start looking at other aspects of this. Let's go through these one at a time. So this Greek letter, RHO, stands for density. So what is this moving through? Well, if I were to take this marker and drop it, there would be air resistance. It's moving through a gas, the air. So what is the density of the air, versus if it landed in water, what's the density of the water, versus mud, or jello, or whatever kind of thing it's moving through. In our P5 world, assuming this circle, this body is moving through a kind of homogeneous uniform space, it's all the same stuff, the density of this air, or fluid, or whatever it is, is a constant. So RHO, the density, is a constant. Skipping v squared for a second, let's go to A. So A is surface area. So if I come back to the diagram in the Nature of Code book, we can see here the idea is what is the surface area of the object coming into contact with the fluid. And you can think of it like is it aerodynamic or not. Does it come like to a sharp point where there's very little surface area? Or is it kind of like a wide load, and there's a lot of surface area moving through this fluid? [GASP] While this is something I absolutely could try to model based on thinking about different shapes and different sizes of those shapes, I could also just consider that a constant, and maybe I'll just say all of the objects in my world come in with a surface area of 1. But I'm really just going to consider this to be a constant. So if I were to model it, it might make the ultimate simulation more dynamic and more realistic. But it's one thing that I think is a detail that I can mostly ignore, especially if I have a lot of circular bodies of relative similar size. Then we have the coefficient of drag itself. What's that? That's a constant. It's a constant. It's a constant that maps to the relative strength of the drag force itself. What I'm saying is, all of these three elements, with this negative 1/2, which is literally a constant, I can consider to be a constant in my simulation. So I can actually take this formula and simplify it greatly. Drag force is equal to negative 1 times some constant-- I'll call that the coefficient of drag. it's a constant that takes into account the negative 1/2, the surface area, the density, and the coefficient of drag-- times v squared, times the unit vector v. So once again, the direction is in the opposite direction of velocity. And it's scaled according to some constant times. And this one is really important. This is the speed squared. It's the magnitude of the velocity vector, and this is key. The faster the object is-- this was not the case with friction, not the case with kinetic friction. No matter how fast the object was moving, the friction force is not proportional to that speed. But in the case of drag. It's absolutely proportional. If I were to hold this marker absolutely still, it's not moving at all. There is no drag force on it. But if it's moving very, very fast, that drag force will be stronger. If it's moving slowly, it'll be weaker. And that's absolutely what I want to model. So I want the magnitude of this vector squared in our formula. I should also note that another way that you might see another notation for writing the magnitude of the vector is the name of the vector with two bars along each side. So you could also rewrite this formula like this. To demonstrate how to implement this, I'm taking an exact duplicate of the code I wrote in the previous video demonstrating friction. And all I've done here is I've renamed the Friction function, And I'm calling it Drag. What are elements-- is there anything here that I want to keep? Well, actually there is one thing that I want to keep, which is that I want to, when I'm getting the direction of the vector, I want negative 1 times the velocity unit vector. So this is what I want to keep. I want the direction now of the drag force. And I've got it here in this variable called drag. What's next? I need the magnitude of the drag force-- speed squared times the coefficient of drag. Well, let's make up a coefficient. Let's call it 0.1. The speed is this dot velocity dot mag. And then set the drag's magnitude to c times speed and apply the force. This is actually quite incorrect. Remember, it's not proportional to the speed. It's proportional to the speed squared, speed times speed. Guess what though? There's actually a function in P5 called Mag squared for magnitude squared. So it'll be a little bit more efficient if I just call this speed squared, and use the magnitude squared function. We can see that these objects that have less mass have a more difficult time accelerating. Let's see what happens if I take c and make it like a really high number, like 5. You can see all of these are really having trouble moving. They're just like slower. If I make it 500, can I get the force to be so strong that they don't move? Whoa. So one that I really have to watch out for, that force could become so strong it will actually push them back up in the opposite direction, which wouldn't actually happen. But again, with all of the various inaccuracies of things