Think about a short walk
“Three steps right and four steps down” describes a movement even before we know where the walk begins. We can write that movement as (3, 4). The same vector can start at many places; its direction and size stay the same. A position such as (20, 10) is an address relative to an origin, while the vector (3, 4) is an offset we can add to that address. Keeping that distinction clear prevents many programming mistakes.
A character starts at (20, 10). Add the movement (3, 4): the new position is (23, 14). To return to the start, add the opposite movement (−3, −4). If the move needs to be twice as large, multiply both parts by 2 to get (6, 8).
Three operations you will use constantly
| Operation | Rule | Example | Meaning |
|---|---|---|---|
| Add | add matching parts | (2, 1) + (3, 4) = (5, 5) | do one move, then another |
| Subtract | subtract matching parts | (5, 7) − (2, 3) = (3, 4) | the move from one point to another |
| Scale | multiply each part | 3 × (1, 2) = (3, 6) | same direction, different size |
Subtraction is the secret weapon. To find the arrow from a player at P to a target at T, compute T − P. Read it as “where I want to be, minus where I am.” If you get it backward, the arrow points the wrong way and an enemy runs away from the player.
Length: the Pythagorean theorem returns
The movement (3, 4) is not seven units long. The horizontal and vertical steps form a right triangle, so the Pythagorean theorem gives its straight-line length: 3² + 4² = 9 + 16 = 25, and √25 = 5. That result helps a game decide whether a player is within reach of an object.
The player is at (2, 3). A coin is at (5, 7). The offset is (5 − 2, 7 − 3) = (3, 4), with length 5. If the pickup radius is 6, the coin is collected. If the radius is 4, it is not.
A handy shortcut: compare squared lengths instead. 3² + 4² = 25 and the radius squared is 36. Since 25 < 36, the coin is within reach, and the program skipped a square root.
Direction without size: normalizing
Sometimes we only care which way an arrow points. Dividing a vector by its own length produces a unit vector, which has length 1. The unit vector for (3, 4) is (3/5, 4/5) = (0.6, 0.8). Multiply that by any speed, and you get a movement of exactly that size in that direction. This is how a game makes diagonal movement fair.
Diagonal speed boost. If “right” adds (1, 0) and “down” adds (0, 1), pressing both adds (1, 1), whose length is √2 ≈ 1.41. The character moves about 41% faster diagonally. Normalizing the input vector before multiplying by speed fixes it.
var speed := 200.0
func _process(delta):
var input := Vector2(
Input.get_axis("ui_left", "ui_right"),
Input.get_axis("ui_up", "ui_down"))
# normalized() keeps diagonal moves at the same speed
position += input.normalized() * speed * deltaDrag the sliders to build a vector.
Geometry gives us the right triangle and the Pythagorean theorem; algebra gives us the distance formula, d = √((x₂ − x₁)² + (y₂ − y₁)²), which is the same theorem written for two coordinate pairs. Algebra II and physics classes may call the pair of changes a vector. In a game, it becomes an instruction: move three units across and four down.
Where you will see it
- Movement: velocity and input direction.
- Aiming: the arrow from a turret to its target.
- Physics: forces such as gravity and wind.
- Interfaces: how far the mouse moved during a drag.
Exercises
What is the vector from (10, 2) to (4, 10)?
(4 − 10, 10 − 2) = (−6, 8). Its length is √(36 + 64) = √100 = 10.
What is the unit vector in the direction of (−6, 8)?
Divide by the length 10: (−0.6, 0.8).
An enemy at (0, 0) chases a player at (6, 8) at 50 pixels per second. What velocity vector should it use?
The direction is (6, 8) ÷ 10 = (0.6, 0.8). Times 50 gives (30, 40) pixels per second.
