here is a scenario for you we have a
processing window we have a mover object
it's a nice little friendly little
circle that we've drawn in that
processing window and that mover object
has a location vector right we know the
location vector we can think of it as a
vector that points from the origin to
that object's location which is some X Y
location on the screen now we know that
object has a location we know it has a
velocity and we're adding this piece
acceleration that's what we did in the
last video we looked at a constant
acceleration or random acceleration we
gave some suggestions ideas in this
video we are going to look at an
acceleration I have a scratch that
always points in the direction of the
mouse so let's draw our mouse arrow I'm
spending way too much time drawing this
mouse arrow and this that right there is
our acceleration this is something we
have to compute each and every frame
because the relationship between the
object location and the mouth changes
the user is moving the mouse the object
is moving so every frame we have to
calculate a vector that points from the
location to the mouse so how do we do
that thinking thinking are you thinking
I'm thinking I already know the answer I
because I thought about this advance we
need subtraction am i recording I'm
recording excellent we need subtraction
so first we need two things one is we're
in the mover class presumably we know we
have a P vector called location so this
we can just assume exists it exists it
is the P vector location now we're in
some function maybe this function is
called update and this is where we're
going to calculate our acceleration at
the very very end of this function we
want to set the value of acceleration to
something a vector that points from the
location to the mouse so how do we do
that well the first thing we need to do
is establish the mouse's location as a
vector the mouse's location is a vector
so we can create one P vector mouse
right is a new p-vector and what are its
values mouse X comma Mouse Y oh okay
so we have a new P vector called Mouse
which has a location mouse X comma Mouse
Y awesome we already have location boom
because we're in the mover class with
the location variable now we just
created Mouse how do we get a vector
that points from one location to another
that a vector that points from one
location to another is the difference
between those two locations the
difference between two vectors the mouse
- location is the vector that points
from location - mouse we went through
this in a video about vector math and
addition and subtraction I think I kind
of botched it which is why I don't want
to do it again but if you're confused by
that go back and try to watch that video
and then send me your complaint letters
to peel box something or other or
whatever I don't know I hate the regular
mail that's another story for another
time so we just can do something much
simpler here we could just say Mouse dot
subtract location and this vector that
we called Mouse is now a vector that
points all the way from here to there
all right
that's pretty good why don't we go ahead
and add that into our code ah no I want
to I want to but it's not the right time
what's the problem here let's let's kind
of let's for a moment bear with me
we're going to redraw this diagram again
okay we have the objects location and
two represented by that circle we have
the objects we have the mouse location
as represented by that arrow
let us now visually represent the vector
we've calculated here as the mouse
location - the object's actual location
does that vector make sense for
acceleration oh I will ask you it in a
different way is it your goal to
simulate the process of teleportation
right if we say if I want to get from
here all the way to where the camera is
which would be a bad idea because I'm
gonna walk
camera and that would be weird and bump
into it and fall over or whatever but if
I want to get from here all the way to
another point if I accelerate with a
magnitude of the distance between where
I am and that point it's like I'm kind
of getting going instantly there no we
want to create a simulation which
appears kind of lifelike and
improvisational these are terms we're
going to use we're especially going to
see those when we get to steering
behaviors later so we don't want to
accelerate with such a large magnitude
that will not be a very satisfying
visual experience for us to watch on the
screen so we need to reduce this
magnitude to something so there are lots
of different ways we could do that I
could say Mouse multiply 0.1 so I would
reduce the magnitude to 0.1 or I might
say normalize the mouse normalize the
vector to unit one and then scale
accordingly or even just as a simple
thing right now I could say set
magnitude to 0.5 some arbitrary value so
look how simple this was all we needed
to do because we have this physics
engine of acceleration into velocity of
velocity into location we already have
all of them sort of ground the
groundwork for motion done all we've
done is created a really simple
algorithm for calculating acceleration
as where's the mouse what's the
difference between the mouse and where I
currently in and make that a vector
sense magnitude to this and then I want
to put an exclamation point here you
can't put that in your code acceleration
equals that vector it's really as easy
as that now that we have this foundation
so let us walk over to our example doo
doo doo doo doo doo doo okay and let's
look at where we are but first let's run
this just so we remember what's it doing
right now this is the random
acceleration so it's moving every frame
with some random acceleration and we can
see that in our code right down here
this is random acceleration random
acceleration now we're going to adjust
this and we're going to do exactly we're
going to rewrite what we did over here
all right see that we're going to put
that over here we're going to do it
together okay so first we need a P
vector name Mouse which is the mouse's
location then we need to subtract the
objects ax
location then we want to set its
magnitude to I don't know we picked
point five and say acceleration equals
Mouse let's run this and look at this I
have an object that is following the
mouse and if I let the mouse just stop
here in the center you can see it's kind
of hovering around it now one thing we
one thing that's important about this to
realize is we still have this velocity
limit five so we had a little safety net
built in we don't actually know if our
acceleration how is really reasonable
so let's active just out of just set up
I really should set this video right now
because I finally did one that's only
seven minutes long but let's just take a
minute to just take a look at what
happens here we can see like whoa okay
so first of all something sort of funny
now that we have this kind of scenario
where we want this thing following the
mouse it doesn't really make sense for
when it goes to the top to come back in
the bottom you can see it's spiraling
out of control so one thing that's
really nice about the way that we built
this sketch is we have update edges and
displaying separate methods so we can
just turn off that wraparound on the
edges very easily and see what's
happening here oh come back now look at
this that acceleration is so strong that
it's kind of we need a much bigger
window to see what's happening so you
can see the value of kind of limiting a
velocity we could also say maybe that
magnitude was kind of very large let's
set it to point zero two and you can see
ah now we have something maybe a little
bit more reason was kind of like
speeding up we have this sort of
orbiting thing it's very similar to what
gravitational attraction might seem like
but I kind of like having that limit in
there I like having it a little bit
stronger so anyway this is up to you how
to tune this and to sort of figure out
what's going to kind of make the most
sense for what you're trying to do but
you can see now if you ever wanted to
have something follow the mouse this is
something following the mouse with this
kind of acceleration model there's a few
things about this that I think would be
worth noting number one is the magnitude
of the acceleration is fixed maybe you
want to think about a scenario where the
closer it is to the mouse the stronger
that acceleration or the weaker that
acceleration so is it stronger when it's
far away or stronger when it's close in
fact when we see gravitational
traction that is a perfect scenario of
this right the formula of gravity the
force of gravity the magnitude of the
force of gravity is inversely
proportional to the distance bodies that
are farther away experience much less
gravitational attraction than bodies
that are close look I have some
clementines here so these gravity weaker
gravity stronger weaker stronger in our
case force the acceleration is the same
acceleration is the same maybe you want
it to slow down when it gets near there
maybe you want to do there's all maybe
you want to add see if you can add some
random acceleration along with the
acceleration towards the mouth there's
lots that you could do for this but
we're gonna we're going to formalize all
this into a more and C's bye thank you v
2 v dr saying what you should look at
doing right now is creating a motion
simulation where you control
acceleration only we said this in the
last video I'm saying it again keep
going with that model in the next series
of videos we're going to we're going to
we're going to say ah acceleration that
is when we when we are controlling an
object's acceleration we're really
applying forces to that object and we're
going to have a specific model for
applying multiple forces to that object
that's where we're going from here and
that's what we're going to see in the
next video goodbye