Section 1: Vectors and directions
Why game maths is mostly vectors
Open any game script and count the variables that hold positions, directions, velocities or offsets. In most games, that is a large share of the code. All of those are vectors, and getting comfortable with them makes a big part of gameplay programming feel simple.
A vector is a list of numbers, usually two in 2D games (x, y) or three in 3D (x, y, z). The same type is used for two different ideas, and confusing them is the source of many bugs:
- A point: a location in the world, such as the player's position.
- A direction with a length: a displacement, velocity or force, such as "3 metres to the right".
Subtracting two points gives a direction. Adding a direction to a point gives a new point. Adding two points together, however, does not mean anything useful, and if you find yourself doing it, something is probably wrong.
The questions vectors answer
Throughout this course, vectors will answer questions like:
- Where is the enemy relative to me? (subtraction)
- How far away is it? (length)
- Which way should I move to get there? (normalisation)
- Is it in front of me or behind? (dot product)
- Is it to my left or my right? (cross product, or a 2D equivalent)
- How do I move smoothly toward it? (interpolation)
Coordinate systems vary
Engines disagree about axes. In Godot 2D and most 2D frameworks, y points down the screen. In Godot 3D and Unity, y points up. Unity uses a left handed coordinate system, Godot 3D a right handed one, which changes the sign of cross products and which way rotations turn. When a formula from the internet does the opposite of what you expect, check the coordinate system first.
How to use this course
Each lesson has a small interactive demo in the course project. Before reading the code, play with the demo and try to predict what will happen. Building intuition is the goal. Formulas are easy to look up; knowing which one you need is the skill that matters.
In the next lesson we look at basic vector operations and implement a simple seek behaviour.