Vectors, trig and matrices for code lesson plan
The math under every game and instrument, in C++ on an ESP32-C3: vectors, atan2, rotation, matrices, easing and smoothing, ending in a ship's compass needle on a screen.
Objectives and materials
By the end, students can
- Vectors for motion and direction, and atan2 for which way to turn.
- Rotation by sine and cosine, and six numbers that do it all at once.
- Interpolation that feels alive and filters that calm a shaky sensor.
- A ship's compass on a screen.
Materials for the whole unit
- ESP32-C3-DevKitM-1
- Full-size breadboard
- Magnetometer, LIS3MDL
- TFT display, 240x320
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Arrows and angles
Vectors for motion and direction, and atan2 for which way to turn.
| Lesson | Time | What students do |
|---|---|---|
| VectorsA capsule drifting toward a docking port, in vectors: add and scale for motion, length and normalize for speed and direction, and the dot product for ahead or behind. | 45 min |
|
| Angles and atan2Four beacons round a ship on a radar: radians and degrees, why atan(y / x) gets half of them wrong, how atan2 gets all four, compass bearings, wrapping, and the shortest turn. | 45 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- Magnetometer, LIS3MDL
- TFT display, 240x320
Check for understanding
Each student finishes "Angles and atan2" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "The shortest turn".
Turning things
Rotation by sine and cosine, and six numbers that do it all at once.
| Lesson | Time | What students do |
|---|---|---|
| RotationTurn a ship outline with sine and cosine: the rotation formula, why it turns clockwise on a screen, how small float errors pile up, and turning about a pivot instead of the corner. | 45 min |
|
| MatricesSix numbers that move, turn and grow a ship at once: 2D affine matrices, why the order of a product matters, the determinant as an area, and an inverse that turns a screen tap into ship coordinates. | 45 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- Magnetometer, LIS3MDL
- TFT display, 240x320
Check for understanding
Each student finishes "Matrices" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Undo it: a tap".
Smooth motion
Interpolation that feels alive and filters that calm a shaky sensor.
| Lesson | Time | What students do |
|---|---|---|
| InterpolationGlide a ship from a station to a dock: lerp, a vector lerp for a path, smoothstep easing that starts and stops gently, and turning a needle the short way around. | 45 min |
|
| Noise and smoothingA shaky compass heading from the LIS3MDL, smoothed three ways: why averaging angles fails at north, averaging vectors instead, and an exponential filter that trades lag for calm. | 35 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- Magnetometer, LIS3MDL
- TFT display, 240x320
Check for understanding
Each student finishes "Noise and smoothing" on their own, without opening the hints. The simulator checks 4 of its 4 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Lag against calm".
Project
A ship's compass on a screen.
| Lesson | Time | What students do |
|---|---|---|
| Project: a compass needle on a screenA ship's compass from the LIS3MDL: atan2 for the heading, a vector filter for calm, a rotation matrix for the needle, the short way around, declination for true north and the cardinal point in big letters. | 45 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- Magnetometer, LIS3MDL
- TFT display, 240x320
Check for understanding
Each student finishes "Project: a compass needle on a screen" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it yours".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.