Using Cos and Sin to animate
Its time for us to take the complex math we thought had no point from highschool and turn it into an aniamtion. actually cos/sin are so useful as we can use them any time we need to track movement such as when you create a googly eye that follows the mouse (that is done using cos-sin) in this example we will use them to create a moon for our globe.
Video Transcript
00:01Alright, so our next task that we want to do is we want to start creating a sphere that
00:05will go around our planet.
00:08Now to do that I want to talk about a little bit of mathematics, nothing too crazy.
00:12Alright, so if we look at our circle, at our sphere, we could find that we already have
00:17some information about our sphere.
00:19We're trying to figure out what is the dot in the sphere because we basically want to
00:22change the degrees and as we change degrees to figure out what the xy coordinates are
00:27of that specific dot.
00:30Now to make that happen basically we just need to know the length of this triangle that
00:35will give us our x and we need to know the height of our triangle or basically know the
00:40length of this line.
00:42So by knowing the length of this line and the length of this line by getting a degree
00:46we'll be able to know exactly what is the height of that triangle.
00:50Now the way it works is this way, when I use cosine what does that actually mean?
00:56When I'm creating a cosine I'm basically saying that a cosine is the adjacent which
01:00is the line that is really next to a triangle in a right angle triangle, the line that's
01:06nearest to the angle that we're trying to discover, divided by the long line which in
01:13our case is our radius.
01:14So basically the cosine value is actually this invisible value, we don't know how much
01:19it is, divided by a value, in our case we know what that value is and that combination
01:26of two values together create the cosine, the value of the cosine.
01:31Now contrary to that the sine, the sine is basically the opposite dot in the other side,
01:37the way that it's calculated it's using the opposite divided by the hypotents that creates
01:42our sine.
01:43Now in our scenario if we wanted to try to figure out our dot here, when we're using
01:48the cosine we're basically going to be able to figure out what is our adjacent and
01:53when we figure out our adjacent we're going to know where our x needs to be because it's
01:58going to give us the length of this line.
02:01On the other way around when we're going to figure out the sine we're going to figure
02:03out the length of our opposite because this is the invisible value that we don't know
02:07what it is.
02:10So the cosine and sine, the cosine and sine are going to help us figure out how we want
02:15to position things.
02:17Now lucky for us because a circle but just by its nature is kind of like a perfect shape
02:23it doesn't really matter that much if we use sine or cosine because they're interchangeable
02:28almost unless it's important to us to get the specific real angle.
02:32In our scenario it's not as important.
02:35So without further ado let's check it out and see how it actually works.
02:40So I'm going to go into our code again and in our code I want to create a new sphere
02:46so I'm going to go right ahead and it's going to start to get getting a little bit messy
02:51so I wonder if we could try to take advantage of our update globe but I'm not going to
02:56do it here because I'm going to leave that for you as homework and instead of that I'm
03:00going to just add more code onto this.
03:02So let me just start adding code I'm not going to even give it good names I'm just
03:06going to call it another private variable I'm going to call it shape2 it's going to
03:11be a type of shape.
03:14Now shape2 is going to be very similar to shape1 only we're not going to use the update
03:18globe because if we use the update globe we will need to update the update globe to be
03:25able to use this new shape that we're creating.
03:28So I'm creating a new shape and I'm just going to quickly basically just create go and create
03:33a shape2 graphics I'm going to just give it some sort of color so I'm going to use
03:38the begin fill I'm going to give it a color just randomly the alpha is going to be a
03:46hundred percent and I'm going to go back to the shape2 graphics and I'm going to
03:51draw a circle I'm going to position it at zero zero and I'm going to give it a
03:56radius of five so we're going to have a circle that its size in a hole would be
04:03pritex 10 pixels from one side to the other and the radius will be five so
04:07let's just make sure that we close our shape we end our film and now it's
04:14time to start positioning our triangle so let's start off with just positioning
04:19it right after we updated our shape so what I want to do is first of all our
04:24shape is positioned in the center of the screen so right after we position it
04:27what I want to do is I want to go to shape2.xposition and I want to tell
04:31shape2 that your xposition is going to be the shape.xposition plus the actual
04:37radius of that element which is probably going to be important because we want
04:41to make sure that we're bigger than the radius right now I'm just going to put
04:45this up so if our circle was 30 I want to put there a space of let's say of 15
04:49so I'm going to put here 45 which will basically give me a space of 10 pixels
04:54because our circle is 5 pixels radius now I'm going to do the same thing for
04:59the Y itself before we start figuring out how to make it animate and I'm
05:04gonna set my Y position and again to keep the whole thing exactly the same
05:08way now basically now the only thing missing is we didn't add our shape into
05:14the stage so I'm going to do that right away we're going to do an add child
05:20shape2 now if we run this we're going to see that our element is sitting right
05:24here but it's not moving which is fine now the next step that we want to do is
05:29enable it to start moving it around now as you could probably know this is
05:32the most extreme point it's the most extreme point where it's basically the
05:36edge edge of that if we had a square here it would be the edge of that square
05:40but we created a bigger square so it's even further out the next step for us is
05:46we want to be able to change it now when we change it obviously the values could
05:49only get smaller this is the biggest value it could be and then it would
05:53get the X and Y coordinates would change in tandem and what we're going to do
05:57is we're going to use the cosine and sine very very simple so let's see how
06:01it works so basically we're going to take the math dot cosine and we're
06:05going to tell tell inside of it what degree we want it to be so first of all
06:09we want to make sure that we duplicated by the real radius because the radius
06:13that our cosine is going to return is for radius of one so in our cosine we would
06:18love to just say let's start from the degree zero now we can't use that
06:22directly as direct numbers because we have to use degrees and we already
06:26created a calculation for the degrees up here remember we took the math pi dot
06:30pi and divided it by 180 so we could take that math degree and start off
06:34with basically saying that we want to go to the angle zero basically for the
06:39cosine and position X and we could do the same thing for Y only instead of
06:44using cosine we're going to use sine now obviously if we look at our sample right
06:50here theoretically really we would want to use the sine for our X because that
06:54would give us the right X position and we probably want to use the cosine
06:59well if to you if we use the cosine we're going to get this length which
07:03will give us the X if we use the sign we're going to get the height which is
07:07going to give us the Y so cosine will probably be better for our X let's just
07:12make sure that we're using cosine for our X and we are and we're using sine for
07:17our Y perfect so now we should find ourselves in degree zero which is
07:21perfect we're exactly in zero this is our zero position and we want to
07:25progress from there zero or 360 all right so the next step is I want to
07:30add some sort of enter frame and basically have it grow each time the
07:35degree changes so for that I'm just going to create an enter frame so I'm
07:42going to create here a nice enter frame on enter and create another private
07:52function and last but not least we want to start updating the position so I'm
08:00going to grab this sphere shape and basically we want to add a value of
08:07some sort each time so what I'm going to do right here is I'm going to create
08:10a new variable a new private variable and I'm going to call it moon degree DGR
08:17it's going to be a number and let's start off from zero and then we could
08:22adjust this moon degree by any wave form we want so let's say we want to add to
08:27that moon degree 3.2 degrees each time it moves all that we have left really
08:33is two things one is this number is going to grow and grow and grow it's
08:36not going to bother flash but it bothers me so what I want to do is I want
08:40to make sure that because a circle can't be bigger than 360 degrees I'm going to
08:45go to that moon degree and I'm going to make sure that I'm going to mold to
08:48equal 360 making sure that a value can't be bigger than 360 as soon as it grows
08:54beyond 360 will automatically be chopped down to the remainder of a division by
09:00360 and that's all we really had to do to get our moon starting to roam
09:05around our earth now obviously this could go in a lot of different
09:09directions and it could become a lot more interesting than it is right now and
09:12obviously I encourage you to try to figure out ways to make it more
09:15interesting you could change it instead of having an enter frame coming more
09:19quickly you could have it speed up more I would give you homework if you
09:23choose to do some homework is I would love for you to create a moon that has
09:27its own moon and just have the spheres basically cycle among themselves and try
09:31to give the shapes a little bit of meat you know give them a little bit of a
09:36shine like our globe has so that's that's all we have for this chapter and
09:41we have just one more video left which will cover immediately
