Trigonometry

Trigonometry in Video Game Design

Trigonometry in Video Game Design

When you press 'X' to jump, pull the trigger to fire a sniper rifle, or drift around a corner in Need for Speed, you aren't just playing a game—you are interacting with a high-speed trigonometry engine. Video games are essentially math happening 60 times per second.

Many students ask, "When will I ever use Sin, Cos, or Tan?" The answer is: every single time you pick up a controller. Without these three functions, modern gaming simply wouldn't exist. In this deep dive, we'll explore exactly how developers use trigonometry to build worlds, calculate physics, and make enemies intelligent.

1. The Screen as a Cartesian Plane

Before understanding the math, you must understand the world. Your computer screen is a giant grid. In 2D games (like Super Mario or Celeste), everything has an (x, y) coordinate.
X: Horizontal position (Left/Right)
Y: Vertical position (Up/Down)

However, the direction you move isn't usually just Up or Right. It's usually at an angle. If you push the joystick 30 degrees to the right, how does the game know how much to change X and how much to change Y? Enter the Unit Circle.

2. Velocity and Movement (Splitting the Vector)

Imagine your character has a speed of 10 pixels/frame and is moving at a 45-degree angle. You can't just add 10 to X and 10 to Y, or they would move faster diagonally (Pythagoras tells us √(10² + 10²) ≈ 14.14). To keep the speed constant, we must split that '10' into horizontal and vertical components.

velocity_x = speed * cos(angle)
velocity_y = speed * sin(angle)

This is the most fundamental code in game development.
Cosine is associated with the X-axis (Adjacent side).
Sine is associated with the Y-axis (Opposite side).

So, for a 45-degree angle:
Vel_X = 10 * 0.707 = 7.07
Vel_Y = 10 * 0.707 = 7.07
New Speed = √(7.07² + 7.07²) = 10. Perfect.

3. Aiming: The ATAN2 Function

Movement uses Sin/Cos to go from Angle → Coordinates. But aiming is the opposite. You have the Coordinates of the enemy (Enemy_X, Enemy_Y) and the Coordinates of the Player (Player_X, Player_Y), and you need to find the Angle to rotate your gun.

You might remember from school that Tan(theta) = Opposite / Adjacent. So, theta = Arctan(Opposite / Adjacent).

In programming, we use a special function called Math.atan2(y, x). Why not just normal atan? Because normal atan can't tell the difference between Top-Right and Bottom-Left coordinates (both have positive slopes). Atan2 handles all 360 degrees perfectly. It is the gold standard for tracking targets.

Open Trig Calculator

4. Procedural Generation with Perlin Noise

How does Minecraft generate infinite hills and valleys? It doesn't store them; it calculates them using math. While Perlin Noise is complex, simpler implementations use Sine Waves.

By adding multiple sine waves of different frequencies and amplitudes together, you create a "random" looking hilly terrain.
Height = sin(x) + 0.5*sin(2x) + 0.25*sin(4x)
This technique is called Fourier Synthesis, and it's used for water ripples, swaying grass, and rolling hills.

5. The Dot Product: Field of View

How does a guard in Metal Gear Solid know if he can see you? He has a "Field of View" (FOV) cone. To calculate if you are inside that cone efficiently, developers use the Dot Product.

If the Dot Product of the [Guard's Looking Direction] and the [Vector to Player] is greater than a certain number, you are spotted. If it's 0, you are perpendicular. If it's negative, you are behind him. This calculation is incredibly fast because it involves simple multiplication, no slow square roots or angles.

6. Common Mistakes

  • Degrees vs Radians: Computers almost ALWAYS use Radians. If you input 90 into a function expecting radians, your character will spin wildly. Always multiply by (PI / 180) to convert.
  • Gimbal Lock: In 3D rotation using Euler angles (X, Y, Z), if two axes align, you lose a degree of freedom. This causes the camera to flip out. Modern engines use Quaternions (4D complex numbers) to solve this.

7. FAQ

Q: Do I need to be good at math to be a game dev?
A: You don't need to be a mathematician, but you need to understand vectors and basic trig. Game engines like Unity handle the heavy lifting, but you need to know which function to call.

Q: Is 3D math harder than 2D?
A: Yes, adding the Z-axis adds complexity (Matrix multiplication, projections), but the core concept of SOH CAH TOA remains the bedrock.

Conclusion

Trigonometry is the hidden language of virtual worlds. It translates the player's intent (pushing a stick) into digital action (movement). So next time you land a perfect headshot or drift a perfect corner, thank a triangle.

Explore More Articles

Expand your knowledge with these related guides.

Home Blog

Calculators

Scientific Statistics Percentage Percentile Trigonometry Matrices Equations Graphing Derivatives Integration Limits

Menu

About Us Request Feature Toggle Theme