okay this is a very important moment a
very important moment we are about to
build build the foundation for
everything we're going to do in all of
these videos by looking at a new concept
essentially a key key new concept so
before we get to this key new concept
which you might be wondering what it is
we're going to just sort of revisit
where we left off a couple videos ago we
were building this example we had a
bouncing ball around the screen we've
changed it instead of a bouncing ball
it's called a mover object because when
you make a raise of ball objects you
start seeing balls all the time and
people start seeing anyway that's not
your problem it's my problem but I've
gotten past it and we now have a mover
object and I changed a few things I'm
going to run this sketch and you can see
instead of bouncing off the edge it's I
want to get to the bottom it goes to the
top when it gets to the right it goes to
the left minor details so what's
important about this okay so if you
recall if we look at the class we can
see here that we have an object with two
vectors location describes where it is
velocity describes the change in
location over time and what do we do in
the update function we add velocity to
location and down further below we have
a display function where we draw the
object at that location that is our
model here that is our model but we at
this moment are going to change this
model you know so okay well let's just
say you look at this and you think
really I'm watching all these videos for
hours and hours and hours and author is
a circle it's kind of moving across the
screen hey well so sorry I just got off
on that tangent there what is it what
would you what would your next instinct
be your next insig might be I know I'm
going to change velocity I'm a change
location I'll make location do something
different outside location on Tech's
school this or velocity lexical does not
make it random to do all sorts of stuff
no this is not the purpose of this video
at my recording recording I don't have
to restart this one this is not the
purpose here we're going to make work as
an exercise to ourselves we are going to
limit what we can do and we're going to
say from this moment on not forever and
ever because I'm sure you the point is
to break all these rules here
we're going to say we
are not going to mess with velocity
we're not going to mess with location
these are just these variables are going
to exist in the state that they are
velocity codes allegation but we are
instead going to add a new vector so
let's try to let's get this makes me
feel weird I keep looking back and forth
let's go over to the whiteboard so okay
so if we were to rewrite that function
just so we remember we have it over here
we have this update method oh right we
have an object it has a location vector
it has a velocity vector I didn't really
I don't know whatever I'm gonna hurt you
rerecorded this huh doing it worse this
time but I don't care I'll do it again
tomorrow get it again forever and ever
we have an update method it's very late
the day I have this rule to record these
videos of the warning when I feel sort
of my brain is working and I feel
energized but stuffs got to get done
stuffs got to happen okay we have an
update method what are we doing that
update method we add velocity to
location what's going on here we have a
processing window we have an object it
has a position vector it has an x and a
y we're drawing it there as a circle
that's the location vector maybe it has
a velocity which says that we had
velocity location so now it's over here
we have velocity location so now it's
over here we have lost the location so
now it's over here great so here's the
thing location changes over time by
velocity we want velocity to change our
instinct might be and this is what I was
getting to before it I couldn't just
make it happen all at once I might say
velocity x equals a random amount
velocity dot y equals a random amount or
maybe velocity x minus equals point one
I try to like figure out some way to
change those variables but no we need to
do this in a much more formal way what
we need is to say we need to add another
vector to this mover object that other
vector we're going to add is a vector
that we are going to call acceleration
and just as location changes by velocity
over time velocity is going to change by
acceleration over time and this makes a
lot of sense if you think about it right
if you're driving a car and you think of
pressing on the gas you're accelerating
that car pressing on the brake you're
decelerating the car but this analogy
only takes us so far because you know
you're thinking of speeding up and
slowing down what would be really is
important to realize is that
acceleration is any change in velocity
we could be changing the magnitude of
our velocity or the direction of our
velocity a turn a speeding up is slowing
down it is a vector that adjusts
velocity over time so so this is this is
the exercise we have to ourselves this
is our physics engine here this is our
methodology acceleration changes
velocity velocity changes ocation we
don't ever set velocity to anything
directly we don't ever set location to
anything directly we now need an
algorithm for acceleration we need to
figure out what should acceleration be
and so what I want to do in the rest of
this video is add this stuff to our
example and then look at a couple
scenarios for what might some algorithms
might be for computing an acceleration
and see what types of effects we get
okay I feel good about this had a little
problem there for a second but we're
moving on we're at five and a half
minutes or so and so far we're good okay
so here we are over here again and let's
look at our class so we know we need to
add to our object and acceleration we
know that we need to give that
acceleration an initial value and just
to simplify things also let's give the
velocity so velocity and acceleration
both start at zero let's run this sketch
again and what do we get a circle that
doesn't move all this time and it's just
a circle the center of the screen it's
like it's a nice very happy circle no
there's nothing nothing to say - you
have to move actually moving is very
good for you and that's the standing
thing I think actually it's anyway never
mind okay
these are pixels who cares okay so but
we do need to add this other line of
code velocity dot add acceleration so
now our physics engine is at work
acceleration goes into
velocity velocity goes into location
okay so now we need an algorithm for
computing acceleration and I wrote down
a couple possibilities over there
there's another whiteboard that you
can't see I wrote down some
possibilities I hope the cameras don't
shut off they're not going to
everything's going to be fine
so - there's three that we're going to
do here one is we're to look at a
constant acceleration great week a
probably figure that up pretty quickly
we're going to look at a random
acceleration and then we're also going
to look at acceleration towards the
mouse although I think I may just say
that for the next video and have the
entire video look at how you might have
a dynamic acceleration that is a vector
always pointing towards the mouse which
is kind of an interesting prospect if
you think about it okay so let's first
look at constant acceleration well in
fact this is a constant acceleration the
acceleration is zero but let's give it a
quantity I don't know what makes sense
let's have it accelerate along the X I
don't know five comma zero that sounds
good right whoa okay so quickly out of
control and I don't know what it's doing
now so something we have to realize here
is that we are living in a world of
pixels I mean we're not let me rephrase
that this is a world of pixels we live
in a physical world where we talk to
each other and we shake hands and we hug
and we say hi and we eat food I don't
know do things it's late I'm getting a
little tired ignore me
this is pixels the point of what I'm
saying is five pixels per frame with
acceleration it's kind of insane
remember we're adding that acceleration
to velocity every frame 5 10 15 20 25 30
35 30 French 30 times per second 60
times per second we're going to be
moving 120 pixels in per frame in a
couple seconds that's insane
so I went off on a tangent but the point
is when we're looking at acceleration
values we need to think of small
quantities we're gonna have to be tuning
our physics engines appropriately to
pixels and acceleration in this case
makes sense with small numbers so a
point O one let's see and we have our
it's moving along it's getting faster
and faster look at it go it's speeding
up it's getting faster right you can see
this is constant acceleration the
object's speed we can see is getting
faster and faster every frame as
acceleration accumulates into velocity
again you might be wondering after all
this time it's just a circle moving
across the screen and it's just getting
faster I know
but these are the building blocks and
and you will obviously be more creative
in this okay but let's look at this for
a second and understand that
acceleration doesn't just affect the
speed of something we're noticing that
because of the the velocity is being
starting at zero but let's say I have
the velocity actually start moving
downwards at a rate of two pixels per
frame look at what's happening to that
it's accelerating to the right it's
almost as if there's a wind force
pushing it to the right it's started
moving down but it's accelerating
further and further to the right so this
is really what's key here if you ever
dreamt about programming smooth organic
flowing motion of all different types of
things then what you needed is to have a
location velocity acceleration and you
need to calculate that acceleration so
this you can see it's speeding up faster
and faster this is just still a constant
acceleration the world is going to get
much more interesting when we don't have
a constant acceleration when
acceleration changes over time based on
any number of factors the simplest way I
can think to demonstrate to you that to
you right now would be let's give it a
random acceleration so let's look at
that so first let me stop that sketch
let me put acceleration back to velocity
back to zero acceleration back to zero
and now in the update method we're
actually going to compute an
acceleration acceleration and you might
think okay I'm going to make a new P
vector and I'm going to give it some
random amount bah-bah-bah so that's all
well and good but one thing I should
mention about the vector class is that
the vector class actually as a function
built into it which will give you a
random unit vector this is incredibly
valuable gives you a vector of length
one at any direction that way anytime
you want boom random direction you got
one and you can scale it because it's a
normalized unit vector you can scale it
very easily so let's look at how we
would do that we say P vector dot random
2d now notice that we have to specify 2d
here this is one of the times where we
want to make sure we don't get a random
3d vector because if we get the Z
component we could potentially have the
XO y component at 0 anyway we want a
unit vector that's in two dimensions
it's just X and y so if I run this let's
take a look at what happens we can see
look at it it's kind of accelerating
randomly it's actually getting out of
control really fast so again this is a
kind of where I need to bring something
up again with you guys this is not
physics in any real way this is there's
no world of things and grab forces and
Malec Tron's and antimatter and the Sun
that's going to explode in a hundred
million years and their evolution this
this is not the real world this is
pixels on the screen ok so that most of
the time we are just trying to create
the feeling of something realistic then
actually perfectly simulating the
realism in some uber amazing scientific
way and so one of the things that we'll
often do is kind of happen you might
call it a cheat but I should method I
would I totally do not allow the world
word cheating when it comes to
programming this stuff because it's all
cheating there is no cheating or not
cheating none of this is real right
there is no this is just a wall by the
way
wall is green there there's nothing here
I can't get past it
but anyway the screen is over here
whatever what but all of that says that
one thing that's often useful to do is
say something like velocity dot limit v
what that limit function does is it says
you know what I'm going to introduce an
artificial constraint over this program
the velocity can never be have a
magnitude greater than 5 if it if it
ever has a magnitude greater than 5
shrink it down to 5 if it's less than 5
it's fine so that's what that limit
function done with does which can
actually be pretty darn valuable so you
see here actually here we go we have
this kind of nervous jittery x' moving
thing and we've created it only by
affecting the object's acceleration so
we could we could now scale that we
could make it more jittery less Yuri the
point of this is that this is your
exercise this is your job right now
using this foundation and the example
I'll link to it below if you're watching
this on Vimeo or through another
whatever wherever I put these I'm going
to link to the example below that has
this basic model try to create see if
you how much personality you can put
into the motion of something just by
drawing a simple shape and only by
affecting its acceleration if you're
stuck for an idea see if you can make it
dynamic like it's always following the
mouse what if it accelerates towards the
mouse and that's what we're going to
look at in in the next video another
couple ideas is what happens if you use
Perlin noise what happens if you use I'm
recording this right what happens if you
use Gaussian distribution of
acceleration values there's there's any
number of possibilities just make
something up try but whatever you do
just only change I'm going to just
highlight this right here only work with
this part of the code see how far you
can get just by messing with
acceleration okay so in the next video
we're going to look at accelerating
towards the mouse we're cutting on it
that's an exercise you can either just
go right ahead or you can try it and
then look at the video which will
provide the answer and the exciting
thing about all of this is that we're
going to see soon how acceleration is
the key to fulfilling Newton's laws of
motion I don't know if that makes sense
but it's getting late and I think I'll
probably take a break and do some more
of this tomorrow morning you don't need
to know that somebody edit this right
did I say this already
somebody just download all these videos
and edit them and like airbrush me or
something to make it look better replace
me with a puppet of a dinosaur or
something I don't know you guys are good
at this computer stuff okay I got to
find the button to stop recording this
time it was recording okay