TExcelTAD
Home › Math for Games › Rotation in 2D

Rotation in 2D

Rotating a point around the origin by an angle θ (counter-clockwise, with y up) uses one pair of formulas you will see in every engine:

text
x' = x * cos θ - y * sin θ
y' = x * sin θ + y * cos θ

Where does it come from? The point (1, 0) rotates to (cos θ, sin θ) and the point (0, 1) rotates to (-sin θ, cos θ). Any point (x, y) is x steps along the first and y steps along the second, so it rotates to x * (cos θ, sin θ) + y * (-sin θ, cos θ). Those two rotated axes are the columns of the rotation matrix you will meet in the next section.

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

struct Vec2 { double x, y; };

Vec2 rotate(Vec2 v, double angle) {
    double c = std::cos(angle), s = std::sin(angle);
    return {v.x * c - v.y * s, v.x * s + v.y * c};
}

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

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double toRad = std::numbers::pi / 180;
    Vec2 p{2, 0};
    for (int deg : {0, 30, 90, 180, 270, -45}) {
        Vec2 r = rotate(p, deg * toRad);
        std::cout << std::setw(4) << deg << " deg: (" << z0(r.x) << ", " << z0(r.y) << ")\n";
    }
}
   0 deg: (2.000, 0.000)
  30 deg: (1.732, 1.000)
  90 deg: (0.000, 2.000)
 180 deg: (-2.000, 0.000)
 270 deg: (0.000, -2.000)
 -45 deg: (1.414, -1.414)

Rotating around a pivot

The formula rotates around the origin. To spin around any other point (a door hinge, the center of a sprite, a planet's sun), move the pivot to the origin, rotate, and move back:

text
rotated = pivot + rotate(point - pivot, θ)
cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>

struct Vec2 { double x, y; };
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; }
Vec2 rotate(Vec2 v, double a) {
    double c = std::cos(a), s = std::sin(a);
    return {v.x * c - v.y * s, v.x * s + v.y * c};
}
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    const double toRad = std::numbers::pi / 180;
    // A square sprite with corners around its center (5, 5), rotated by 45 degrees.
    Vec2 center{5, 5};
    Vec2 corners[] = {{4, 4}, {6, 4}, {6, 6}, {4, 6}};
    for (Vec2 c : corners) {
        Vec2 r = center + rotate(c - center, 45 * toRad);
        std::cout << "(" << c.x << ", " << c.y << ") -> (" << z0(r.x) << ", " << z0(r.y) << ")\n";
    }
}
(4.000, 4.000) -> (5.000, 3.586)
(6.000, 4.000) -> (6.414, 5.000)
(6.000, 6.000) -> (5.000, 6.414)
(4.000, 6.000) -> (3.586, 5.000)

Local axes: forward and right

A 2D character with heading θ has a forward vector (cos θ, sin θ) and a right vector (sin θ, -cos θ) (forward rotated by -90°). Moving "forward 3 and right 1" in the character's own frame is forward * 3 + right * 1 in the world. Converting between a character's local frame and the world is what the Spaces section is all about.

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

struct Vec2 { double x, y; };
Vec2 operator+(Vec2 a, Vec2 b) { return {a.x + b.x, a.y + b.y}; }
Vec2 operator*(Vec2 v, double s) { return {v.x * s, v.y * s}; }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    double heading = 90 * std::numbers::pi / 180;      // facing +Y
    Vec2 forward{std::cos(heading), std::sin(heading)};
    Vec2 right{std::sin(heading), -std::cos(heading)};
    Vec2 pos{10, 10};

    Vec2 gunMuzzle = pos + forward * 1.5 + right * 0.25;   // gun held slightly to the right
    std::cout << "forward (" << z0(forward.x) << ", " << z0(forward.y) << ")\n";
    std::cout << "right   (" << z0(right.x) << ", " << z0(right.y) << ")\n";
    std::cout << "muzzle  (" << z0(gunMuzzle.x) << ", " << z0(gunMuzzle.y) << ")\n";
}
forward (0.000, 1.000)
right   (1.000, 0.000)
muzzle  (10.250, 11.500)

See it move

The canvas below rotates a ship's outline around its center with the same formula. Note that canvas y points down, so positive angles turn the ship clockwise on screen.

html · try it yourself
<canvas id="c" width="360" height="220" style="background:#0d1b2a;display:block;max-width:100%"></canvas>
<div style="font:13px system-ui;color:#555;margin-top:4px">
  angle <input id="a" type="range" min="-180" max="180" value="30"> <span id="v"></span>
</div>
<script>
  const ctx = document.getElementById('c').getContext('2d');
  const slider = document.getElementById('a'), label = document.getElementById('v');
  const ship = [[24, 0], [-14, -12], [-8, 0], [-14, 12]];       // local points, nose along +x
  const center = { x: 180, y: 110 };
  function rotate([x, y], a) { const c = Math.cos(a), s = Math.sin(a); return [x * c - y * s, x * s + y * c]; }
  function draw() {
    const deg = Number(slider.value), a = deg * Math.PI / 180;
    ctx.clearRect(0, 0, 360, 220);
    ctx.strokeStyle = '#33415c'; ctx.beginPath();
    ctx.moveTo(0, center.y); ctx.lineTo(360, center.y); ctx.moveTo(center.x, 0); ctx.lineTo(center.x, 220); ctx.stroke();
    ctx.fillStyle = '#ffd166'; ctx.beginPath();
    ship.map((p) => rotate(p, a)).forEach(([x, y], i) => {
      const sx = center.x + x * 2.5, sy = center.y + y * 2.5;
      i ? ctx.lineTo(sx, sy) : ctx.moveTo(sx, sy);
    });
    ctx.closePath(); ctx.fill();
    const [fx, fy] = rotate([1, 0], a);
    ctx.strokeStyle = '#06d6a0'; ctx.lineWidth = 2; ctx.beginPath();
    ctx.moveTo(center.x, center.y); ctx.lineTo(center.x + fx * 90, center.y + fy * 90); ctx.stroke(); ctx.lineWidth = 1;
    label.textContent = deg + '°  forward = (' + fx.toFixed(2) + ', ' + fy.toFixed(2) + ')';
  }
  slider.addEventListener('input', draw);
  draw();
</script>

Composing rotations

In 2D, rotating by a and then by b is the same as rotating by a + b, and the order doesn't matter. Enjoy it: in 3D neither is true, and that is why the Rotations section exists.

Tip: If you rotate the same vector a little every frame (v = rotate(v, small)), rounding errors slowly change its length. Store the angle and rebuild the vector from it each frame, or renormalize now and then.

Test yourself

Where does the point (1, 0) end up after rotating by θ counter-clockwise?

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

How do you rotate a point p around a pivot c?

  1. rotate(p, θ) - c
  2. c + rotate(p - c, θ)
  3. rotate(p + c, θ)
  4. rotate(c, θ) + p

In 2D, is rotating by 30° then 50° the same as 50° then 30°?

  1. Yes, both equal an 80° rotation
  2. No, the order matters
  3. Only around the origin
  4. Only for unit vectors