TExcelTAD
Home › Math for Games › Radians, Sine and Cosine

Radians, Sine and Cosine

Angles in games come in two units. Designers and editors show degrees (a full turn is 360°). Every math library works in radians (a full turn is 2π ≈ 6.283). Mixing them up is one of the most common bugs, because sin(90) compiles happily and returns 0.894 instead of 1.

Radians

One radian is the angle at which the arc along a circle is as long as its radius. A full circle has a circumference of 2π r, so it is 2π radians.

text
radians = degrees * π / 180
degrees = radians * 180 / π
Degrees 0 30 45 60 90 180 270 360
Radians 0 π/6 π/4 π/3 π/2 π 3π/2 2π

C++20 has std::numbers::pi in <numbers>; older code uses M_PI (not standard) or its own constant.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

constexpr double PI = std::numbers::pi;
constexpr double toRadians(double deg) { return deg * PI / 180.0; }
constexpr double toDegrees(double rad) { return rad * 180.0 / PI; }

int main() {
    std::cout << std::fixed << std::setprecision(4);
    std::cout << "90 deg  = " << toRadians(90) << " rad\n";
    std::cout << "1 rad   = " << toDegrees(1) << " deg\n";
    std::cout << "sin(90)          = " << std::sin(90.0) << "   <- 90 radians, a bug!\n";
    std::cout << "sin(toRadians(90)) = " << std::sin(toRadians(90)) << "\n";
}
90 deg  = 1.5708 rad
1 rad   = 57.2958 deg
sin(90)          = 0.8940   <- 90 radians, a bug!
sin(toRadians(90)) = 1.0000

Sine and cosine on the unit circle

Walk around a circle of radius 1 starting at (1, 0), counter-clockwise. After turning by angle θ you are at:

text
(cos θ, sin θ)

That is all sine and cosine are for most game code: they turn an angle into a direction. Multiply by a radius to get a point on any circle, add a center to move the circle.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double PI = std::numbers::pi;
    // Place 6 enemies evenly on a circle of radius 4 around the point (10, 5).
    const int count = 6;
    const double radius = 4, cx = 10, cy = 5;
    for (int i = 0; i < count; i++) {
        double angle = 2 * PI * i / count;
        double x = cx + std::cos(angle) * radius;
        double y = cy + std::sin(angle) * radius;
        std::cout << "enemy " << i << " at " << std::setw(3) << i * 360 / count << " deg: ("
                  << z0(x) << ", " << z0(y) << ")\n";
    }
}
enemy 0 at   0 deg: (14.000, 5.000)
enemy 1 at  60 deg: (12.000, 8.464)
enemy 2 at 120 deg: (8.000, 8.464)
enemy 3 at 180 deg: (6.000, 5.000)
enemy 4 at 240 deg: (8.000, 1.536)
enemy 5 at 300 deg: (12.000, 1.536)

Waves: bobbing, pulsing, swaying

As time grows, sin(t) swings smoothly between -1 and 1 forever. That makes it the cheapest animation tool there is:

text
value = center + amplitude * sin(2π * frequency * time + phase)

amplitude is how far it swings, frequency how many full swings per second, and phase shifts it in time so that a row of objects doesn't move in lockstep.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
#include <string>

int main() {
    const double PI = std::numbers::pi;
    // A coin bobbing 0.25 m around y = 1, once per second. Print a little ASCII plot.
    for (int frame = 0; frame <= 10; frame++) {
        double t = frame * 0.1;
        double y = 1.0 + 0.25 * std::sin(2 * PI * 1.0 * t);
        int col = static_cast<int>(std::lround((y - 0.75) * 40));    // 0..20 columns
        std::cout << std::fixed << std::setprecision(1) << "t=" << t
                  << std::setprecision(3) << " y=" << y << " |" << std::string(col, ' ') << "o\n";
    }
}
t=0.0 y=1.000 |          o
t=0.1 y=1.147 |                o
t=0.2 y=1.238 |                    o
t=0.3 y=1.238 |                    o
t=0.4 y=1.147 |                o
t=0.5 y=1.000 |          o
t=0.6 y=0.853 |    o
t=0.7 y=0.762 |o
t=0.8 y=0.762 |o
t=0.9 y=0.853 |    o
t=1.0 y=1.000 |          o

Wrapping angles

Angles that keep growing (a spinning wheel, a turret that has turned 30 times) should be wrapped to a fixed range, usually (-π, π] or [0, 2π). The shortest signed difference between two angles is especially important: it decides whether a turret turns left or right.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

const double PI = std::numbers::pi;

// Wrap any angle into (-PI, PI].
double wrapPi(double a) {
    a = std::fmod(a + PI, 2 * PI);
    if (a < 0) a += 2 * PI;
    return a - PI;
}

double deg(double r) { return r * 180 / PI; }
double rad(double d) { return d * PI / 180; }

int main() {
    std::cout << std::fixed << std::setprecision(1);
    double tests[] = {370, -190, 720, 181, 45};
    for (double d : tests)
        std::cout << d << " deg wraps to " << deg(wrapPi(rad(d))) << "\n";

    // Shortest turn from 350 deg to 10 deg is +20, not -340.
    double from = rad(350), to = rad(10);
    std::cout << "shortest turn 350 -> 10: " << deg(wrapPi(to - from)) << " deg\n";
}
370.0 deg wraps to 10.0
-190.0 deg wraps to 170.0
720.0 deg wraps to 0.0
181.0 deg wraps to -179.0
45.0 deg wraps to 45.0
shortest turn 350 -> 10: 20.0 deg

Note: Engines differ in which way a positive angle turns. On a screen with y pointing down (canvas, most 2D UI), increasing the angle turns clockwise visually even though the math is the same. Always check one known case, such as "angle 90° should point to the bottom of the screen", in a new engine.

Test yourself

How many radians are in 180°?

  1. π/2
  2. π
  3. 2π
  4. 180

Which point lies on the unit circle at angle θ?

  1. (sin θ, cos θ)
  2. (cos θ, sin θ)
  3. (θ, 1)
  4. (tan θ, 1)

A turret faces 350° and the target is at 10°. What is the shortest signed turn?

  1. -340°
  2. +20°
  3. +340°
  4. -20°