A quick note before we start: You will need the Vitalize plug-in for some of the examples in this article to work. You can get it by following this link:
http://www.clickteam.com/vitalize4/download.html
For regular Internet Explorer users, your computer will automatically ask if you would like to install Vitalize. All you need to do is say yes.

This will be a four part article, and each part will be fairly long, so be prepared for a lengthy read!

Previous Parts:

Part 1 - Going the Distance
http://create-games.com/article.asp?id=1965

Now that that's out of the way...

Trigonometry Part 2 - Give Me a Sine!

In the last article, we covered Pythagorean theorem, finding the distance between two objects, Hypotenuse, Opposite and Adjacent, and a few other little things like "impossible" numbers. Now this is all very handy, but often we want to know more than just the distance between the two objects! For instance, are the two objects looking at each other? If not, how do I make them look at each other? The answer my friend, is NOT to use 32 directions, and then always set one object to 'look' at the other. Have you ever wondered if there could be more than 32 directions? Well there are. In fact there are 360 directions, that means you're missing out on 328 of them!! We know a full circle is 360 degrees, but how can we relate this to Trigonometry? There is more than one possible answer to that question, one of which, is Sine.

A quick note on triangles, we already know which sides are which, but what does that even mean? Besides distances, you can also use those sides to calculate angles. You may have heard this before, and if not then it probably won't make much sense, which is good because it doesn't have to at this stage:

Sine (angle) = Opposite/Hypotenuse

Cosine (angle) = Adjacent/Hypotenuse

Tangent (angle) = Opposite/Adjacent

S O H - C A H - T O A

Today we'll just be covering Sine, so I won't go into much detail about the others.

Sohcahtoa? Is it some kind of rice biscuit? Is it a Cambodian Tribal chant? Is it the initials of each equation you see above, printed out in that order? Wait, that last one was right! You might not know what those equations do right now, but I can tell you that they are VERY VERY important! Obviously, it would be very confusing trying to remember each one of them off the top of your head, as you'd soon forget what they meant and start tearing holes in the space-time continuum with crazy equations like "Sine = Adjacent/Opposite", which of course it doesn't, although it looks similar. However, if you can think "SOHCAHTOA", then it's as easy to remember as apple pie! Thus, the Universe is safe and we can all sleep comfortably at night, knowing there's at least one person out there who's not destroying reality as we know it.


Angles, beautiful angles!

A few quick details about Sine; Sine is a function. It's not a number, it's a function used to work out a number, much like the "/" sign is a function to divide a number by the other. The Sine function is used in mathematics as "Sin" and should always be used this way. There is a mathematical process used to work out the "Sine" of an angle, but it is very very complex and would be a nuisance to type out every time we needed a Sine value. This is why we use the word "Sin" in maths, because the mathematical formula that is really going on in place of "Sin" is quite complicated, so it is just represented by the word "Sin". The next section explains the Sin in great detail, and it uses a lot of mathematical stuffs.

This section probably won't make much sense because the mechanics of Sin are very complicated, so I'll try and make it as simple as possible. Let's say that we have an angle called "x" and we want to find out the Sin of that angle. Here is the formula for working out the Sin:

sin x = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ...,

The "^" sign means "to the power of" (eg: 5^3 = 5 to the power of 3, which is 5*5*5). The "!" sign means "Factorial" (eg: 5! = 5 * 4 * 3 * 2 * 1 = 120).

If you're wondering what the "..." means at the end, it simply states that this equation is infinite! As you could probably guess, the next part of the equation would be "(x^9)/9!", and the equation continues like this forever, alternating between + and -, and never ending. The thing is, the longer you make this equation, the more accurate Sin will be.

Let's say we're trying to find the Sin of an angle that is 23 degrees. First of all, you have to convert 23 into "radians". To convert an angle into radians, first divide it by 180 and then multiply it by Pi. So now we have:

23/180*Pi

Which turns out to be: 0.4014257

Now that we have 23 in radians, we can use it in our equation above. Most calculators just use the first three "parts" of that equation, even though it is infinite, the first three alone will give a very accurate result. These equations are getting more and more complicated so I'll explain now that all I'm going to do with our equation above is replace all the X's with our number: 0.4014257

Here we go:

sin 23 = 0.4014257 - (0.4014257^3)/3! + (0.4014257^5)/5! - (0.4014257^7)/7!

What a mouthful! That equation can now be evaluated and the resulting answer will be the Sin of 23! Lucky for you, I'm just going to put the answer to that equation right here so you don't have to work it out:

sin 23 = 0.390731102008345

And that long number right there is the Sin value of 23 degrees! You may have noticed that this was a very long and complicated process. Imagine having to go through that every time you needed to find the Sin of an angle! It would mean a whole lotta messy code which leads to a whole lotta bugs in your application, also I'm not sure if MMF2 can handle the Factorial "!" function.

And that is why we use the word "Sin". Sin will simply do all of that powering and factorialing and radiarizing (not real words), to whatever angle we put inside it's brackets. For example, in MMF2:

Sin(23)

Will give us 0.390731102008345, just like that.

