Trigonometry: Part 1
Page 1 of 2
<1 | 2>
Single-page view
Trigonometry: Part 1

This article originally appeared in Dev.Mag Issue 14, released in June 2007

Trigonometry. The mere mention of it strikes terror into the minds of bewildered high school students. Confusing ratios, bizarre diagrams, trying to find the sine of the cosine of the tangent of theta - many people struggle to wrap their minds around it and eventually give up in desperation to do something easier, such as ending war or achieving cold fusion.

Well, despite what your high school teachers may have said, trigonometry can actually be useful! Yes, in the vast sea of unintelligible terms is a valuable and powerful mathematical tool that can aid you immensely in the humble task of game design. The aim of this series is to explain to you just how simple trigonometry really is, and how you can use it to solve some very common programming problems. Hopefully it will make the job of coding your next blockbuster game just that little bit easier.

First Principles -The Triangle:

Before we start writing any trig in our code, it’s necessary to understand all the nasty mathematical stuff behind it (just joking, it’s not that bad!). First, we should take a look at how trigonometry works at the most basic level. To do that, we’ll need a triangle. More specifically, we’ll need a right-angled triangle (a triangle that contains a single 90 degree corner). Here’s one I found lying around:

A boring right-angled triangle
A boring right-angled triangle

Firstly, it’s important that we name the sides of the triangle so that we can identify them when we build our equations later. In trig, we name sides based on their relationships to the corners of the triangle. Take this triangle as an example:

Boring right-angled triangle with angle theta
Boring right-angled triangle with angle theta

In this triangle, we’ve defined one of the non-right-angles with the variable theta (θ). For now, this is simply a variable to identify the corner - don’t worry about about how the numbers fit in just yet!

Now that we’ve defined θ, we can use it to identify our sides.

Right-angled triangle with angles and sides defined
Right-angled triangle with angles and sides defined

Now we’ve designated our sides. In trig, sides are always designated relative to the angle that we’re working with, θ in this case. o is the side opposite to the specified angle. a is the angle adjacent (next to) the specified angle. h is the hypotenuse, which is always the side opposite to the 90 degree angle. Clear? Well, if not, let’s go through the same exercise with the other angle in the triangle. Let’s designate that one Alpha (α), and take a look at how this affects our sides.

Right-angled triangle with angle alpha, and sides
Right-angled triangle with angle alpha, and sides

Note that this triangle is still the same triangle. We’ve just renamed the sides according to which corner we’re viewing them relative to, in exactly the same way that we did with angle θ. You’ll note that h is still assigned to the same side. As I mentioned, this is because the hypotenuse is always the side opposite to the 90 degree angle, and so won’t change if we switch corners. If this still seems a little confusing, don’t worry - next we’ll be seeing how all of this ties together!



Words from the readers
Aha, indeed, looks like some old articles still had this problem. All is well now.
Posted by Chippit at 17:09:53 on 07 May 2009
Apostrophes don't show up correctly in the article (I just see HTML codes, in Chrome, at least) a bug? - ht
Posted by Herman at 12:35:00 on 07 May 2009
I didn't know they had a game design course. I'll check it out. Thanks
Posted by GameFreak at 23:30:32 on 05 May 2009
As it so happens, the University of Cape Town recently started offering a course in Computer Game Design. It's probably the best you can get in the country, if you're heading in that direction.
Posted by Chippit at 23:08:11 on 05 May 2009
Im in matric, and will soon be studying to do game design. Thanks for tutorial, this really helps me cause it explains better than what my teacher does. Also do you guys know of where the best places are to study for game design, preferably close to or in Cape town.
Posted by GameFreak at 22:39:01 on 05 May 2009
Yeah, y'know, we really should've noticed that way back then. We suck.
Posted by Chippit at 16:12:50 on 07 April 2009
Take note, dear readers. When us writers mess up, justice is swift and painful, even when the article was written two years ago.

T_T
Posted by Gazza_N at 15:58:55 on 07 April 2009
You are quite correct. 4.243 is the correct value for both sides. We've punished Gazza_N by rescinding his daily rations for today. And tomorrow. All this despite the fact that I should've spotted that error myself. :P
Posted by Chippit at 15:18:12 on 07 April 2009
Thanks for the awesome tutorial, please just fix the value produced when Solving for o o should actually be 4.423 the same as the value you nominate when using cos as the function and solving for a, the sides are identical in length aswell since the two acute angles are 45 degrees. I believe it was just a typo, thanks for the article it was very informative and actually helped me understand the basics which my teachers forgot to tell me!!!! That the functions are RELATIVE to the angle you are working from!
Posted by Scipher at 07:43:11 on 07 April 2009
ARGH! *Q-Man has flashbacks of high school and maths*; to be fair, I was asleep most of the time, so flashbacks wouldn't really work for me. Still - maths D: Rather you than me!
Posted by Quinton at 00:57:00 on 02 April 2009
Have your say: