LerpworksAcademy
On this page

Lesson by steps

Vectors: arrows you can add

19 steps

  • Why this matters
  • A vector is a step
  • Addition: two steps in a row
  • Subtraction: how to get from A to B
  • Multiplying by a number: longer, shorter, backward
  • Length: the Pythagorean theorem with nothing to memorize
  • Normalizing: direction without distance
  • Three numbers, one arrow
  • Where it breaks
  • Connections: the same idea in other fields
  • Key points

Math and physics › Vectors

Vectors: arrows you can add

This lesson is part of the course “Math in plain words”.

A vector is not a formula, and it is not the “directed line segment” of a textbook. It is a step: how far to go, and which way. Almost all the math in games is built on steps like this: position and velocity, the direction to an enemy, wind, the normal of a surface, even the color of a pixel. This page shows what a vector looks like in a picture, then four things you can do with one: add, subtract, multiply by a number and measure its length. After that, formulas from shaders and game logic read like ordinary sentences.

Why this matters

Open any material, any character-movement setup or any lesson on graphics, and you find vectors. “Position plus velocity times time.” “Normalize the direction to the target.” “Dot product of the normal and the light.” If no picture stands behind the word “vector,” these phrases are spells, and you have to memorize them.

In school, vectors usually arrive as rules for numbers: add the coordinates, take the square root of the sum of squares. The rules are correct, but without a picture they do not stick. This page works the other way around. The picture and the meaning come first, then the rule, so you can see why the rule has to be what it is.

A vector is a step

Picture a treasure map: “from the old oak, three paces east and two paces north.” That is an instruction for getting from one place to another, and it holds two things: which way to go and how far. On paper you draw it as an arrow. The tail is where you start, and the tip is where you arrive.

An instruction like this is a vector. You write it as numbers, one for each axis: (3, 2) means three to the right and two up. You measure steps along directions called axes: the x axis points right, and the y axis points up. The numbers themselves are the components of the vector. In 3D there is a third axis, so a vector takes three numbers: (x, y, z).

The key property of a vector is that a step has no location. “Three east and two north” can be walked from the oak, from a rock or from the river, and it is the same instruction. So you can move the arrow anywhere, and the vector does not change. It stays the same vector as long as the arrow keeps its length and its direction.

Try it

Drag the tip of the arrow to change the step and its numbers. Drag the tail, and the arrow moves while the numbers stay the same: a vector is not tied to a location. The dashed lines show the steps along the axes that make up the vector. The slider multiplies the vector by a number.


Two things to notice. If you drag the arrow by its tail, the numbers do not change: a vector does not know where it is drawn. And a negative number is a step in the opposite direction: (−2, 1) is two to the left and one up.

A point and a vector

So what is a point? A point is a place: “the oak stands right here.” A vector is a move: “three paces east from here.” It is like the difference between the address of a house and the directions “from the bus stop, turn right and go to the end of the street.”

You can ask about any point, though: which step leads to it from the origin? The point (5, 3) is where the step (5, 3) takes you when you start at the origin of the world. That is why engines store points and vectors the same way, as three numbers, and why they appear side by side in formulas. They still mean different things, and the meaning tells you which operations make sense:

  • point + vector = point: “three paces east of the oak” is a new place;
  • point − point = vector: how to get from one place to another;
  • vector + vector = vector: two steps in a row;
  • point + point makes no sense: “the place of the oak plus the place of the river” depends on where the map’s origin is.

The lesson on spaces shows how the graphics card tells the two apart: a point has a fourth number, w = 1, and a vector has w = 0.

Addition: two steps in a row

First go three east and one north, then one east and two north. Where do you end up? In total you went 3 + 1 = 4 east and 1 + 2 = 3 north. The result is (4, 3).

(3, 1) + (1, 2) = (3 + 1, 1 + 2) = (4, 3)

That is the whole rule for addition: x with x, y with y. Why does it work? East steps do not affect north steps, so east steps add up with east steps, and north steps with north steps. In a picture this is the tip-to-tail method: put the tail of the second arrow at the tip of the first. Walk the first arrow, then walk the second from where the first one ended. The arrow from the start to the end of the whole path is the sum.

Order does not matter. Taking the first step and then the second, or the second and then the first, ends in the same place: the two paths go around a parallelogram from opposite sides and meet at the far corner.

Try it

