Up to date
This page is up to date for Godot
If you still find outdated information, please open an issue.
This tutorial is a short and practical introduction to linear algebra as it applies to game development. Linear algebra is the study of vectors and their uses. Vectors have many applications in both 2D and 3D development and Godot uses them extensively. Developing a good understanding of vector math is essential to becoming a strong game developer.
This tutorial is not a formal textbook on linear algebra. We will only be looking at how it is applied to game development. For a broader look at the mathematics, see https://www.khanacademy.org/math/linear-algebra
Coordinate systems (2D)¶
In 2D space, coordinates are defined using a horizontal axis (
x) and a
vertical axis (
y). A particular position in 2D space is written as a pair of
values such as
If you're new to computer graphics, it might seem odd that the
y axis points downwards instead of upwards, as you
probably learned in math class. However, this is common in most
computer graphics applications.
Any position in the 2D plane can be identified by a pair of numbers in this way.
However, we can also think of the position
(4, 3) as an offset from the
(0, 0) point, or origin. Draw an arrow pointing from the origin to the
This is a vector. A vector represents a lot of useful information. As well
as telling us that the point is at
(4, 3), we can also think of it as an
θ (theta) and a length (or magnitude)
m. In this case, the arrow
is a position vector - it denotes a position in space, relative to the
A very important point to consider about vectors is that they only represent relative direction and magnitude. There is no concept of a vector's position. The following two vectors are identical:
Both vectors represent a point 4 units to the right and 3 units below some starting point. It does not matter where on the plane you draw the vector, it always represents a relative direction and magnitude.
You can use either method (x and y coordinates or angle and magnitude) to refer
to a vector, but for convenience, programmers typically use the coordinate
notation. For example, in Godot, the origin is the top-left corner of the
screen, so to place a 2D node named
Node2D 400 pixels to the right and 300
pixels down, use the following code:
$Node2D.position = Vector2(400, 300)
var node2D = GetNode<Node2D>("Node2D"); node2D.Position = new Vector2(400, 300);
Godot supports both Vector2 and Vector3 for 2D and 3D usage, respectively. The same mathematical rules
discussed in this article apply to both types, and wherever we link to
Vector2 methods in the class reference, you can also check out their
The individual components of the vector can be accessed directly by name.
# Create a vector with coordinates (2, 5). var a = Vector2(2, 5) # Create a vector and assign x and y manually. var b = Vector2() b.x = 3 b.y = 1
// Create a vector with coordinates (2, 5). var a = new Vector2(2, 5); // Create a vector and assign x and y manually. var b = new Vector2(); b.X = 3; b.Y = 1;
When adding or subtracting two vectors, the corresponding components are added:
var c = a + b # (2, 5) + (3, 1) = (5, 6)