OH_Mathematics for game creators 2026_voorbeeldhoofdstuk
Chapter 2 Trigonometry
Trigonometry deals with the calculation of angles and distances within triangles.
Most of the concepts are derived from the definitions of sine, cosine and tangent in the unit circle. It is, therefore, very important to see the link between the trigonometry concepts and the concepts of the unit circle.
We will start with the definition of a right triangle and continue with the relationship between any right triangle and a triangle within the unit circle.
2.1 Trigonometry
Given a right triangle defined by points A, B, and C. An important point to make is that you will label the sides as seen from point A.
As visualized by the protractor, the angle at point C in this triangle equals 90°
• hypotenuse: this is the longest side in a right triangle. The angles that are connected to this side are always smaller than 90°.
• opposite: this side is opposite to point A
• adjacent: this side is adjacent to point A. A right triangle is defined as a triangle where one of the angles is 90°, as shown in the picture where the angle ɣ is 90°. The sum of angles in a triangle is always 180°. Chapter 2 Trigonometry
2.2 Sum of Angles
It is easy to claim that the sum of angles in any triangle is 180°. However, you can create visual proof of this statement:
The triangle shown here does not have a right angle.
As a first step you extend the segment AC, because the trick is to try to move all the angles around point A
Next, you create a line parallel to segment BC and paste it onto point A Thus, you get a new segment B’A
As a final step, you can find the angle β between the new angle ɣ and the original angle α. You can now see that the sum of these angles equals 180°.
2.3 Pythagoras
The Pythagorean formula in a right triangle is the well known formula:
a 2 + b 2 = c 2
Where
• c is the hypotenuse or the longest side in a right triangle.
• a and b are the short sides of the triangle.
This formula is frequently used in game development to calculate distances between points and to calculate the length of a vector.
2.4 Pythagoras Proof
Proof
There is a nice visual proof of the Pythagorean formula. You start again with a right triangle with sides a, b, and c
The hypotenuse is c and the angle ɣ equals 90°.
You can now create a square where all the lengths are equal to a + b.
As a next step you create four copies of this triangle. Because of the way the triangles are arranged there is an empty spot in the centre with an area of exactly c 2
You can now rearrange two of the triangles and the segments above to show another way to calculate the empty area which is: a 2 + b 2 .
Since you did not create an additional empty area, the two empty areas must be equal and you have now proven the Pythagorean formula: a 2 + b 2 = c 2 .
2.5 Right Triangle Formulas
To derive the formulas for a right triangle we take another look at the unit circle:
You start with a point P on the unit circle, which defines an angle on the unit circle. The segment c has a length of 1 because P is a point on the unit circle.
Next you project downwards from point P to find the cosine of the angle α . This projection makes a right angle with the horizontal axis, visualized by angle ɣ.
Next you project horizontally on the vertical axis to find the sine of the angle α
Finally, you move this last segment to the right to create a triangle within the unit circle. Because the cosine and sine functions work with perpendicular projections, the lengths of side a and side b equals the cosine and sine of the angle α
In a right triangle where the hypotenuse has a length of 1 the sides of the triangle correspond to |cos (α)| and |sin(α)|. Within the unit circle it is easy to discern which one of the two sides is the cosine of the angle and which one is the sine of the angle. However, if the triangle is rotated, you need to carefully consider which side is the adjacent, and hence the cosine of the angle, and which side is opposite, and hence the sine of the angle.
2.6 Scaling up the Unit Circle
Compared to the unit circle in the previous section you can now apply a scaling transform on the triangle.
The scaling transform multiplies all the sides of the triangle with the same number. In game development this is typically referred to as a uniform scale. An important property of this transform is that the angles within the triangles will remain the same.
In the following figure the scale that you apply is the radius r which is equal to 3. c = r = 3
P c = r = 3
P
The segment c has a length of 3 because P is a point on a circle with radius = 3 The right triangle’s other two sides are also scaled up by a factor of r = 3 in this figure. c = r = 3
γ P
= c cos (α)
= c sin ( α )
Consider again that if you start from the centre of the circle in the figure, we can label the sides of the triangle as follows:
• a: adjacent side
• b: opposite side
• c: the hypotenuse. This side is also equal to the radius r in this circle.
In any right triangle you can then state: • a = c: cos (α) ⇒ cos (α) =
b = c: sin(α) ⇒ sin(α) =
= ) ) = ) ) = tan(α)
2.7 Right Triangle Formulas
When confronted with a right triangle problem it is very important to label the sides of the triangle correctly. In the following figure we look at the problem from the perspective of point A. =hypotenuse = adjacent = opposi
You can use the following thought process to label the sides of the right triangle:
• Starting with the sides that are connected to point A the longest side will always be the hypotenuse or the segment c.
• The other side that is connected to point A can only be segment a or the adjacent side.
• Finally, the side that is not connected to point A must be segment b or the opposite side.
The right triangle formulas for the sine, cosine and tangent of the angle α are:
2.8 Polar Coordinates
An interesting application of sine and cosine is that they allow you to define a point with an angle and a radius.
On the unit circle
The sine and cosine of an angle can now be used to calculate a 2D coordinate.
For an angle 0.7854 in radians or 45°, the value of P1 = (0.7071 0.7071).
You can calculate this point with the following formula:
P1 = (cos(α) sin(α)).
With the actual value of α filled in, the formula is:
P1 = ( cos(0.7854) sin(0.7854) )
Note: In most programming languages, the convention is to use radians for calculations. It is a common pitfall to use the incorrect unit with trigonometric functions.
2.9 Breaking out of the Unit Circle
Polar coordinates
Now you can use the cosine and sine of an angle together with a radius to calculate a point on an arbitrary circle.
For an angle 1.0472 in radians or 60°, and a radius of 2.5, the value of P1 = (1.25 2.1651).
y
sin ( α )
1 R cos (α) R sin ( α )
Px
cos (α)
You can calculate this point with the following formula:
P1 = (R cos(α), (R sin(α )).
With the actual value of α filled in, the formula is:
P1 = (2.5 cos(1.0472), 2.5 sin(1.0472 ))
Note: in most programming languages the radians value is used for calculations, and it is important to get used to this radians unit.
2.10 Arc Sine
The inverse trigonometric formulas project a point from an axis back onto the circle. For the inverse sine formula (arcsin), a value is projected from the vertical axis.
We start with a point A on the vertical axis. The y-position of point P has to lie within the unit circle on the vertical axis, which means that the maximum is 1 and the minimum is –1. The y -position is 0.5 in this example.
Point A is then projected to the right to find the intersection with the unit circle, resulting in the angle α1 with a value of 30°. Notice how this angle varies between 90° and –90°. There is still another way to project this point, do you know which?
If we can project to the right, we can also project to the left, resulting in the angle α2 with a value of 150°
We can conclude that the formula arcsin( y) leads to two equally valid results, namely α1 and α2 . In this case: arcsin( y) = arcsin( y) = 30° or 150° .
2.11 Arc Cosine
The inverse trigonometric formulas project a point from an axis back onto the circle. For the inverse cosine formula (arccos), a value is projected from the horizontal axis.
We start with a point B on the horizontal axis. The x-position of point P has to lie on the horizontal axis within the unit circle, which means that the maximum is 1 and the minimum is –1.
Point B is then projected upwards to find the intersection with the unit circle, giving the angle α1 with a value of 60°
Notice how this angle varies between 0° and 180°.
If we can project upwards, we can also project downwards, giving the angle α2 with a value of – 60°
Notice how this angle is the negative value of α1.
We can conclude that the formula arccos (x) leads to two equally valid results, namely α1 and α2 .
In this case: arccos (x) = 60° or – 60°.
2.12 Arctangent
The arctangent is a frequently used function in game development, as it can be used to calculate an angle when you have a horizontal and a vertical distance. However, there is one important consideration: the formula is a ratio (division) of the two short sides of the triangle, with the adjacent side in the numerator:
When the adjacent side is zero, the tan(α) is undefined and it is also not possible to calculate the arctangent
You might wonder if a similar problem occurs for the sine and cosine functions. Indeed, there is, because the hypotenuse is in the numerator. This is only a problem if both the adjacent and opposite sides are equal to zero
2.13 Inverse Formulas – Atan2
Most programming environments avoid the danger of the arctan function with a function called atan2 . This function is defined as follows:
= atan2 (opposite, adjacent)
This function checks for division by zero and returns a correct value between –π and π or –180° and 180°.
2.14 Non Right Triangles
The right triangle that we discussed so far is special, because one of the angles in the triangle is 90°. You will encounter other triangles as well, so it is important to showcase how to solve acute or obtuse triangles.
We start with an acute triangle defined by points A, B, and C, which are given. All of the angles in this triangle are smaller than 90°.
We divide the triangle into two right triangles by projecting point C on the horizontal axis, resulting in point D.
This results in two right triangles △ AC D and △ BC D, with a side h whose height is still unknown
You can work out the solution by trying to find a formula for x . As a first step we write down the Pythagorean formula for both triangles:
△ ∶ 2 + ℎ2 = 2
△ ∶ ( )2 + ℎ2 = 2
Because h is unknown we try to get rid of this variable:
Combining both formulas gives the following equation:
c 2 – x2 = b 2 – a 2 + 2 a x – x2
Both sides have –x2 so we can simplify to:
c 2 = b 2 – a 2 + 2 a x
Now we can isolate x because a, b and c are known: = 2 2 + 2 2
This formula can be used as the basis for the law of cosines. The next step is to consider the triangle ADC and use the cosine formula with A as the base point.
With the CAH formula you can now calculate the cos (α):
When rearranging this formula you get the law of cosines formula:
With the same methodology the formula can be determined for the other two angles: