Trigonometry

From ZDoom Wiki
Jump to navigation Jump to search

Trigonometry for the Unwashed Masses, Because It's Middle School Material and It's Not Hard I Mean Seriously People Come On

A lesson by Zippy


A triangle is a 3-sided polygon. If there aren't 3 sides or it is not a closed polygon, then it is not a triangle.

A triangle only has 3 internal angles (since it only has 3 sides). These angles will always add up to 180 degrees. If they don't, then it is not a triangle.

Each internal angle has 2 adjacent sides (the two sides that make up the angle) and 1 opposite side (the side that is not part of the angle). The angles and their opposite sides have a relationship such that the larger the angle, the larger the opposite side (relative to the other sides). This makes sense if you picture a triangle with the large internal angle: the larger the angle the further apart the adjacent sides are, therefore the opposite side must be longer to connect the two. In other words, if you know all 3 angles, you also know the relative sizes of all 3 opposites: 30 degrees, 90 degrees, 60 degrees = smallest opposite, largest opposite, middle opposite.

A right triangle is a triangle where one of the internal angles is 90 degrees, known as a right angle. That is, 2 of the sides are perpendicular (like an "L").

The 90 degree angle in a right triangle is always the largest angle (180 degrees total - 90 degrees right angle = 90 degrees left to split between the last two angles). If the 90 degrees angle is the largest angle, it immediately follows that its opposite side is also the largest side of the right triangle. This side is known as the hypotenuse. So remember: hypotenuse = largest side of a right triangle, always opposite the right angle.

The 2 important things we're going to cover now are the Pythagorean Theorem and Sines, Cosines, and Tangents.

Pythagorean Theorem

The Pythagorean Theorem states that, for a right triangle, the length of the hypotenuse squared is equal to the length of both sides squared. That is:

a = length of side 1
b = length of side 2
c = length of hypotenuse

a^2 + b^2 = c^2

To solve for any side:
a = SqrRoot( c^2 - b^2 )
b = SqrRoot( c^2 - a^2 )
c = SqrRoot( a^2 + b^2 )

For example, if you know the difference in the X coordinates and Y coordinates of 2 actors in the level, you can compute the distance between the two with this theorem.

1 = ACTOR 1
2 = ACTOR 2

      2
      |
      |
      |
      |
 1----+
1 is at X = 25 and Y = 20
2 is at X = 45 and Y = 60
The difference in X is 20 (larger minus smaller, 45 - 25 = 20) and is how far apart the two actors are in the X plane.
The difference in Y is 40 (larger minus smaller, 60 - 20 = 40) and is how far apart the two actors are in the Y plane.
If you look at the diagram, you see the X distance and the Y distance form a RIGHT TRIANGLE.
Therefore, the Pythagorean Theorem can be used to compute the hypotenuse.  In this case, it is the distance between the 2 actors.

      2
     /|
    / |
   /  |
  /   |
 1----+
a^2 + b^2 = c^2
20^2 + 40^2 = c^2
400 + 1600 = c^2
2000 = c^2
SqrRoot(2000) = c
44.721 = c (rounded)

Sines, Cosines, and Tangents

The sides and angles of right triangles have special relations that allow for easy computations with the Sine (Sin), Cosine (Cos), and Tangent (Tan) functions:

SohCahToa

Soh - Sin is opposite over hypotenuse
Cah - Cos is adjacent over hypotenuse
Toa - Tan is opposite over adjacent

The functions describe the ratios between sides for different angles of a right triangles.

For any angle in a RIGHT TRIANGLE
Sin(angle) = lengthOfOppositeSide / lengthOfHypotenuse
Cos(angle) = lengthOfAdjacentSide / lengthOfHypotenuse
Tan(angle) = lengthOfOppositeSide / lengthOfAdjacentSide

Note that because some of the ratios rely explicitly on the hypotenuse, the equations are not quite as useful when the given angle is the right angle (e.g. for a right angle, the opposite side is the hypotenuse, so Sin(90 degrees) = 1). With these, you can figure out the lengths of sides of a right triangle given one of the (non-right) angles.

Using this knowledge, you can "thrust" the player in a given direction based on his arbitrary angle. Right triangles enter the problem as so: Thrusting the player once at an angle of 30 degrees (northeast) with arbitrary force of 15 is functionally equivalent to thrusting the player twice with lesser forces: once at 0 degrees (due east) and once at 90 degrees (due north). That is, after doing either of these operations (one angled thrust OR two direct thrusts), all things being equal, the player will wind up in the same position. In other words, the angled thrust has an X and Y component to it. In order to fake a thrust, the author needs to know these components, since he can't tell ZDoom to reposition the player at an angle.

If you take the X and Y components and line them up, you notice that they make a right angle (due east vs. due north), as they should with one being solely in the X direction and the other being solely in the Y. If they form a right angle, the original angled thrust can be placed between the two to complete the right triangle. We can now use Sin and Cos to figure out the X and Y components, since we know the angle and "force" of the intended "thrust":

P = Player
D = Destination
a = Known angle
      D
     /|
    / |
   /  | y
  /a  |
 P----+
   x

angle = 30
force = 15 // The hypotenuse

Sin(angle) = opposite / hypotenuse  // opposite is y component of force
Sin(30) = opposite / 15
Sin(30) * 15 = opposite
0.5 * 15 = opposite
7.5 = opposite

Cos(angle) = adjacent / hypotenuse  // adjacent is x component of force
Cos(30) = adjacent / 15
Cos(30) * 15 = adjacent
0.866 * 15 = adjacent
13 = adjacent (rounded)

Piece of cake, right? Now does this all make sense? The 3 sides of the right triangle are 15 (the hypotenuse), 7.5 (y-side), and 13 (x-side). 15, the largest side, is opposite the right angle (90 degrees), the largest angle. Correct. 7.5, the smallest side, is opposite the 30 degree angle, the smallest angle. Correct. 13, the middle-sized side, is opposite the last remaining angle. Is that right?

180 degrees = 90 degrees + 30 degrees + X degrees
180 - 90 - 30 = X
90 - 30 = X
60 = X

60 degrees: smaller than 90 but larger than 30. Correct.

It couldn't be any simpler.

You can make use of these properties anytime you are dealing with right triangles. 2 lines come together at 90 degrees? That's a right angle, which means you can make a right triangle. A line pointing in an arbitrary direction? Break it down into X and Y components and you have a right triangle.

So that's Trigonometry basics in a nutshell.


Tutorial provided by Zippy