Rotating in 2D
To rotate anything by an angle , you only need to know where the two axis arrows go: lands on and on , and every other point comes along for the ride. When something turns toward a target, wrap the angle difference into first, so it takes the short way around.
What the symbols mean
- Angle
How far to turn, counterclockwise, in radians (or degrees, if your cosine takes degrees).
What the symbols mean
- The point
The point (x, y) written as the complex number x + iy.
- Unit complex number
cos θ + i sin θ: multiplying by it turns by θ.
What the symbols mean
- Raw difference
Target angle minus current angle, which can be any size.
What the symbols mean
- Angular velocity
How fast the angle changes, in radians per second.
- Radius
Distance from the center of the turn.
- Speed
How fast a point at that radius moves.
Explanation
Rotating something in 2D just means swinging every point of it around a center by the same angle, while each point keeps its distance from the center. The formula , looks like a jumble, but it's really two small answers: where the -direction ends up, and where the -direction ends up. A rotation matrix is just those two answers written side by side as columns. Once you have it, turning a spaceship, a clock hand or a whole level is one multiplication per point. It's simpler than it looks; bear with it.
Where the formula comes from. Start with the two arrows and . Turn by and it lands on the unit circle at ; that's what cosine and sine are (The Unit Circle). The arrow starts a quarter turn ahead, so it lands at . Now any point is a recipe made of those two arrows, , and turning the ingredients first and mixing them afterward gives the same thing as mixing first and turning. So
which is the matrix above, read column by column. The columns are where the axes land (The Matrix of a Transformation). There's an animation further down where both columns sweep around while a ship turns.
Notice what a rotation never does: change lengths. Square and add,
because the cross terms cancel and . That makes a handy check on any rotation you compute by hand.
Turning about another point. The matrix turns things about the origin. To swing a door about its hinge , shift so the hinge sits at the origin, rotate and shift back: .
The same thing with complex numbers. Write the point as and multiply by :
The real part is and the imaginary part is : a unit complex number is a 2D rotation (Complex Numbers in Polar Form). Turning twice is multiplying twice, , so the angles add, and if you expand both sides and match real and imaginary parts, out fall the angle-sum formulas for and . Keep this picture in mind: later in this unit a quaternion plays exactly this role in 3D.
In code it's three lines:
function rotate(x: number, y: number, angle: number): [number, number] {
const c = Math.cos(angle), s = Math.sin(angle);
return [x * c - y * s, x * s + y * c];
}
Angles wrap around. A heading of and a heading of point the same way; so do and . That's harmless until something has to turn toward a target. A turret facing with a target at computes : turn clockwise. But the target is only away the other way. The fix is to wrap the difference into by adding or subtracting whole turns, which is the formula above. Try it on : , rounding up gives , and . In code, Math.atan2(Math.sin(d), Math.cos(d)) does the same job in radians, since atan2 always answers in .
To blend between two angles the short way, lerp by the wrapped difference (Lerp):
A common mistake. It's tempting to write angle = lerp(a, b, t) for angles, because it works for positions. It only works when the two numbers happen to be less than apart. Blending from to , the plain lerp passes through and swings the long way around, so the player watches a turret spin almost a full circle to aim at something just to its left. The animation further down shows both side by side. Always lerp by the wrapped difference.
Angular velocity. How fast something turns is its angular velocity , in radians per second, so . A point at distance from the center then moves at speed : in one second it sweeps an arc of radius and angle , and arc length is radius times angle. Each frame of length adds to the angle, which is why game code multiplies by dt.
Turning at a limited speed. A tank turret can't snap to its target; it turns at most . Each frame, wrap the difference and then clamp the step:
While the target is far away it turns at full speed, and on the last frame the clamp lets it land exactly on the target instead of overshooting.
See it
Worked examples
Rotate the point by counterclockwise about the origin, and check that its distance from the origin didn't change.
- 1
Write down the two numbers the matrix needs: and .
A turret faces and must aim at a target at . It turns at most per second and the game runs at 60 frames per second. Which way does it turn, how far each frame and how many frames until it's on target?
- 1
Find the raw difference, target minus current: . Taken literally, that says turn clockwise.
Practice
Type your answer and check it. Solve them all to complete the lesson.
Rotate the point by counterclockwise about the origin. Enter the new point .
A door in a top-down game is hinged at , and its handle is at . The door swings counterclockwise about the hinge. Where is the handle now? Enter .
A turret's heading is (angles measured counterclockwise from east) and its target is at . Taking the short way, what signed angle should it turn, in degrees? Positive means counterclockwise.
Treat the point as the complex number and rotate it by multiplying by . Enter the resulting point .
A loading spinner in an app is a dot px from the center that goes around times per second. How fast does the dot move, in pixels per second?
A tank turret faces and its target is at . The turret turns at most per second and always takes the short way. How many seconds until it faces the target?
Multiply the rotation matrices , where . What is the entry in row 1, column 2 of the product?
A 2D camera's heading follows a target with frame-rate-independent smoothing: each frame, with per second. The heading is , the target is at and one frame lasts s. What is the heading after one frame, in degrees between and ?
The hardest one here. Get it, and you've got this lesson.
A turret at the origin faces and turns at most per second, always the short way. A target appears at the point . The turret's barrel is m long. Where is the tip of the barrel s later? Enter the point in meters.
Where it's used
Every 2D game turns its sprites with exactly this matrix, usually packed into one transform with the position. Top-down shooters wrap and clamp turret headings every frame, minimaps rotate the world by the transpose so "up" is always the player's facing, and analog gauge needles, clock widgets and loading spinners are on screen. Robots do the same arithmetic in their steering loops: a mobile robot's heading error is wrapped into before it reaches the controller, or a robot facing that needs spins all the way around instead of nudging .