In Sum mode, arrow b starts where a ends, and the sum runs from the start of the path to its end. Drag the tips to change the arrows. The checkbox shows the other order: b first, then a. In Difference mode both arrows start at the origin, like the addresses of two points. There b − a is the step from the tip of a to the tip of b.


Where this shows up in games:

  • Movement. New position = old position + the step for one frame. That step is the velocity, also a vector, multiplied by the frame time.
  • Current and wind. A boat rows north, and the current pushes it east. The sum of the two vectors shows where the boat really goes.
  • Color. The color of a pixel is also a vector of three numbers (R, G, B). The light of two lamps adds up like vectors, channel by channel, as the lesson on color shows.
  • Texture scrolling. An address in a texture is a vector (u, v). Add a small vector that grows with time, and the texture drifts like water.

Subtraction: how to get from A to B

The most common question in a game is how to get from here to there. Where is the enemy relative to me? Which way should the projectile fly? The answer is subtraction:

step from A to B = B − A          // “end” minus “start”

I am at (2, 1), the enemy is at (8, 9):   enemy − me = (8 − 2, 9 − 1) = (6, 8)

Check it: add (6, 8) to my position and you get (8, 9), the enemy’s position. Subtraction answers the question “what do I add to A to get B?”

The order matters here. Me − enemy = (−6, −8): the same arrow turned around, from the enemy to me. Mixing up the order is a classic mistake: an enemy that should run to the player runs away from the player instead. You do not need to memorize a rule. Ask yourself: end minus start.

In Difference mode of the demo above you can see the same thing in a picture: b − a is the arrow from the tip of a to the tip of b.

Multiplying by a number: longer, shorter, backward

Take the step (3, 1) twice and you get (6, 2). To multiply a vector by a number is to make the step that many times longer without changing its direction. Each component is multiplied by the number: a doubled step is twice as far east and twice as far north.

  • × 2: twice as long, same direction;
  • × 0.5: half as long;
  • × (−1): same length, opposite direction;
  • × 0: no step at all, (0, 0).

An ordinary number, as opposed to a vector, is called a scalar. It only changes the scale, hence the name. A speed of 5 m/s, a frame time of 0.016 s and a wind strength of 0.3 are all scalars. The most common movement formula is a direction multiplied by a speed: “which way” times “how fast.”

Try the “Multiply by” slider in the first demo. The arrow stretches, shrinks and flips around, but it stays on the same line.

Length: the Pythagorean theorem with nothing to memorize

Take the step (3, 4): three east and four north. How long is the step if you cut straight across instead of following the grid? Along the grid it is 3 + 4 = 7, but the straight line is shorter. The arrow, the step along x and the step along y form a right triangle, and the Pythagorean theorem gives the length of the arrow, also called its magnitude:

length² = x² + y²          // 3² + 4² = 9 + 16 = 25
length  = √(x² + y²)       // √25 = 5

in 3D:   length = √(x² + y² + z²)

What does that mean in a picture? Build a square on each of the three sides of the triangle. The area of the square on the long side equals the sum of the areas of the other two: 25 = 9 + 16. The “Under the hood” block below shows why, with a proof that has nothing to memorize.

Try it

Drag the tip. A square is built on each side of the triangle, with its area inside. However you change the arrow, the big square equals the two small ones together.


The most useful consequence is that the distance between two points is the length of the vector between them. How far away is the enemy? The length of (enemy − me) is the length of (6, 8), which is √(36 + 64) = 10 meters.

Why the Pythagorean theorem works: a proof in a picture
c² a² b²
The big squares on the left and right are identical, and so are the triangles, whose short sides are a and b. What is left over is one square on the long side (left) or two squares on the short sides (right).

Take a square with side a + b and four identical right triangles with short sides a and b. Arrange the triangles in two ways. On the left they sit in the corners, and a tilted square is left in the middle. Its side is c, the long side of the triangle. On the right, the same triangles are paired up into two rectangles, which leaves two squares: a × a and b × b.

The big square is the same and the triangles are the same, so the free area is the same too: c² = a² + b². You do not need to memorize anything. It is enough to remember the picture.

In 3D you apply the rule twice. First you find the length of the step across two axes, √(x² + y²). Then that length and the third number z form a right triangle again, and the length of the whole step is √(x² + y² + z²).

Normalizing: direction without distance

