Note: I will refer to angles throughout, so if you are a visual type person having this open to the side could help.
So why should you bother doing this math stuff?
Well, because it will make your game / animation / project look that much better. It will allow for a new kind of user interaction where you can calculate many more things based on where the user's mouse is in relation to other elements. You can use this trigonomerty to calculate and set other elements of your project accordingly. In the end it will make your project look more professional.
First things first, its back to Freshman year math. Everybody remember all those different types of triangles on the chalkboard? Well I do and its time to review all of the concepts before converting them into some handy Flash scripts.
There are a number of "special" triangles. The one we will focus on right now is the right triangle. This means that one of the angles in the triangle is 90 degrees (angle c) and that we can use sin cosine and tangent to convert from the lengths of sides to the degrees of angles and vice versa. SOHCAHTOA. Sacagawea huh? Itís actually an acronym to remind you how to use sin cosine and tangent. But first, if you donít remember all of the terms we need a quick review. The hypotenuse is always the side across from the right angle (angle c). The hypotenuse is also always the longest side. All of the names of the sides are in relation to which angle you choose. In the diagram above, I picked angle b. That means side A would be the adjacent side and side B would be the opposite side. Now back to SOHCAHTOA. It stands for: Sin equal Opposite over Hypotenuse. Cosine equals Adjacent over Hypotenuse. Tangent equals Opposite over Adjacent. Converting this into symbols would mean: sin(a) = A/C sin(b) = B/C cos(a) = B/C cos(b) = C/A tan(a) = A/B tan(b) = B/A. (Note we donít use angle c because we would already know that itís 90 degrees.)
With that out of the way there is only one thing left to worry about before starting in with Flash and thatís radians. So whatís a radian? Well it assigns the value of 2(PI) to an entire circle (instead of 360 degrees). This means 57.3 degrees = 1 radian and 90 degrees = (PI)/2 radians. Why in the world would they want to do a crazy thing like that? Well because when dealing with circles or advanced calculus and things, the common practice is to use radians, so we are stuck with them for now. The up side is that there is an easy way to convert back and forth from radians to degrees. 1 degree * (PI)/180 = 1 radian. 1 radian * 180/(PI) = 1 degree.
Flash Trigonometry Functions
Whew, with all that out of the way, we can finally start into Flash. Flash has 6 (technically 7 but we will get to the special one later) built in trigonometry functions: Math.sin() Math.cos() Math.tan() Math.asin() Math.acos() Math.atan(). There is also the constant Math.PI, which we can use instead of typing 3.1415926535Ö The catch is as I said before they all use radians which means we would have to do something like: Math.sin(#degrees * (Math.PI / 180)). We could say that this angle is equal to the opposite over the hypotenuse and then do some basic algebra to solve for one of the sides.
Example: Hypotenuse is 5 units long. Angle a is 30 degrees. We want to calculate the length of side B we would use: sideB = Math.sin(30 * (Math.PI/180)) * 5; This would give us an answer of 2.5 units. We could also do sideA = Math.cos(30 * (Math.PI/180)) * 5; which would give us an answer of .866 or the square root of three over two.
Example2: Hypotenuse is 5 units long. Side A is 2.5 units long. We want to calculate the length of an angle. We will need to use one of the inverse trigonometric functions (asin acos atan). It would look something like angleA = Math.asin(2.5/5) * (180/Math.PI); We can an answer of 30 degrees. We could also do angleB = Math.acos(2.5/5) * (180/Math.PI); to get an answer of 60 degrees.
Okay, now you know how to do these basic trigonometric calculations in Flash, but what are they good for? Well they are used in the overhead views of racecar games for example. You use the rotation of the car as one of your angles. You also use the speed you want it to go as the hypotenuse and then you can break that apart into x and y vectors using a basic right triangle.
So what if your triangle doesnít have a convenient 90 degree angle? Well then you can try one of the two trigonometric laws:
Law of Sin
The law of sin states that: A/sin(a) = B/sin(b) = C/sin(c). This could be used in a convoluted way if we are trying to create a "smart" AI for a pong game. So instead of having the paddle adjust to just whatever the current y value is, it will calculate where the ball is going to end up. Note that you would also have to use two regular triangles as well as the law of sin. If you want more information on who this would work you can ask.
Law of Cosine
The law of cos is a little more complicated. A^2 = B^2 + C^2 - 2*B*C*cos(a). When you translate this into Flash make sure you use Math.pow(num, 2) for all of the squaring. This could be used to find an angle when all you know is lengths of sides. Combine this with the distance formula and you have a powerful way to determine the rotation of something. It could be used in a complex platformer to allow for sloped ground.
This isn't trigonometry as much as it is geometry, but I referred to it above so I thought I would include it. D^2 = (x2 - x1)^2 + (y2 - y2)^2. This can calculate the length of the line that goes straight from one point to another. It is based on the pythagorean theorem. There are countless possible uses for this one such as detecting the length of the distance between the mouse and a point to determine the power of a launch or something. Make sure you use Math.pow() and Math.sqrt() when using this one.
Going back to the original example I posted, the Theorem states that C^2 = A^2 + B^2. If you know two of the lengths of a right triangle, you can use this to determine the length of the third side. You can use this in combination with any of the afore mentioned methods of finding angles to make them even more powerful.
First time with max characters exactl