Placeholder Image

Subtitles section Play video

  • [BELL RINGING]

  • Here we are.

  • I think this really is the last video in chapter 1 on vectors.

  • I'm going to add one more detail to this example

  • of the random walker, which is no longer called a walker.

  • I'm now calling it a mover.

  • And where I last left off with this example is

  • every time I click the mouse, the mover

  • picks a new random velocity that can

  • be seen here in the constructor p5.vector.random2d.

  • Now, you know that's a static function because I

  • made a whole video about that.

  • And then that velocity-- every frame is added to the position.

  • And the mover is drawn as an ellipse at that position.

  • So I want to add one more concept to this video.

  • And that is the concept of acceleration.

  • Acceleration will act as the building block for all

  • of the examples I'm going to show you in chapter 2

  • because of Newton's second law, force

  • equals mass times acceleration.

  • So acceleration plays a key role in any kind of physics engine

  • or physics simulation.

  • But right now, we're not going to worry

  • about the physics aspect of this and just add

  • the idea of acceleration.

  • Our mover object currently has a position and a velocity.

  • And we've established that the position is a vector that

  • describes relative to the origin where that object is

  • in two-dimensional space.

  • The velocity describes how the position changes over time.

  • So on a frame-by-frame animation,

  • the position is here.

  • Then the velocity is added to the position.

  • And its new position is here.

  • And if its velocity remains constant,

  • it would keep just moving like this.

  • This velocity would be applied, applied, applied.

  • However, acceleration can be described as the change

  • in velocity over time.

  • So if this object, this mover, were to accelerate,

  • maybe you-- and the typical way you

  • might think about that acceleration

  • is it's getting faster.

  • So we could imagine it that way.

  • First, its velocity is this.

  • Then it's like this.

  • Then it's like this.

  • So it's accelerating and moving larger steps each frame.

  • But acceleration just means the change in velocity.

  • And remember, velocity is a vector.

  • So that could mean it's changing its direction

  • or its magnitude could be slowing down.

  • That's an acceleration speeding up, turning.

  • All of those combined together-- that's an acceleration.

  • It's just another vector.

  • And it goes into our--

  • what I'm calling the motion 101 algorithm.

  • And that motion 101 algorithm can be found right here

  • in the update function.

  • Add the velocity to the position--

  • this.position.addvelocity-- and right

  • before that, this.velocity add this.acceleration.

  • So now there we go.

  • Why isn't it accelerating?

  • It's not accelerating.

  • It's not changing its magnitude or direction

  • because the acceleration is 0.

  • So let's give it something.

  • Let's also give it a random vector.

  • Ooh.

  • So now you can see that it's speeding up.

  • Now, the acceleration here is constant.

  • And it's almost as if there's this big fan

  • right in the center that's just pointing

  • in a direction and blows the mover off in a direction.

  • You could see that it's-- turns ever so slightly.

  • This is most likely because whatever velocity it picks

  • is not the same direction as its acceleration.

  • To demonstrate what's happening here a bit more clearly,

  • I might do something like set the magnitude

  • of the acceleration to something much smaller.

  • So I'm still picking a random unit vector in some direction,

  • but then setting the magnitude to 0.01.

  • And you can see now it's turning around.

  • And I could also go back to putting the background

  • and setup.

  • That might help us see the path that it's taking.

  • And we got-- here we got an acceleration

  • in a different direction from the initial velocity.

  • I could also reset the acceleration

  • to something random every time an update--

  • let me take this and put it here,

  • take this off, run it again.

  • So this is something resembling that original random walk.

  • But the acceleration is cumulative.

  • It's accumulating into the velocity.

  • So if it's picking it in the same direction

  • randomly a few times in a row, it's

  • going to build up enough speed and fly off in that direction.

  • Now, there is another useful vector function

  • that I haven't mentioned to you that's

  • similar to the normalize function that I could use here.

  • And it's the limit function.

  • What limit does is take any vector

  • and cap its magnitude at a certain amount.

  • So if I say limit 5 and this vector has a magnitude of 15,

  • it will take that vector and shrink it down,

  • set its magnitude to 5.

  • The difference between limit and, say,

  • set magnitude or normalize is if it's less than 5,

  • it's not going to raise it up to 5.

  • It's going to leave it at, say, 2.

  • So back in the code, what I can say

  • is any-- after I apply the acceleration,

  • let's make sure the velocity doesn't get too large.

  • And I can say limit.

  • I don't know.

  • I'm just going to say limit it to 2.

  • So it's a pretty strict limit there.

  • And then if I run the sketch, what this-- this

  • is now really looking much more like that random walk before.

  • But I am using the concepts of acceleration and velocity.

  • And so now this would allow me to do a lot more stuff

  • with a random walk.

  • And that's going to come as I get

  • to more stuff and more and more of these examples in chapter 2.

  • But ultimately, what I want to show

  • you here is what happens when I calculate

  • a very specific acceleration, like one that points

  • towards the mouse location.

  • Still I might say you could pause the video here and try

  • all sorts of other algorithms for calculating acceleration.

  • What if you used Perlin noise for the acceleration

  • or what if you had the acceleration based

  • on some vector that's based on some other type of data that's

  • coming into your system?

  • But for this particular example, I

  • can return to everything that I did in the previous video,

  • which is I have the position.

  • I have the mouse.

  • And now I just want to do that same exact math operation,

  • mouse minus position.

  • Set the magnitude to something fixed.

  • And apply that as the acceleration.

  • Let's see what happens.

  • Start by creating a vector at the mouse location.

  • [MUSIC PLAYING]

  • Then I'm going to set the acceleration

  • to the result of subtracting the mouse minus this object's

  • position.

  • Then I'm going to set the magnitude of that to 0.1.

  • And I'm picking that arbitrarily.

  • Now, I have a three-step process.

  • Calculate the acceleration.

  • Apply the acceleration to the velocity.

  • Limit the velocity.

  • Then apply the velocity and position.

  • Let's take out the limit just for right now.

  • Maybe we don't need it.

  • Amazingly, we get this result that's

  • something like an elliptical orbit.

  • You would think the object would just be going straight

  • towards the mouse.

  • So if this is the mouse right here and this is the object

  • and it's going in this direction,

  • basically, I'm taking this and applying it

  • as the acceleration.

  • So if I take this acceleration vector

  • and apply it to the velocity, velocity plus acceleration

  • equals the new velocity, which is this.

  • And then add.

  • So then it's over here.

  • Then this is the vector I add to it.

  • So we take this velocity at acceleration.

  • And now this is the new velocity.

  • See how I'm ending up in this path?

  • Now, depending on the relative strength of the acceleration,

  • then I might be sucked into where I'm--

  • these-- what I'm being attracted to much more quickly or I

  • might spiral around it.

  • And we can experiment with that as a variable.

  • So for example, what if I said instead of 0.1,

  • set magnitude to 5?

  • Well, you can see now it's really going out of control.

  • So maybe this is where I want to limit the velocity.

  • And you can see now I've done that.

  • It can't move very fast.

  • But it's going to-- remember, that acceleration

  • is super strong.

  • It's going to move right to it.

  • And in fact, it's actually going to stop at it because it--

  • if it gets past it, it's going to tell it to slow down and go

  • back in the other direction.

  • And maybe I want to limit this and set this here.

  • There's so many different ways.

  • I don't know what I'm trying to do here.

  • But you can see I can get very different kinds of qualities

  • of how this object follows the mouse based on this model

  • by playing with what the velocity's limit is

  • and what the strength of the acceleration is as well.

  • Now, how I actually calculate the acceleration

  • and how I think about this particular mover

  • object in a world that a lot of different things

  • could affect its acceleration-- that's really where I'm going.

  • And that's really the model of forces.

  • So there's really this idea of a force.

  • There's this force that's pulling

  • the object towards the mouse.

  • But what if there were other forces?

  • What if there's forces that are coming

  • from the walls of the canvas, so to speak, the edges?

  • What if there was just this wind force or gravity or other types

  • of things, friction force?

  • So this is where I'm going in chapter 2.

  • I am going to calculate a variety of different force

  • vectors and put them all into the object's acceleration.

  • Now you have something you can really

  • play with as an exercise.

  • Can you create a simulation with an object

  • moving around the canvas that all of its motion

  • emanates from its acceleration?

  • What are other ways you could calculate the acceleration

  • to create some type of dynamic motion?

  • And so this wraps up this section on vectors.

  • I'm sure I missed so many things about vectors.

  • And there's many more math operations

  • with vectors and other things to think about and consider.

  • And hopefully, I'll continue to touch

  • on those as I go on and on to more and more examples.

  • If you make something, share it with me

  • at thecodingtrain.com in the comments here.

  • If you have questions, you can also ask them.

  • You could join the Discord to ask your questions there.

  • And I will be back in section 2 to talk about Newton's laws

  • of motion and forces.

  • [MUSIC PLAYING]

[BELL RINGING]

Subtitles and vocabulary

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