Placeholder Image

Subtitles section Play video

  • [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

  • that I'm doing, I probably need to put some constraints

  • on this.

  • But with something like a coefficient

  • of drag of like 0.1, in this contact with that,

  • I'm getting realistic behavior.

  • But I really want to emphasize this in a more meaningful way.

  • So I think what I'm going to do is consider half of the canvas

  • to have like a thick liquid that has a strong drag to it,

  • drag coefficient, and the other half to be basically

  • a vacuum with no drag.

  • So let's go to the canvas and draw a rectangle

  • and say fill to 55 and then alpha at 50 a rectangle.

  • And we can make it even a little bit brighter.

  • OK.

  • So I want to draw on half of the canvas

  • this rectangle that shows as if these objects are falling

  • and land in water, or land in some liquid.

  • So in this case, I'll just say if move or dot position dot

  • y is greater than height divided by 2, move or dot drag.

  • So we should now see them all fall at the exact same rate.

  • But once they hit the water, the fluid resistance

  • will affect them differently.

  • Pause dot y.

  • Let's make that drag force even a little bit stronger.

  • Ooh, look at that.

  • See, that's the issue.

  • If it's too strong, you could see it's not very realistic.

  • It's like bouncing off of it, which

  • is kind of like a weird fun little like bug.

  • It's a feature, not a bug.

  • But obviously, it doesn't feel very realistic.

  • Maybe it might make sense for me to have this drag coefficient

  • be a global variable and actually perhaps

  • even it's something that gets passed in to the function

  • itself.

  • Let's make it 0.2.

  • So this wraps up this particular example

  • to demonstrating a drag force.

  • Here's some exercises of things you could try.

  • One is I'm not being very thoughtful

  • about how I'm considering the two dimensional

  • space that is the canvas.

  • I just kind of used height divided by 2

  • as this arbitrary marker between vacuum and liquid.

  • Maybe I would actually want to create

  • a liquid class, an object that describes

  • a density, a coefficient, and an area of the canvas

  • where that liquid or gas is present.

  • And then I could build a map of having different liquids

  • with different coefficients and different parts,

  • and have a much more dynamic system of things experiencing

  • different amounts of drag, depending on where they are.

  • And maybe it's all color-coded.

  • There's lots of possibilities there.

  • Another thing that I might consider

  • is thinking about surface area.

  • Here I'm assuming that all of the objects

  • have the same surface area, which isn't true.

  • And I can visually see that they don't.

  • And there's probably at least a very basic way

  • that I can consider the size and have

  • that be a factor in how I calculate

  • the magnitude of a drag force.

  • So that's definitely something I would suggest trying as well.

  • So let me know if you have questions

  • about this particular implementation.

  • Try to make your own version of it.

  • Think about just even the sort of visual design interaction

  • of the system, but also what kinds of adjustments could

  • you make to how the drag force behaves

  • and what types of results might you get from that.

  • And go to thecodingtrain.com where

  • you can share your versions of this particular example.

  • And I've got one more chapter 2 video

  • to make, where I'm going to look at the formula

  • for gravitational attraction, and look at orbiting bodies.

  • See you there.

  • [THEME MUSIC]

[BELL DINGS]

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it