Often you need only “which way,” without “how far”: where to turn the head, where the wind blows, where a surface faces. For that you divide the vector by its length. What is left is an arrow with the same direction and a length of exactly 1. This is called normalizing the vector, and a vector of length 1 is a unit vector.

direction = vector / length

enemy − me = (6, 8), length 10   →   direction = (0.6, 0.8)
run toward the enemy at 5 m/s:   (0.6, 0.8) × 5 = (3, 4) per second   // length exactly 5

A direction of length 1 is handy because you can multiply it by any length you need: a speed, a force, a distance. Without normalizing, the distance to the target leaks into the step. An enemy 100 meters away would run 10 times faster than an enemy 10 meters away.

Try it

The character on the left runs toward the target. Drag the target closer and farther away, and compare the two ways of computing the step per second. The circle marks where the character will be in one second.


Unit vectors are everywhere in graphics. A normal is a unit vector that says which way the surface faces. The direction to the sun is a unit vector. They are kept at length 1 because then the dot product of two such arrows gives only the angle, with no length mixed in. The next lesson covers this.

Three numbers, one arrow

In engines, a vector is a set of two, three or four numbers. In shaders such sets are called float2, float3 and float4, and in Unreal Blueprint the type is Vector. The same kind of set stores things that do not look like arrows at first glance:

WhatNumbersWhat kind of step
Position(x, y, z)the step from the origin of the world to the object
Velocity(x, y, z) in m/sthe step for one second
Normal(x, y, z), length 1which way the surface faces
Color(R, G, B)how much red, green and blue
Texture address(u, v)the step from the corner of the texture

That is why the same operations work for all of them. Multiply a color by 2, and it becomes twice as bright: the ratio of the channels stays the same, and so does the hue. Multiply UV by 2, and you get tiling. Every address is twice as far from the corner, so the texture repeats twice.

Where it breaks

  • An enemy races toward a distant target but crawls toward a close one. The step is computed as (target − me) × speed, with no normalizing, so the distance has leaked into the speed. Normalize the direction first, then multiply by the speed.
  • An enemy runs away instead of chasing. The subtraction is the wrong way round: me − target instead of target − me. Check it: end minus start.
  • A character runs faster diagonally than straight. “Right” and “up” are both pressed, and the input (1, 1) has length √2 ≈ 1.41, which is 41% more. Normalize the input, or limit its length to 1.
  • An object vanishes or acts strangely when the target is exactly where the object is. You normalized the zero vector, which divides by zero. Check the length before you normalize.
  • Speed depends on the frame rate. The velocity is added to the position every frame but is not multiplied by the frame time. At 120 frames per second everything then moves twice as fast as at 60. The lesson on motion covers this.
  • The numbers add up correctly, but the result is meaningless. You added two points, or vectors from different spaces. Points are never added to points, and vectors must first be converted into one space.

Key points

  • A vector is a step: how far to go, and which way. The numbers (x, y, z) say how much of the step falls on each axis. A vector is not tied to a location, so you can move the arrow.
  • A point is a place, and a vector is a move. Point + vector = point, point − point = vector, and a point is never added to a point.
  • Addition is two steps in a row: x with x, y with y. Order does not matter.
  • Subtraction B − A is the step from A to B: end minus start. Its length is the distance between the points.
  • Multiplying by a number changes the length, and a minus turns the arrow around.
  • The length comes from the Pythagorean theorem: √(x² + y² + z²). To compare distances, you can skip the root.
  • Normalizing keeps the direction and makes the length 1. Velocity = direction × speed. Without normalizing, the distance leaks into it.

Test yourself

8 questions. Your best result goes to your profile; take it as many times as you like.

1. A character stands at (2, 1), and a chest is at (5, 5). Which vector leads from the character to the chest?

2. A boat sailed 3 km east, then 4 km north. How far is it from the start in a straight line?

3. An enemy runs toward the player with the formula “step = (player − enemy) × speed.” What will the player notice?

4. What does normalizing a vector do?

5. What do you get if you multiply the vector (2, −1) by −3?

6. The player presses “right” and “up” at the same time, so the input is (1, 1). Why does the character run faster diagonally?

7. Two house positions were added: (10, 20) + (30, 4) = (40, 24). What does the result mean?

8. The color (0.2, 0.4, 0.6) was multiplied by 2. What happened to the color?

Something still unclear? Select that piece of text and press “Ask about this”. The question goes to the author, and the place gets rewritten more clearly.