Using Trigonometric Functions in Game Programming
By: Ali Soltanian Fard Jahromi
What are the Trigonometric Functions?
The trigonometric functions are a set of functions which show the relation between an angle of a right-angle triangle and the ratio of the lengths of two of its sides. The trigonometric functions are: sine, cosine and tangent. The trigonometric functions are periodic, meaning that their graphs repeat continuously in a specific pattern. For example, here is the cosine function’s graph:
Uses of the Trig Functions in Game Programming
I am a game developer and a digital artist (CGI, 3D animation, digital art). I have developed and released many games in different genres. Trigonometric functions can come particularly useful in game programming. For example, in some games, I have to find a way to implement a day and night system, where the world oscillates between day and night repeatedly as the game progresses. This repetitive pattern can be represented by the cosine trigonometric function. The cosine function always results in a number between negative one and one. In the code for my game, I have a value which represents the brightness of the atmosphere of the environment which can be anywhere from zero to one.
To achieve the day and night cycle, I create a variable that is constantly increasing. I then take the cosine of it. Furthermore, I take the absolute value of that variable so that the value of the variable is never below zero. The figure below is a model representing the day and night cycle implemented in the game:
The result is a trigonometric function representing the day and night cycle in my game program.
(C) 2021, Ali Soltanian Fard Jahromi. All rights reserved. Article can be shared as long as due credit is given and no modifications are made to the original article.