## 3.1: Angles and Angular Motion - The Nature of Code

#### Sharing buttons:

oscillation this is the topic now that

we're going to look at in this section

of videos part three it's math it maps

to if you're following along in the

nature of codebook chapter 3 we're going

to look at oscillating motion what does

it mean to have something that moves

with a period with a frequency swinging

of a pendulum the bouncing of a spring

the plucking of a guitar string the the

waves of water what types of things can

we model in nature with oscillating

motion and we can see a nice example of

this this array of pendulums this is

something about that by the end of this

section of videos you could model

in-processing we're actually going to

look specifically at how to model a

pendulum and then you could make an

array of them all starting at different

times and this might be an exercise you

could think about doing at the end of

this section of videos but so before we

could do any of that

okay so oscillating motion waves

pendulum Springs that's what we're

talking about but to be able to really

do this stuff and actually to a really

important topic that we need to have in

our bag of tricks when doing computer

graphics programming is we need to know

trigonometry well this is a right

triangle a right triangle has sides

right side side side it has angles the

relation to study of the relationship

between the angles and sides of a right

triangle that's trigonometry and we need

trigonometry to do lots of stuff on the

screen we need to know about sine cosine

tangent and it's also going to allow us

to do some nice stuff related to

oscillating motion figuring out angular

motion angular forces in terms of

looking at a pendulum so trigonometry we

need to we need somewhere to talk about

trigonometry this is the place this is

the time before we can even talk about

trigonometry which will really be in the

next video though I just want to think

about basics of angles and processing

how do we rotate something based on an

angle and how might we model angular

motion could angle be a position could

there be an angular velocity in an

angular acceleration that's what I want

to look at just in this first video to

get

kind of started here so you're probably

familiar with angles right here is a

90-degree angle here is a 45-degree

angle you know I'm guessing if you're

found your way to these videos you've

probably done some stuff in processing

and realize that processing in most

computer graphics environments look at

angles in radians right 90 degrees is PI

divided by 2 radians 45 degrees would be

PI divided by 4 a full 360 degrees is 2

pi so I you know I want to just take a

brief 30 seconds here and cover what is

a radiant it's kind of like it might be

a little mysterious to you well the

quite simple if you have a circle with

not sure if I'm drawing this to scale

one Radian is the angle at which the

length of this arc is also equal to 1

incidentally with a circle of radius 1

the in the length of the entire contour

the circumference of that circle is 2 pi

so pi is this magical number that just

kind of appears beautifully out of the

circumference of a circle and we get and

in radians are this very convenient

convenient unit of measurement for

angles it's not perhaps not as intuitive

to us because we're used to thinking in

degrees but the more you program the

more your brain will reverse itself to

we've got angles and radians hey that's

pretty good so we should be able to

rotate something right if I if this is a

rectangle if I want to rotate this

rectangle by 45 degrees or PI divided by

4 radians I should be able to get this

rectangle now this is not a lesson in 2d

transformations to do this rotation

you're going to need translate you might

need push matrix and pop matrix to

figure out a lot of this stuff we're

going to incorporate that into our code

let me briefly say that if 2d

transformations are not familiar to you

what you probably should look at is

processing org learning

scroll down I would suggest looking at

this particular tutorial I'm standing in

front of it

2d transformations so this will get you

it go pause go look at this tutorial if

transformations are not familiar to you

but so what we need to do to make this

happen is translate to the object's

location rotate by that angle and then

draw the object okay

so let's actually make that happen in a

processing sketch and we're we're going

to talk this I think it could be a short

video I'm excited we're going to talk

just about looking at angular motion

velocity and acceleration okay so let's

come back over here let's open up this

processing sketch so I have this premade

processing sketch which just draws a

rectangle in the center right so this is

the thing we're talking about with

transformations you might think oh I

know I'm just going to type in rotate

here and say pi divided by 4 and now run

it again and what's going on here it's

like down on the bottom it's not in the

center it is sort of rotated so what

happened is rotation always happens

around the point of origin so we rotated

that search square around I can't do it

around the origin up there we want to

rotate that square around its center to

move the origin to the center

we need to use translate so I'm going to

say hey translate to width divided by 2

height divided by 2 and then and then

rotate by that angle and draw the

rectangle at 0 0 and now we see we have

a rectangle rotated 45 degrees exciting

okay remember we there's probably about

10 15 videos about motion position

velocity acceleration velocity is the

change in position over time

acceleration is the change in velocity

over time well an angle is in many ways

a position it's the angular position of

that square now interestingly this is

actually simpler to deal with because an

angle is a scalar quantity it's a single

number it's not a vector but we can

apply we can have one variable to be the

angle another available to be the

