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:2:50

Video transcript

so far we've limited our simulation to vertical motion only but we want our hairs to sway in various directions so we'll need to update our code to include two dimensions of motion it's fairly easy to do this we just need to double up many of our variables so that we'll have both an x and y component right now we only have an initial vertical position called position Y so I'm going to add a new one called position X I'm also going to add a new variable to store the horizontal component of the velocity I'll call this velocity X it should be initialized to 0 the same as velocity Y because our particle starts from rest now I'll move into our draw function and duplicate each equation so we have a horizontal component as well currently there is spring force Y but not spring Force X I'll add that now spring force x equals negative K times position X minus anchor X I'm going to add a new variable called damping force X I'll set it equal to damping times velocity X with these new variables I can add a new net force calculation in the x-direction called force X it will be slightly different than force Y because we can ignore gravity since the force of gravity only acts in the vertical direction so force x equals spring force X minus damping force X now let's move on to acceleration now add a new variable called acceleration X and it will be equal to force X divided by mass let's do the same thing with the resulting velocity I'll add a new variable called velocity X which is equal to velocity X plus acceleration X times time step finally we can update the position so can have a horizontal component - I'll do this with a variable called position X which equals position X plus velocity X times time step now we have x and y components for force acceleration velocity and position we just need to update our draw code so the circle representing our protocol and the lie representing our spring can move in the x-direction I'll simply add position X to the end of the line connected to the mass and for the circle representing our mass I'll add position X to its center position now watch what happens when I run the code ah-ha 2d motion to make our movies we do the same thing in 3d using a third Z component that's a fun bonus project to start thinking about but let's pause here so you can get comfortable with this code before moving on to the last step