TwiddleLab / Math / Trig and Waves
Rotation with Trig
What you will learn
- Turning by θ counterclockwise is the matrix R(θ) = [cos θ, -sin θ; sin θ, cos θ].
- Its columns are where î and ĵ land: (cos θ, sin θ) and (-sin θ, cos θ).
- A game turns a ship by sending every corner through the same matrix, so the shape never squashes.
How the lesson goes
Your game turns a spaceship by any angle, 60 times a second.
Which four numbers do it? First: turn the ship 90°. Where does its nose land?
Turn the dial and watch the whole grid turn.
The card shows the matrix of the turn. Three goals: 90°, 180°, 30°.
Drag î around the circle.
ĵ stays a quarter turn ahead. Watch the two columns of the matrix.
This is the rotation matrix.
Turning by θ sends î to (cos θ, sin θ) and ĵ to (-sin θ, cos θ), a quarter turn ahead.
Put them in as columns: R(θ) = [cos θ, -sin θ; sin θ, cos θ]. Any point (x, y) goes to x × column 1 + y × column 2.
Build the matrix for a 270° turn.
Fill the four boxes with 0, 1 or -1 until the ship sits on the dashed outline.
R(270°) = [cos 270°, -sin 270°; sin 270°, cos 270°] = [0, 1; -1, 0].
Turn the point (3, 1) by 90°.
Tap the grid point where it lands.
R(90°) = [0, -1; 1, 0], so (3, 1) goes to (0 × 3 - 1 × 1, 1 × 3 + 0 × 1) = (-1, 3).
This game code squashes the ship.
One sign is wrong. Run it, flip the wrong sign, run again.
Row 1 of R is (cos a, -sin a), so x2 = x cos a - y sin a. With a plus, at 45° both rows are the same and the ship collapses onto a line.
Game: turret.
Three rocks. Turn the barrel until it points at the rock, then Fire.
R(θ) = [cos θ, -sin θ; sin θ, cos θ].
Draw your own ship from cells, then turn it with the matrix.
Previous lesson: Law of Cosines · Next lesson: Project: Make a Synth Note · Part of the course: Trig and Waves
Lessons are interactive and need JavaScript. Turn it on to play them.