|
Author
|
||||
|
Related articles
|
||||
|
More in Development
|
||||
This article originally appeared in Dev.Mag Issue 12, released in July 2007
Previously...
Last issue we covered the core principles of trigonometry and learned how to use these to calculate unknown sides in a triangle. Well, I hope you’re rested and ready Commander, because we’ll be jumping straight back in with another essential technique - calculating unknown angles.
Angling 101
As always, let’s start with a triangle. This one has two known sides (the hypotenuse and one other), but the angle, theta (θ), is unknown. Bummer. Let’s fix that.
Okay, let’s use our super trig techniques from last time. Last time we learned to solve sides by substituting into the magic formula:
"But, hold on", you might think, "we’re not solving for an unknown side this time!" You’re absolutely right. When solving for angles we need to perform a little tweak to the formula, but the core principle remains the same. Watch and be amazed:
Still looks familiar, right? Let’s see how it works. Since we’re solving for θ, we’ll name our sides relative to that angle in the usual way. This leaves us with h equal to 9 units, and a equal to 3 units. Now we need to set our ratio. As you can see in our adjusted formula, we’ll be using our known sides for this. However, if you study the ratios from last month, you’ll realize that there are two possibilities for the ratio depending on which side of the division line we choose to put our numbers. For instance, if we choose:
we would use cos(θ) as our ratio. However, if we choose:
our ratio would be sec(θ). The choice is really up to you, but since cos is generally the easiest and most frequently used ratio, that’s the one I’ll be working with. While we’re at it, I’ll plug the numbers in as well.
Now we need to solve for π. That means that we’ll have to cancel cos out to get θ on its own. For this, we’ll use the [math ]arccos(θ) trig function.
Huh?
Inverted (arc) functions are essentially the antimatter of the trigonometry world. By applying the inverted version of a function (arcsin, arccos, arctan, etc), we can cancel it out to obtain an angle on its own. However, in accordance with the algebraic method, this means that we also have to apply it to the numbers on the right hand side of the equation. As a result, our example will play out a little something like this:
Note: In the Windows Calculator, using inverted functions is as easy as selecting the "Inv" checkbox in the upper left of the screen before clicking a function button.
There’s our answer! Angle θ is roughly 70.5 degrees.
By now you should have a fairly good idea of how to calculate both unknown sides and unknown angles in a triangle. Soon you’ll be ready to learn how to apply all of this to your games, but first...
Degrees and Radians
Before we begin with game applications, one last tidbit of essential information that will make or break your ability to use trig in your games - are you aware that there are two units of measure for angles? Most people are taught to measure angles in degrees, and for this reason I’ve been using degrees as the angular unit in all of the calculations so far. Now, while there’s technically nothing wrong with degrees as a unit of measure, radians are considered to be more mathematically correct. For this reason they are the primary unit of angular measurement in science and engineering. Luckily, most decent programming languages have trig functions built into them by default. Unluckily, they mostly operate using angles measured in radians. Because of this, you’ll need to be able to convert between the two.
Radians are not difficult to understand. Degrees are based on one full rotation being measurable in 360 units. Radians are based on one full rotation being measurable in 2π (π ≈ 3.142 ∴ 2π ≈ 6.283) units. Therefore, conversion is just a matter of looking at the angles proportionally. Don’t panic! It’s as easy as using these two formulae:
Game Maker saves you from all this mathematical nastiness by compressing all of it into two simple functions: degtorad(angle) and radtodeg(angle). Just be sure that you plug the right measurement into the right formula!
|
Words from the readers
|
||||
|
The pictures numbers does not correspond with the figures provided in the text of the 1st page of this article, adjacent in picture is shown as 4 on the picture but used as 3 in the text.
Posted by Scipher at 08:07:01 on 07 April 2009
Have your say:
|
||||