2D Vector Rotation Matrix
Spin vectors around the origin like a true mathemagician.
How does it work? ↓How does 2D Rotation work?
The Rotation Matrix Math
To rotate a 2D Vector by a specific angle ($\theta$), you
multiply it by a 2x2 Rotation Matrix. This is a fundamental
operation in Linear Algebra.
The formula (in code) expands to this:
new_x = (x * cos(angle)) - (y * sin(angle)) new_y = (x * sin(angle)) + (y * cos(angle))
Degrees vs. Radians
Engineers love reading "Degrees" (0° to 360°). Computers,
however, only understand Radians (0 to $2\pi$).
Before calculating your sin or cos functions,
you must convert your angle degree variable into
radians, otherwise your vector will spin wildly out of control.
radians = degrees * (Math.PI / 180.0)
Common Uses
- Orbiting Cameras: Rotate the camera's offset vector around the player.
- Twin-Stick Shooters: Firing bullets in a rotating spread pattern.
- AI Movement: Spinning a "wander" target around the enemy's pathfinding radius.
- Asteroids-style Flight: Calculating velocity vectors based on ship facing.
Answers to Common Developer Questions
How do you rotate a 2D Vector?
To rotate a 2D vector around the origin, you multiply it by a 2x2 Transform Matrix. Given an angle θ, the new coordinates are: x' = (x * cos θ) - (y * sin θ) and y' = (x * sin θ) + (y * cos θ).
Should I use Radians or Degrees for programming math?
Almost all game engines (Unity, Godot) take Degrees in their UI properties because they are easier for designers to read. However, all internal math functions like Math.cos() and Math.sin() strictly require Radians. You must always mathematically convert degrees to radians first.
What is Gimbal Lock in rotations?
Gimbal lock is a problem that occurs in 3D (Euler) rotations when two axes align, causing a loss of a degree of freedom. It does not affect 2D rotation matrices because 2D space only has a single rotational plane (the invisible Z-axis).