If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content
Current time:0:00Total duration:4:37

Video transcript

in the last lesson we came up with a model that mimic the behavior of curly hair our model used a variety of Springs connected together with weights to give us this effect the goal of this lesson is to write the computer code powering the simulation one thing to consider before starting this lesson we will be using some of Newton's equations to animate our simulation and we explored how these equations work in our effects lesson to begin let's simulate a very simple model a particle which is only experiencing gravity and it's dropped from some position on the screen the program I'll write to simulate this situation will divide into two parts at the top output our initial system settings such as the starting position and force of gravity and below that we'll use a function called draw to do the animating first let's define our initial settings gravity is a forceful one to control so let's define a variable called gravity instead of the say a 10 we can play with it later we will want another variable to store the particles mass I'll set this at 30 for now plus a variable for the initial height we drop the particle from alcohol this position Y we also need an initial velocity for a particle I'll call this velocity Y and at the very beginning of our simulation right before we drop it we set this to be zero finally we need a way to control the speed of our simulation we'll do this with a variable called time step think of time step is how much time elapses between each drawing update a larger time step will make the particle speed up between frames and a smaller time step will slow it down now let's consider what goes on inside the draw function when we run our program the computer will first execute the initial settings once and then loop through the draw function multiple times per second so each frame of our animation will be a single pass of this draw function first I'll compute the forces acting on the particle for now the only force acting on it is gravity which is pointing downwards I'll store this downward force using a variable called force y and from Newton's second law we know that this force will be equal to the particles mass times acceleration due to gravity next I use that force to define how fast our protocol will accelerate downward I'll store this value a new variable called acceleration Y to do that I'll rearrange the handy formula F equals MA to give acceleration y equals force Y divided by mass notice we've already calculated force Y in the previous step now that we know how fast our protocol is accelerating we can update its velocity using the formula velocity equals velocity plus acceleration times time step we derive this formula in our effects lesson check it out for more details note that the velocity variable on the right hand side of this equation initially stores the previous velocity value after this line is executed the velocity variable on the Left stores the updated velocity value finally we can use this velocity to draw our particle in a new position using the equation position y equals position y plus velocity y times time step as before the position Y variable on the right hand side of this equation initially stores the previous position value after this line is executed the position Y variable on the Left stores the next position value notice how each step in our calculation uses the result of the previous step the initial force calculation is used to find acceleration acceleration is used to find velocity and velocity is used to update the position now the fun part we just draw our particle in that new position to do that we just draw a circle using the position Y variable as its height here I'm drawing a circle using the ellipse function with equal width and height now let's run our program to see what happens oops I want to erase the previous circle every time we move it so it looks like one thing falling instead of the snaky thing I'll fix this by erasing or redrawing the screen every time I draw a new circle let's try that nice if I increase the force of gravity our particle falls faster as we'd expect that's Newton's laws of motion in action let's pause here so you can get comfortable with this code