Okay, I just bumped into a lamp over here, but everything's fine okay. Good morning
We are going to get started about vectors again this morning today
I have a long list of videos here to make we're going to go through P vector syntax basic vector maps
We're going to look at this thing called
Acceleration where you get into Newton's laws and forces and we're really by the end of this string of videos
There's maybe five or six or seven we're going to get through we are going to have our first physics engine
We're going to be simulating bodies in our world responding to forces. It's very exciting
[whew] okay
So but what are we going to do this video this video sole purpose is just to begin to feel comfortable with
P vector Syntax, what do I mean by P vectors? so p vector,
affectionately known as phu-vector
uh, is a class in processing. You might be familiar with P image or phu- image?
Phi-mage or phu-font, phu-font,phu-font, pee-font...anyway,
There are lots of built in classes in processing to describe an image a font
Capture object a movie object a serial object this is hopefully something you're familiar with having used processing
we're going to learn about a new class today called P vector. P vector is going to store the
Components of a vector if you remember from the previous video a vector which can be represented as an arrow?
Direction which way is the arrow pointing
magnitude how long is that arrow has components there is an
X component and a y component now in truth the p vector class also stores a z component
But for the purpose of these videos that we're doing now we're going to ignore 3d. It's a very uncomfortable
Strange place to be in 3d. It's just living at two dimensions
You could just kind of you know be yourself really anyway um okay, so this is what we're going to do now um
We need to get into actual vector math
What does it mean to add a vect, add vectors, multiply, dot product, magnitude, normalize or all these functions?
We're gonna get to but first. I just want to look at something really basic
What if we have a program?
that has an x
and y variable, what if we want to store those x's and y's
right together in One vector object
So what we can now do is say P vector
V or you know maybe for the sake of argument
I'm going to say position or in fact in all my [videos] I kind of like the variable name "position" better, but all my vars-
examples for some reason I use the word location
Don't know maybe I'm into real estate or something..
Okay, a P-vector location
We're instead of having a separate x&y we're going to say a p vector a location now
When you have an x in a y you might say x equals 100
y equals something instead, we're going to say location
equals a new p vector
100 comma 50 for example
I don't know can you see all that you I think comma 50 is cut off
but you can imagine comma 50 parenthesis semicolon
Okay, so what is going on here?
primitive variable the name is x the datatype is float
A Object variable the name is location the datatype is P vector. Whatever you make an object bubble be ball B
What kind of objects do people make and process?
I don't know fish f right then you say make a new fish make a new ball
Make a new bubble right we are instantiating that object. We do the same thing with P vector
P vector, uh
Location we say location is a new instance of a p vector object and the constructor takes two or three
Arguments, so in our case. We're dealing with [two-dimensional] vectors
P vector 100 comma 50
Everybody okay, everybody. I don't know if there is an everybody is there at everybody
I don't know maybe you're like a large group of people just sitting in some strange audience watching this video
I seriously doubt that.
okay, uh so now that we're comfortable with this let's go look at a real-world processing example
ahh
Okay, here. We have your kind of standard
Processing example you have a bouncing ball sketch. You have a ball object b. You make a new ball B
Moves, it [bounces]. It displays. This is your basic stuff that we're we're used to but let's actually now switch over to
The ball tab this is where things matter now
Look we have an x and a y and at x speed and a y speed this is where we need to rewrite [our] code
using vectors
Okay, [I'm] staring over here like really intently okay, so instead [of] let's close this here
It's distracting instead of an [x] and y I want to say P vector location
So I'm just going to write that [in] there, and I'm actually just going to delete those
Now we have another pair [of] x's and y's x speed y speed we know down the move function x B changes x y speed
Changes y
We're going to do the same thing
But with vectors so instead of an x speed and a y speed we are going to say p vector
And I'm going to call this velocity
now it turns out velocity is going is a key concept in that we're going to build into our physics engine if an object has
a position
How that position changes over time is its velocity velocity is a vector that gate provides directions for how that object's position
Should change from one moment to the next.
so position is x comma y
Velocity here is x speed comma y speed that's the sort of that's the conceptually the thing we're doing [ok] so now we have to
Look at the constructor. We initialized x and y separately xb and y sweet separately now
We're going to do that with vectors [ok] now. We're going to say location equals new P vector
same values
and
Velocity... equals.. new p vector. Can you hear that "tapp tapp" typing? I don't know is that like [a] nice soothing sound for you
right so we can [see] here all I'm doing is replacing each individual variable a
being being initialized individually each individual variable
[Da] I
Would restart this video, but I just got too much to do you know what to do
You're just [gonna] have to live with my weird ramblings, okay um
Velocity now we initialize it as a p vector with two arguments
Got it [all] right. We're good. Don't don't don't don't don't don't do that
We run it of course if into here, okay x equals x plus xp. Okay? We need to stop here [for] a moment
Uh well maybe we don't we will stop here in a moment
but let's [actually] let's just look at [this.look] [x]
changes for Xp
Eight y changes by y speed well instead of x and y instead of x given y speed we have location and velocity [I] know
I know I got [it] [okay]. Here's what we're going to write, right?
location
equals location
Plus velocity right that's the same thing instead of two variables each getting changed by their corresponding
x
Speed y speed now
We have one variable getting changed my velocity look that's got to be perfect right ah there's what could possibly be [wrong] with this I
Think it's great. I think this is how the code should be let's run it. Okay now. I don't know if you can see this
Zoom worked the operator, huh plus
Buckle underneath [here] the operator plus is undefined for the argument types processing quick or not Pete becker processing that [cord] IP vector
It's freaking out, right?
What's happening here okay? So now we do need to stop for a moment and go back to the board okay, so
Let's think about what the plus operator means
we know
writing plus in Code Works quite often
x
Equals [five] plus [two] now [it] works. We get the value Seven five plus two
We can even do something like s if s was a string equals a b
Plus C. D
What does plus mean when it comes to strings it means concatenation we get [abcd] so plus with numbers means add?
Plus with strings means concatenate now. What if we have P vector?
V and U and w for example and I said w equals V plus U so these are p vector objects?
the thing is uh
You just can't do that. It's very sad
I don't good. Said it would be nice and there are some other programming languages environments that allow you to use this plus operator with
objects, but
Processing doesn't know how to say a vet p vector plus another p vector any more than it knows how to say a p font
Plus another p
Font or a p image plus another p image, so yes, we want to do addition
but addition the plus operator is reserved only for primitive numbers and for strings so in order to
Add [two] p vectors together we have to use methods that are in the p vector class
Those methods we [can] make a list of them add
subtract multiply
There's gonna be normalize dot product Ross product limit. [oh], my goodness are so many
We could go through the holistic and walk over there
We could look at the list that's what the next video is going to be about so the next video
[I] want to go through some of these functions and what the math behind them means for now
Let's just take a little step into adding vectors. So what was the situation? We had?
We had a x equals x plus x speed before we had vectors and we had the same for y
Now we think we have location. I'm going to shorthand these loke equals [lok] plus velocity
Well by the way, there's a shorthand x plus equals x speed
Which means add x speed to x and in fact the aDd method in the p vector class we can say this
lok
at
Velocity what this means is take this vector and add it to [that]
We're going to look again at the details of addition in the next video
But you can kind of guess what that means add?
The x of velocity to [the] x of location add The y velocity to the y of location y?
because here is locations x comma y add
[the] some x value add some y value and now here's the new location
So we add the velocity vector to the location which is kind of like a vector from the origin
Of like know, [I] can't move there's a wall here move this wall, and then we get our new location
[so] this is what we're doing now. Let's go back to [our] code
Maybe I kind of feel like you might have a question right now
You should ask that question in some format that I don't know what exists there
You can if you're watching this on vimeo you can make a comment below, please I'll answer it that way
[okay], so here. We [are] over here. Now. We know that instead of this line of code the [cou] the correct thing
We write is velocity add. [oh] sorry location dot add velocity
Location dot add velocity is the same thing as saying add velocity to location store it back in itself location equals location plus velocity
That's what we're doing
now
now we run it and [ah] look at this if
the x
Location is greater than width or less than zero that's where we reverse the direction so all of that is for the bouncing so here's
the thing about
Vectors for the most part what we want to do I feel I feel they need to come over here is do stuff like this
We want to deal with vector operations between vectors. We don't really want to get into the components individually
It's kind of like that object oriented principle of encapsulation. We just want to drive the car [while] we're driving the car
We don't want to be messing with the wiring the x and y [components] are really the wiring of that object
But sometimes we can't help ourselves
We need to just do something to the [x] like we need to just know did the x part of this object's vector get past
the end of the screen and this is a case where that's true here in this bounce function, so I'm going to say if
location dot width or
Location dot x and I was so smart, I thought to size my window properly then here
What do I want to reverse I want to say velocity?
[Dot] x
Reverses Direction, so I'm just changing
Everywhere here now. I'm just kind of it's not very exciting
[but] this is I'm just using the [x] and y components of these vectors
Appropriately, and now when we want to and certainly when we want to draw an object we want to display it on the screen that
Is a moment where we need to [access] the individual components so all the drawing functions in processing generally ellipse?
Rectangle line da da Da they all want an x location in a while, okay, ssin, right?
We can't say draw an ellipse just with this vector object
Although we could make a request that those would all be implemented
But I don't know I think I like it [this] the way it is it's fine
We need to access the individual components to draw the object of that location, so I think we've gotten this right
We have our two p vectors in the objects constructor. We initialize them in the move function
Velocity gets added to the location. That's what we do
frame every frame location changed my velocity location change my velocity location change my velocity and then when the only other functionality
We do is have we get to the edge? Yeah? We should turn [around] we want to stay on the screen
That's what we want to do and then we want to draw ourselves
So here we are
This is a moment now. I kind of hope that something went wrong so we can discover it
I kind of feel like we got it
All right, though, but one of these days something will go wrong and we'll discover the problem
[I] was a live in real time but some we live for me. I can't with this system
Will stream by the way so we'll do some live stuff where we can almost feel as if we we know each other enough together
It's [ok] if you don't want to get any [closer]. [I] would understand ok uh
Ok it worked so um this is this marks the end of this particular video
[what] I would suggest to you before moving [onto] the next [one] and we sort of mentioned this in the previous video [is]
Find a sketch of yours sure you got one that's got x's and y's all over the place
[just] [try] rewriting it with vectors with the identical
Functionality don't make it do anything different if you get stuck you can [always] just revert to saying wherever you had x say the vector
Not x wherever you had y say the vector dot y try to use some of the functions in the vector class like add?
If you can but you you know don't worry yet. We're going to go through though
Go through all those in the next video
I really hope this won't work because ok this is like a one take one, and I feel like it's not the worst one
[we] [ever] made ok see you later uh
I got to find the clicker to Stop [good]. [bye]