angular velocity and another variable to

be the angular acceleration so let's

take a look at that what if I at the top

here say

a is some angle the angular velocity is

some amount and the angular acceleration

is some amount now what are the rules of

motion velocity changes position

acceleration changes velocity this is

exactly what we did in our update

function in our movers logan velocity

add acceleration but we're just doing it

here with angular and with with we're

just doing it here with angular's with

angles let's run this sketch nothing

happens but of course if we actually put

in we say hey let's give ourselves a

little acceleration we can see okay come

on that's a very small acceleration

right is it turning what's going on here

lost my mind ah

you know what would be nice it would be

nice to put the variable in here okay so

that was a little bit of a mistake you

know I had PI over 4 in there still but

now we've got a back in there we say ok

so that was not such a tiny little

acceleration you can see how quickly

acceleration adds up in terms of

rotating by radians but you can see here

we go we're getting faster and faster

and faster we could use constrain to put

a cap on velocity lots of things we

could do we could also just really

quickly right now just to kind of polish

off this example I could say hey let me

actually calculate the acceleration on

the fly by mapping Mouse X which goes

between zero and width to a number

between negative 0.01 and 0.01 let me

add if some extra zeros in here to make

it smaller let me run this and we can

see here now if I stay in the center

there no acceleration if I go this way

we're going to start going that way if I

come over here it's going to slow down

stop and come back so you can see how

I'm modeling the motion the rotational

the angular motion of an object the same

way we modeled linear motion throughout

Cartesian space angle is

angular velocities velocity angular

accelerations acceleration so you might

sort of wonder how would we then apply

this to to one of our previous examples

and I think it's worth just pulling one

up if I grab this example if you recall

which is which was our a track array of

of movers that are attracted to a

singular attractor in the center what if

you wanted to give these objects a

little bit of angular motion well if we

go look at the class we can see we have

location velocity and acceleration each

object has those three variables to keep

track of its motion throughout this

Cartesian space so what if I just went

back to this example we did and grabbed

these three variables let's give this

object now in addition to having the

vectors for its motion throughout space

we have three angle three three scalars

for its angular motion and I could go

and I could just I could go to its

display function and I could add the

transformation code we need to so that

we can rotate the object and we're going

to now once we've translated to where

the object is we've got to draw it at

zero and notice the use of push matrix

and pop matrix in this object this

object is going to perform a

transformation so that it is at a

location and wrote and rotated we want

it not to affect any of the other

objects it just needs to translate and

rotate for itself so we say push to save

this transformation state do all our

weird translation rotation say pop to

restore so this object is kind of has

its own transformation state okay now

that we've done that so much for short

video over ten minutes now that we've

done that we can run this and see what

it looks like okay so one thing is

circles and not so much you don't really

see the rotation so much so let's let's

let's change these two rectangles and

let's say rect mode Center let's run

this we can see we've got rectangles

there of course we didn't at we didn't

add the angular motion to our sketch we

need to go and say what's that motion

algorithm for angles it's this

so now in our update function just in

the way that we add acceleration

velocity velocity location we need to do

that with with our angles velocity - the

angle angular acceleration - the angular

velocity we're now doing that both with

the objects angle and with its and with

its a position so if we run this now we

can see they're all spinning now here's

the question though how should they spin

and there's lots of ways and simulation

ways we can kind of get it we could get

further into this but what I want to

just say here for the second for the

moment is what if we just did something

simple and just said let's calculate its

angular acceleration according to its

acceleration along the x axis and I'm

going to divide it by 10 so you can see

here we're get something that appears

somewhat dynamic that the that the

annular the way that it's rotating is

related to how it accelerates

horizontally now that is a completely

arbitrary choice but you might try and

as an exercise you might try to build

this in such a way that can you get it

to appear realistically like as if it's

sort of spinning throughout space and as

it slows down it spins back the other

way what happens if you use Y what m/s

you've used the magnitude of the vector

the angle of the vector there's lots of

things that you could map to that

object's acceleration you could also

think about forces if there was a wind

force with that wind force the hore

affect its rotation in some such a way I

don't know that there be wind in this

example but you can sort of see where

this is going so this this video all we

really did all I want from this video

hopefully it worked was to kind of get a

sense of right what is an angle angles

can be used in an object's rotation and

angular motion the same way we modeled

motion in throughout space

two-dimensional space we can that same

concepts we can we can use through with

an angle with angle s position angular

velocity and angular acceleration okay

and in the next video what we're going

to look at is actually start to

understand a sort of look at the details

behind trigonometry what does it mean

then to take the sine of an angle the

cosine of an angle the tangent angle and

how might that affect some of these

systems that we build okay there we go

that's it for now