Stage 01 · Lesson 05/12

Math for Robotics · Lesson 05

3D Vectors

Est. 35–45 min

Why

Why height forces you into 3D

Concept

The big idea: (x, y, z), unit vectors, and the cross product

  • A 3D vector is (x, y, z). Magnitude |v| = √(x²+y²+z²).
  • Unit vector: v / |v| — same direction, length 1.
  • Dot product still measures alignment: a·b = |a||b| cos φ.
  • Cross product a×b is a vector perpendicular to both (right-hand rule). |a×b| = |a||b| sin φ.
  • Use cross products for surface normals and torque direction τ = r × F.
NoteSimplification: we do not cover Plücker screws or spatial algebra yet.

How it works

How it actually works, step by step

  1. Write three components

    Agree on +z (often up). Write (x,y,z) in metres.

  2. Normalise when you need direction only

    Divide by magnitude to get a unit direction for “look-at” or approach axes.

  3. Dot for alignment

    Same as 2D — now with a z term.

  4. Cross for a perpendicular

    Point fingers along a, curl toward b; thumb shows a×b. Check a·(a×b)=0.

a × b = (aᵧb_z − a_z bᵧ, a_z bₓ − aₓ b_z, aₓ bᵧ − aᵧ bₓ)

Visual

Visualise it: 3D magnitude, dot, and cross

Slide a and b components. Read |a|, a·b, angle, and a×b. Dashed arrow is the cross product.

Interactive · 3D vector lab (magnitude, dot, cross)

+x+y+z

|a|

3.000

a · b

0.000

∠(a,b)

90.0°

|a × b|

6.708

a × b = (-2.00, 5.00, -4.00)

Dashed arrow is a × b (right-hand rule). Cross product is perpendicular to both a and b — useful for surface normals and torque direction.

Worked example

Worked example: a=(1,2,2), b=(2,0,−1)

Code lab

Code lab: magnitude, unit, cross

vectors_3d.py · Python
"""Stage 1 L05 — 3D magnitude, unit vector, and cross product."""
import math

a = (1.0, 2.0, 2.0)
b = (2.0, 0.0, -1.0)

def mag(u):
    return math.sqrt(u[0]**2 + u[1]**2 + u[2]**2)

def unit(u):
    m = mag(u)
    return (u[0]/m, u[1]/m, u[2]/m)

def cross(u, v):
    return (
        u[1]*v[2] - u[2]*v[1],
        u[2]*v[0] - u[0]*v[2],
        u[0]*v[1] - u[1]*v[0],
    )

def dot(u, v):
    return u[0]*v[0] + u[1]*v[1] + u[2]*v[2]

c = cross(a, b)
print(f"a = {a}, |a| = {mag(a):.3f}")
print(f"unit(a) = ({unit(a)[0]:.3f}, {unit(a)[1]:.3f}, {unit(a)[2]:.3f})")
print(f"a × b = {c}")
print(f"check a·(a×b) = {dot(a, c):.1f} (should be 0)")
print(f"check b·(a×b) = {dot(b, c):.1f} (should be 0)")
print(f"|a×b| = {mag(c):.3f}")

Expected output

a = (1.0, 2.0, 2.0), |a| = 3.000
unit(a) = (0.333, 0.667, 0.667)
a × b = (-2.0, 5.0, -4.0)
check a·(a×b) = 0.0 (should be 0)
check b·(a×b) = 0.0 (should be 0)
|a×b| = 6.708

Real world

Normals, torque, and measuring arms

Diagram of the right-hand rule for the cross product of two vectors.
Right-hand rule for a × b. Robotics uses this constantly for normals, angular velocity direction, and torque.Photo: Sepitropova · CC BY-SA 4.0 · Source: Wikimedia Commons · resized, converted to WebP
Portable 3D measuring arm used to digitize points in space.
A 3D measuring arm reports points as (x,y,z) in its base frame — exactly the vectors in this lesson.Photo: Brechmann-Guss · CC BY-SA 3.0 · Source: Wikimedia Commons · resized, converted to WebP

Mistakes

Common mistakes and confusions

Hardware & safety

Hardware lab and safety notes

