Skip to content

Vector Operations

In this lesson you’ll learn the basic operations you can perform on vectors: addition, subtraction, scalar multiplication, and the dot product.

Vectors are added component-wise:

u+v=u1+v1,  u2+v2,  u3+v3\mathbf{u} + \mathbf{v} = \langle u_1 + v_1,\; u_2 + v_2,\; u_3 + v_3 \rangle

Subtraction works the same way:

uv=u1v1,  u2v2,  u3v3\mathbf{u} - \mathbf{v} = \langle u_1 - v_1,\; u_2 - v_2,\; u_3 - v_3 \rangle

Geometrically, vector addition follows the triangle rule (or parallelogram rule). Place the tail of v\mathbf{v} at the tip of u\mathbf{u}, and the result u+v\mathbf{u} + \mathbf{v} goes from the origin to the tip of v\mathbf{v}:

u v u + v u = ⟨2, 3⟩ v = ⟨5, -1⟩ u + v = ⟨7, 2⟩

In the diagram above: the blue arrow is u=2,3\mathbf{u} = \langle 2, 3 \rangle starting from the origin. The green arrow is v=5,1\mathbf{v} = \langle 5, -1 \rangle placed at the tip of u\mathbf{u}. The dashed orange arrow shows the result u+v=2+5,  3+(1)=7,2\mathbf{u} + \mathbf{v} = \langle 2+5,\; 3+(-1) \rangle = \langle 7, 2 \rangle, going directly from the origin to where v\mathbf{v} ends. This is the triangle rule in action.

Multiplying a vector by a number (scalar) scales its length:

cv=cv1,  cv2,  cv3c \cdot \mathbf{v} = \langle c\, v_1,\; c\, v_2,\; c\, v_3 \rangle
  • If c>1\lvert c \rvert \gt 1, the vector gets longer
  • If 0<c<10 \lt \lvert c \rvert \lt 1, the vector gets shorter
  • If cc is negative, the vector reverses direction
v 2v -v v = ⟨2, 1⟩ 2v (longer, same direction) -v (reversed)

The diagram shows three versions of the same direction. The blue arrow is v=2,1\mathbf{v} = \langle 2, 1 \rangle. The green arrow is 2v=4,22\mathbf{v} = \langle 4, 2 \rangle, which points the same way but is twice as long. The orange arrow is v=2,1-\mathbf{v} = \langle -2, -1 \rangle, which has the same length as v\mathbf{v} but points in the opposite direction. Multiplying by a positive scalar stretches; multiplying by a negative scalar flips.

The dot product (also called scalar product) of two vectors returns a single number:

uv=u1v1+u2v2+u3v3\mathbf{u} \cdot \mathbf{v} = u_1 v_1 + u_2 v_2 + u_3 v_3

It can also be written as:

uv=uvcosθ\mathbf{u} \cdot \mathbf{v} = \lVert \mathbf{u} \rVert \, \lVert \mathbf{v} \rVert \cos \theta

where θ\theta is the angle between them.

Important properties:

  • If uv=0\mathbf{u} \cdot \mathbf{v} = 0, the vectors are orthogonal (perpendicular).
  • The dot product tells you how much one vector points in the direction of another.
59° u v u · v = 14 u = ⟨3, 4⟩ v = ⟨-2, 5⟩ θ = 59° (dot product)

Here u=3,4\mathbf{u} = \langle 3, 4 \rangle (blue) and v=2,5\mathbf{v} = \langle -2, 5 \rangle (green) meet at the origin with a 59-degree angle between them (purple arc). The dot product uv=(3)(2)+(4)(5)=14\mathbf{u} \cdot \mathbf{v} = (3)(-2) + (4)(5) = 14. Since the result is positive, the vectors point in roughly the same general direction (the angle is less than 90 degrees). If the dot product were zero, they’d be perpendicular; if negative, they’d point away from each other.

Example 1: Addition and Subtraction

Let u=2,3\mathbf{u} = \langle 2, 3 \rangle and v=5,1\mathbf{v} = \langle 5, -1 \rangle

u+v=7,2\mathbf{u} + \mathbf{v} = \langle 7, 2 \rangle

uv=3,4\mathbf{u} - \mathbf{v} = \langle -3, 4 \rangle

Example 2: Scalar Multiplication

Let w=1,2,3\mathbf{w} = \langle 1, 2, 3 \rangle

3w=3,6,93\mathbf{w} = \langle 3, 6, 9 \rangle

2w=2,4,6-2\mathbf{w} = \langle -2, -4, -6 \rangle (reverses direction and doubles length)

Example 3: Dot Product

u=3,4\mathbf{u} = \langle 3, 4 \rangle, v=2,5\mathbf{v} = \langle -2, 5 \rangle

uv=(3)(2)+(4)(5)=6+20=14\mathbf{u} \cdot \mathbf{v} = (3)(-2) + (4)(5) = -6 + 20 = 14

Angle check: cosθ=145×290.52\cos \theta = \frac{14}{5 \times \sqrt{29}} \approx 0.52, so θ59°\theta \approx 59°

Vector operations are fundamental in game development:

  • Movement: Add velocity vector to position vector each frame
  • Forces: Add multiple force vectors (gravity, thrust, friction) to get net force
  • Lighting: Dot product between surface normal and light direction determines brightness (Lambertian shading)
  • AI: Dot product helps determine if an enemy is facing toward or away from the player

Example: When your character jumps, you add a gravity vector (pointing down) to the current velocity vector every frame.

To add two vectors, you:
If you multiply a vector by $-2$, what happens?
The dot product of two vectors is:
If $\mathbf{u} \cdot \mathbf{v} = 0$, the vectors are:
In game lighting, the dot product is commonly used to calculate: