Basic Trigonometry for Game Development

Trigonometry derives from Greek words tigonon or Sanskrit word trikona both meaning triangle and metron from Greek or matra from Sanskrit, both meaning measurement. India created the earliest known tables of values of trigonometric functions such as sine and cosine.

So why do we need to know about trigonometry in game dev?

Well, whether we like it or not, to build good game mechanics, one really needs to know at least the very basics of trigonometry to find out angles and distances etc. Don’t worry you don’t have to learn every thing or spend years understanding it. Most game engines provide helper functions to make things easier for game development. Having said that, I feel it is important to have an overview of what is happening behind those functions, otherwise, you will end up just copy pasting other people’s code, which is ok, but not recommended, since your game needs to behave according to your ideas and rules and not someone else’s code. Another reason is, that most of these functions are built based on what is the most common scenario or most used or requested by most game developers. You may not always want exactly what everyone else wants. You might have a situation which is different from most situations in a game. In such situations, it will be handy to know some basic trigonometry to be able to write code for those special situations.

Having said that, I will cover the very basics of Trigonometry that you will need to understand as to know how things are working in your game or game engine. Why I will cover only basics? Cause I am no mathematician, I only know basics :).

So let’s begin.

Finding Sides from Angles

As we discussed in my other tutorial on Basics of Vector Math, We can make right-angled triangles of any Vector, we can use that to calculate angles using trigonometric functions.

enter image description here

As you can see in the diagrams above, a Vector can be converted into a Right-angled Triangle You can use the trigonometric functions to calculate angles and distances between any two vectors.

Hypotenuse : the length between two points in a triangle opposite to the right-angle.
Adjacent Side : the side which is between the right-angle and the angle you want to calculate which is denoted by θ. It is adjacent to the angle θ hence the name adjacent side.
Opposite Side : the side which is exactly opposite to the angle θ.

Using these sides and the angle θ and the help of trigonometric functions sine, cosine and tangent, we can calculate the angle θ if we know any two sides, or we can calculate the length of a side if we know one of the sides and the angle θ. Let’s look at the following formulas:

sin \theta = \frac{opposite}{hypotenuse}

cos \theta = \frac{adjacent}{hypotenuse}

tan \theta = \frac{opposite}{adjacent}

Now, using the above formulas, if you know any two you can calculate the third. So, lets begin with finding out one of the sides assuming we know the angle and one side.

hypotenuse = 10
angle = 30 degrees
Let’s say we need to find the opposite side. We can use the sine function in this case.

sin \theta = \frac{opposite}{hypotenuse}

\rArr sin 30 = \frac{opposite}{10}

sin30 is 0.5, so:

\rArr opposite = 0.5 * 10 = 5

Inverse Trigonometric Functions for finding angles from sides

More often in game development you will need to use inverse functions where we know the lengths of the vectors but we need to find the angle.

The formulas we use are the same as above except now they become:
\theta = sin^{-1} * \frac{opposite}{hypotenuse}

\theta = cos^{-1} * \frac{adjacent}{hypotenuse}

\theta = tan^{-1} * \frac{opposite}{adjacent}

the names of the inverse functions are arcsine, arccosine and arctangent.

In most game engines including Godot, there will be functions to calculate these.

  • sin()
  • cos()
  • tan()
  • asin()
  • acos()
  • atan() and atan2()

For example, let’s assume we need to find the angle between the x-axis and a 2D Vector (7, 12). So here we know that x is 7 so the adjacent length is 7 and the opposite side is 12. To calculate the angle we can use arctan function.

\theta = tan^{-1} \frac{12}{7}

\rArr 59.7\degree

game engines normally use angles in radians and not degrees so we can convert the degrees to radians

\frac{59.7}{180}*{\pi}

This may see a lot but most game engines provide these functions and all you have to do is just pass the right parameters.

atan2

It is possible that one of the sides is 0. In that case, there can be an error dividing by zero, so instead of using atan(v : float) we can use atan2(y, x). the atan2 function makes sure that if x is zero it will not cause an error. Other functions like asin(v : float), acos(v : float)are also available. Check Godot documentation for complete list.

All these functions return angles in radians so you do not need to convert them in to radians.

This time let us use an actual example of when can we use such functions.

Let’s assume that in your 3D game you want to support joystick and you get input values from a joystick. Based on these input values you want to rotate the players direction. You need to rotate the player in the direction where the joystick input Vector is pointing. In most game engines, the joystick input will be normalized. We need to calculate how much the player needs to rotate on y axis. We can simply do that by getting the current angle using the function atan2. Since the forward z-axis in Godot is negative and the y-axis is up, we can get the desired results by using the following equation:

var angle = -atan2(input.x, -input.y)
player.rotation.y = angle

In the above example, just to simplify, I am not using any linear interpolation when the player rotates from one angle to the other. the rotation will be instant.

This is pretty much all you need to know for basic game development. I will be posting more examples in my future posts and share how I use these trigonometric functions in my games.

Please leave comments in the comments section to let me know what you liked or disliked. Let me know if you found this article helpful. I will try my best to incorporate you suggestions in the future posts.

Thanks for reading!
~ Vini

Featured Image by Anoushka P on Unsplash

Last modified: December 17, 2021

Author

Comments

Write a Reply or Comment

Your email address will not be published.