Every phone and many robot boards have an accelerometer (part of an IMU). Held still, it measures a 3D vector of about 9.8 m/s² pointing up (it feels the table pushing against gravity).

GY-521 breakout board with the MPU-6050 chip and pins labelled VCC, GND, SCL, SDA, XDA, XCL, AD0, INT.
GY-521 module with an MPU-6050 accelerometer + gyroscope. Note the X and Y arrows printed on the board — the sensor's own frame.Photo: Nevit Dilmen · CC BY-SA 3.0 · Source: Wikimedia Commons · resized, converted to WebP
  • Install any free sensor app (look for raw “accelerometer” x, y, z). Lay the phone flat: you should see roughly (0, 0, 9.8).
  • Tilt it 90° onto its side: the 9.8 moves to x or y. The magnitude stays ≈ 9.8 — only direction changed.
  • Later, an MPU-6050 on I²C gives the same kind of vector to an Arduino (3.3 V/5 V module, 4 wires, no high current).

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
Cross product points the wrong wayOrder swapped (b × a = −a × b)Keep the order a × b; use the right-hand rule
Unit vector has length ≠ 1Divided by the wrong magnitudeDivide each component by |a| of the same vector
Normal of the desk is not ±zPoints not measured in one frameMeasure all three points from one corner
Phone shows −9.8 on zPhone is upside down (screen facing table)That is correct — the sign tells you which way is up
NoteChange one thing at a time. Most Stage 1 bugs are units (degrees vs radians, mm vs m), order of multiplication, or a frame that was never named.

Think

Think about it

Think

Why is a×a always the zero vector?

Experiment

Try it in the lab: desk axes

Experiment

Desk axes

You will need

  • Your right hand
  • vector-3d interactive
  • Notebook

Steps

  1. Point index finger +x along the desk, middle finger +y, thumb +z (right-hand triad).
  2. In the interactive, set a and b nearly parallel — confirm |a×b| shrinks.
  3. Set them nearly perpendicular — |a×b| grows toward |a||b|.

Interactive · 3D vector lab (magnitude, dot, cross)

+x+y+z

|a|

3.000

a · b

0.000

∠(a,b)

90.0°

|a × b|

6.708

a × b = (-2.00, 5.00, -4.00)

Dashed arrow is a × b (right-hand rule). Cross product is perpendicular to both a and b — useful for surface normals and torque direction.

|a×b| should approach 0 when the angle readout approaches 0° or 180°.

Knowledge check

Check your understanding

Knowledge check

1. |(1,2,2)| equals…

2. a × b is perpendicular to…

3. Right-hand rule thumb shows…

4. Unit vector has length…

Practice quiz

Loading practice quiz…

Mini assignment

Hands-on assignment

Mini assignment

3D point log

Measure or invent three points on your desk as (x,y,z) cm in a right-handed frame. Compute two edge vectors and their cross product (a normal to the desk plane — should be mostly ±z).

Deliverables

  • Frame sketch
  • Three points
  • Cross-product normal

Do this, step by step

  1. Measure three corners of a book on your desk in cm (one frame, z = 0 for all).
  2. Make two edge vectors from the first corner and compute their cross product by hand.
  3. Run vectors_3d.py with your numbers and compare.
  4. Normalise the result and check its length is 1.000.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: Example corners: P0 = (0,0,0), P1 = (20,0,0), P2 = (0,15,0) → edges (20,0,0) and (0,15,0).
  2. Hint 2: (20,0,0) × (0,15,0) = (0, 0, 300) — straight up, as expected for a flat book.
  3. Hint 3: Unit normal = (0, 0, 300)/300 = (0, 0, 1).

Challenges (pick by level)

  • 🟢 Easy: Compute |(2, 3, 6)|. (Answer: 7.)
  • 🟡 Medium: Show that a × b is perpendicular to both a and b using two dot products.
  • 🟠 Hard: Log your phone's (x, y, z) at 5 tilts and compute the magnitude each time. How close to 9.8 m/s² is it?

Next step

Next, Lesson 6: Matrices — a neat way to transform many vectors at once. Related: IMUs and sensor fusion in Stage 5 — Robotics Fundamentals.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in