Lesson plan

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.

4 chapters, 7 lessons, about 5 hours, on the ESP32-C3-DevKitM-1. Every lesson runs in the browser, with nothing to install.

The unit

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.

Chapter 1. About 1 h 30 min

Arrows and angles

Vectors for motion and direction, and atan2 for which way to turn.

LessonTimeWhat 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
  1. Arrows in code
  2. Speed and direction
  3. Ahead or behind?
  4. Twice as fast
  5. Overshoot
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
  1. Radians
  2. atan forgets the signs
  3. Compass bearings
  4. Wrap to 360
  5. The shortest turn

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".

Chapter 2. About 1 h 30 min

Turning things

Rotation by sine and cosine, and six numbers that do it all at once.

LessonTimeWhat 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
  1. Two lines of trig
  2. Errors pile up
  3. Lost in the corner
  4. Turn about a pivot
  5. Any angle
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
  1. Six numbers
  2. Order matters
  3. The determinant
  4. Bigger ship
  5. Undo it: a tap

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".

Chapter 3. About 1 h 20 min

Smooth motion

Interpolation that feels alive and filters that calm a shaky sensor.

LessonTimeWhat 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
  1. lerp
  2. A glide in frames
  3. Ease in, ease out
  4. Angles wrap
  5. The long way is short
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
  1. A shaky compass
  2. Average the vectors
  3. Exponential smoothing
  4. Lag against calm

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".

Chapter 4. About 45 min

Project

A ship's compass on a screen.

LessonTimeWhat 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
  1. The instrument
  2. Come about
  3. Southwest
  4. True north
  5. Make it yours

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".

Your class

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.