However, there is a much simpler method that can only be used when referring to right angled triangles. This is where our "Sine (angle) = Opposite/Hypotenuse" equation comes in.

Imagine a right angle triangle (I know, it's hard but just bear with me), now let's say that the vertical side is the "Opposite", obviously the longest side will be the Hypotenuse and the other side will be the Adjacent. If you don't believe me, have a picture!

Image

Here's where Sine comes in. Sine can be used to find out Angle X, even when we don't know what Angle X is!

And here's how:

First of all, we remember what SOH means: Sine (angle) = Opposite/Hypotenuse.

To do this calculation, we need to know the lengths of both the hypotenuse and the opposite sides. For this example, the hypotenuse will be 5 pixels, and the opposite will be 3 pixels. Our angle will be called X.

Now we know the lengths, we can put them into our equation:

Sin X = 3/5

3/5 = 0.6

Therefore, Sin X = 0.6

The thing about Sin is that the Hypotenuse will always be LONGER than the Opposite, so whenever we go "Opposite/Hypotenuse", the answer will be BETWEEN -1 and 1. Every Sin for every number in this universe is between -1 and 1! I will explain more about this later on. For now, we know that the Sin value of our unknown angle is 0.6. So how do we find out the angle if all we know is the Sin of it?

The answer is simply: ArcSine

"ArcSine" is known as the Inverse Sine Function. As it's name states, this function will just do the exact opposite of the Sine function. It will get a number between 1 and -1, then do all those lengthy calculations backwards and spit out a degree. Using our example from way up above:

ArcSine(0.390731102008345) = 23 degrees.

Remember, Sine will give us a number between 1 and -1 once we give it an angle, ArcSine will give us an angle once we give it a number between 1 and -1. In maths, ArcSine is represented by the word Sin with a little "-1" symbol in the top-right corner. This is very misleading as it looks like "Sin to the power of -1" but in fact doesn't mean that at all. So we will just refer to it as ArcSine, however I will show you the method of using it in MMF2 in the next Part, as MMF2 doesn't know what the word "ArcSine" means.

Back to our triangle! Remember when our hypotenuse was 5 and our opposite was 3, we discovered that the Sin of angle X was 0.6. Now that we know about ArcSine, we can find out angle X!!!

Here goes:

ArcSine(0.6) = 36.869896

And there we have it! We've just worked out that angle X is 36.869896 degrees, and all we needed to know were the lengths of the Opposite and the Hypotenuse!

And now, I'd like to show you a visual example of Sine. Imagine a circle just like this:

Image

This circle has a radius of 1, as you can plainly see. Now I want you to imagine that we put a triangle inside this circle, exactly like this:

Image

The BLUE line represents the hypotenuse and together with the base of the triangle, it forms an angle! The angle in this picture is 50 degrees. The RED line represents the opposite, but it represents something else as well. Believe it or not, the height of the RED line is the Sin value of 50 degrees! Of course, we know that a sine value will be between 1 and -1, so this picture is on a large scale. The red dot on the vertical line shows where the RED line hits the outside of the circle, forming the point on our delicious triangle. This red dot also shows us the value of Sin(50), which is very close to the number one. In fact Sin(50) = 0.766044, which looks about right! And so this is how Sine is connected to circles. As the angle gets bigger (Blue line moves anti-clockwise), the red line will get higher and higher until it reaches the top of the circle, where the angle is 90 degrees. Sin(90) = exactly 1, but then as the angle gets even bigger and the blue line starts moving down again, the red line also starts getting shorter!

Here's a little Vitalized application that I whipped up to show you what happens when the angle goes over 90. The skinny red line is the angle, the thick red line represents Sin, and the thin green line will 'point' to the value of Sin on the centre-line. First of all, just play around with dragging the blue dot around the circle and watch what happens to the triangle and the value of Sin.



Some people instantly think that since Sin(90) = 1, then Sin(45) must equal 0.5, because 45 is half of 90. This isn't the case! Try setting the angle to 30 degrees!

From now on, I will call the horizontal centre-line the "X axis" and the vertical centre-line the "Y axis".

Let's look at the Draw Sinewave button. Setting the Angular Velocity to 50 will draw a pretty decent looking Sinewave . So what is a Sinewave?

There's a small point that constantly moves along the X axis at the same speed. The thing is, it's being moved up and down according to the value of Sin! Since the angle is always increasing, this creates a wave-like pattern, hence the name "Sinewave". We won't go into detail about "Angular Velocity", so for now it just means "how quickly the red dot moves along the X axis". Take your time and play with the application a bit, until you FULLY understand the basic principles of Sin.


Conclusion

And so, this brings us to the end of yet another article.

This one was a lot harder than the last one, and I'm telling you now that they're only going to get harder. However, once you have a basic understanding of Sin, understanding Cosine and Tangent are going to be much easier I assure you! You can re-read this article over and over if you like, until the point where you won't be able to say anything but "Sin" and you'll start having hallucinations of giant Sinewaves coming out of anything round.

Please ask any questions you might have about this article, and if you think I've missed something out, please let me know!

Part 3 is under construction but there will be a link to it here at a later stage.