An introduction to Vector Math

A vector is an n-touple, a finite ordered list of element. An n-touple can have any number of dimensions, for games the number of dimensions (n) is usually 2, 3 or 4. An n dimensional vector \(\vec{V}\) is represented as follows:

$$ \vec{V} = (V_0, V_1, V_2 ... V_n) $$

The above notation uses subscripts, each subscript is a component of the vector. A common way to refer to a specific component is \(\vec{V}_i\), where \(i\) is the subscript being accessed. Subscripts start with 0.

In games, the components of a vector are often generalized to the axis they represent. Specifically, components 0, 1, 2 and 3 are often represented as x, y, z and w. The following notations are used interchangeably:

$$ (V_0, V_1, V_2, V_3) = (V_x, V_y, V_z, V_w) $$

What is in a vector?

A vector represents a magnitude and a direction. In games, the components of a vector are often used to measure signed displacement along an axis. For example, in a two dimensional vector the x component measures displacement on the x axis, while the y component measures displacement on the y axis.

Visually, a vector is drawn as an arrow. Because a vector does not have a position, it can be drawn anywhere! In the interactive example below, the blue arrow can be manipulated by dragging the triangle. All of the green arrows represent the same vector as they all have the same magnitude and direction, even tough they are graphed in different positions.

Canvas support required

Implementation

In C or C++ it's common to use a union or to overlod the [] operator to allow for a vector to be accessed by both numeric subscripts and x, y, z, w variables. I try to only provide pseudo-code for these blogs, all the code provided will use explicitly named members.

struct vec2 {  
    float x;
    float y;
}

struct vec3 {
    float x;
    float y;
    float z;
}

struct vec4 {
    float x;
    float y;
    float z;
    float w;
}