# Mathematics for 3D Game Programming and Computer Graphics – Vectors

Vectors are of fundamental importance in any 3D game engine. They are used to represent points in space, such as the locations of objects in a game or the vertices of a triangle mesh. They are also used to represent spatial directions, such as the orientation of the camera or the surface normals of a triangle mesh. Understanding how to manipulate vectors is an essential skill of the successful 3D programmer.

Throughout this book, we encounter vectors of various types, usually representing two-dimensional, three-dimensional, or four-dimensional quantities. For now, we make no distinction between vectors representing options and vectors representing directions, nor do we concern ourselves with how vectors are transformed from one coordinate system to another.

### The Dot Product

The dot product of two vectors, also known as the scalar product or inner product, is one of the most heavily used operations in 3D graphics because it supplies a measure of the difference between the directions in which the two vectors point.

$\mathbf{P} \cdot \mathbf{Q} = \sum_{i=1}^{m} P_i Q_i.$

This definition states that the dot product of two vectors is given by the sum of the products of each component. In three dimensions, we have

$\mathbf{P} \cdot \mathbf{Q} = P_x Q_x + P_y Q_y + P_z Q_z.$

The dot product $P \cdot Q$ may also be expressed as the matrix product:

$\mathbf{P}^T \mathbf{Q} = \begin{bmatrix}P_1 & P_2 & \cdots & P_n\end{bmatrix} \begin{bmatrix} Q_1 \\ Q_2 \\ \vdots \\ Q_n \end{bmatrix},$

Now for an important theorem that reveals the ubiquitous utility of the dot product.

Given two $n$-dimensional vectors $\mathbf{P}$ and $\mathbf{Q}$, the dot product $\mathbf{P} \cdot \mathbf{Q}$ satisfies the equation

$\mathbf{P} \cdot \mathbf{Q} = \lVert P \rVert \lVert Q \rVert \cos \alpha$

where $\alpha$ is the planar angle between the lines connecting the origin to the points represented by $\mathbf{P}$ and $\mathbf{Q}$.

### The Cross Product

The cross product of two three-dimensional vectors, also known as the vector product, returns a new vector that is perpendicular to both of the vectors being multiplied together. This property has many uses in computer graphics, one of which is a method for calculating a surface normal at a particular point given two distinct tangent vectors.

The cross product of two 3D vectors $P$ and $Q$, written as $P \times Q$, is a vector quantity given by the formula:

$P \times Q = < P_yQ_z - P_zQ_y, P_zQ_x - P_xQ_z, P_xQ_y - P_yQ_x >$

A commonly used tool for remembering this formula is to calculate cross products by evaluating the pseudodeterminant

$P \times Q = \begin{vmatrix} i & j & k \\ P_x & P_y & P_z \\ Q_x & Q_y & Q_z \end{vmatrix}$

##### Theorem 2.8

$\lVert P \times Q \rVert = \lVert P \rVert \lVert Q \rVert \sin \alpha$

where $\alpha$ is the planar angle between the lines connecting the origin to the points represented by $P$ and $Q$.

We know that any nonzero result of the cross product must be perpendicular to the two vectors being multiplied together, but there are two possible directions that satisfy this requirement. It turns out that the cross product follows a pattern called the right hand rule.

This site uses Akismet to reduce spam. Learn how your comment data is processed.