﻿ Game Programming Gurus Free JavaScript Editor     Ajax Editor ﻿

Main Page

### Trigonometry

Trigonometry is the study of angles, shapes, and their relationships. Most trigonometry is based on the analysis of a right triangle, as shown in Figure C.1.

##### Figure C.1. The right triangle. Table C.1 lists the radian/degree values.

##### Table C.1. Radians vs. Degrees
360 degrees = 2*PI radians is approximately 6.28 radians
180 degrees = PI radians is approximately 3.14159 radians
360 degrees = 1 radian is approximately 57.296 degrees
2*PI radians
2*PI radians = 1 degree is approximately 0.0175 radians
360 degrees

Here are some trigonometric facts:

Fact 1: There are 360 degrees in a complete circle, or 2*PI radians. Hence, there are PI radians in 180 degrees. The computer functions sin() and cos() work in radians, not degrees—remember that! Refer to Table C.1 for a list of common angles in both systems.

Fact 2: The sum of the interior angles theta1 + theta2 + theta3 = 180 degrees or PI radians.

Fact 3: Referring to the right triangle in Figure C.1, the side opposite theta1 is called the opposite side, the side below it is called the adjacent side, and the long side is called the hypotenuse.

Fact 4: The sum of the squares of the sides of a right triangle equals the square of the hypotenuse. This is called the Pythagorean theorem. Mathematically, it's written like this:

```hypotenuse2 = adjacent2 + opposite2
```

Or sometimes it's written using a, b, and c for dummy variables:

```c2 = a2 + b2
```

Therefore, if you know two sides of a triangle, you can find the third.

Fact 5: There are three main trigonometric ratios that mathematicians like to use: sine, cosine, and tangent. They are defined as ```DOMAIN: 0 <= theta <= 2*PI
RANGE: -1 to 1
``` ```DOMAIN: 0 <= theta <= 2*PI
RANGE: -1 to 1
``` ```DOMAIN: -PI/2 <= theta <= PI/2
RANGE: -infinity to +infinity
```

Figure C.2 shows graphs of all the functions. Notice that they're all periodic (repeating) and that sin(theta) and cos(theta) have periodicity of 2*PI, while tangent has periodicity of PI. Also, notice that tan(theta) goes to +-infinity whenever theta mod PI is PI/2.

##### Figure C.2. Graphs of basic trigonometric functions. NOTE

You may note the use of the terms domain and range. They simply mean the input and the output, respectively.

Now, there are about a gazillion trigonometric identities and tricks, and it would take a math book to prove them all. I'm just going to show you the ones that a game programmer should know. Table C.2 lists some trigonometric ratios as well as some neat identities.

##### Table C.2. Useful Trigonometric Identities
Cosecant: csc(theta) = 1/sin(theta)
Secant: sec(theta) = 1/cos(theta)
Cotangent: cot(theta) = 1/tan(theta)
Pythagorean theorem in terms of trig functions:
sin(theta)2 + cos(theta)2 = 1
Conversion identity:
sin(theta1) = cos(theta1 – PI/2)
Reflection identities:
sin(-theta) = -sin(theta)
cos(-theta) = cos(theta)
Addition identities:
sin(theta1 + theta2) = sin(theta1)*cos(theta2) + cos(theta1)*sin(theta2)
cos(theta1 + theta2) = cos(theta1)*cos(theta2) – sin(theta1)*sin(theta2)
sin(theta1 – theta2) = sin(theta1)*cos(theta2) – cos(theta1)*sin(theta2)
cos(theta1 – theta2) = cos(theta1)*cos(theta2) + sin(theta1)*sin(theta2)

Of course, you could derive identities until you turned many shades of green. In general, identities help you simplify complex trigonometric formulas into simpler ones so you don't have to do the math. Hence, when you come up with an algorithm based on sin, cos, tan, and so on, always take a look in a trigonometry book to see whether you can simplify your math so that fewer computations are needed to get to the result. Remember: speed, speed, speed!!!

﻿
Bitcoin Dice - Crypto Casino Ajax Editor     JavaScript Editor