Click here to Skip to main content
15,886,873 members
Articles / STL

3D Game Math Primer 4: Operations on Vectors

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Dec 2012CPOL5 min read 9.9K   2  
3D Game Math Primer 4: Operations on Vectors

In a 3D game environment, vectors are used to hold the values of points or vertices. Thus a vector would contain a coordinate [x, y, z] which represents the specific vertex in 3D space. Points are defined such by vectors because the start position of the vector is usually taken as [0, 0, 0] which is the origin of the coordinate space. Thus all the vertices in 3D models and game elements are represented by vectors, but it is important to remember that vectors are not 3D vertices. (There is another type of vector that is represented by 4 coordinates [x, y, z, w], which are homogenous coordinates. We will talk about this in a later post dedicated solely to this very important and interesting topic).

Another place where vectors are used are surface normals. Every 3D surface has a surface normal, which is nothing but a vector pointing away from the surface and perpendicular to that surface. This vector (surface normal) determines how light sources in the environment light up the specific surface. This is just one aspect of the surface normal, as it is used in many other places in the game. Vectors are also used in shading (which we will talk about later), and dynamically processing visual elements in the game.

Simply put, vectors are one of the most used constructs in 3D games. This is why learning and understanding about vectors and their operations is very important. Without further ado, let's dive in to learn about vector operations and some basic linear algebra.

The Zero Vector

The zero vector is the additive identity in the set of vectors. The 3D zero vector would be denoted by [0, 0, 0]. This vector is a special vector because it is the only vector with no magnitude or direction. It would be easy to assume the zero vector as a point, but the reader should remember that a point represents a location. It is better to remember the zero vector as a vector with zero displacement.

Vector Negation

Vectors negation can be related to multiplication of scalar numbers by -1. The negated vector is known as the additive inverse of the original vector. A vector of any dimension is negated by all its individual components as shown below:

  • - [x, y] = [-x, -y]
  • -[x, y, z] = [-x, -y, -z]

Geometrically speaking, vector negation produces a vector same as the original, but with opposite direction.

Magnitude of a Vector

Vectors have a magnitude (length) which can be calculated simply by taking the root of the sum of squares of the individual dimensions. This is very simple if you remember the very basic skill of calculating the length of the hypotenuse in a right angled triangle using the Pythagorean theorem (for 2 dimensions).

For a 2D and 3D vector, the magnitudes are defined as below, respectively:

2D3D

Vector Multiplication and Division by a Scalar

Vectors can be multiplied by scalars, and this happens by multiplying the individual components of the vector by the scalar value. What we geometrically obtain by scalar multiplication is another vector that is parallel to the original vector, but which could differ by magnitude or direction, depending on the scalar value. Some examples are given below:

k[x, y, z] = [kx, ky, kz], -2[4, 0, 1] = [-8, 0, -2]

Vectors can be divided by scalars as well, and this would be equivalent to multiplying the vector with the reciprocal of the scalar value, which is shown as follows:

1/k[x, y, z] = [x/k, y/k, z/k]

Some important aspects to note:

  • We do not use the multiplication sign in scalar-vector multiplication, nor the division sign.
  • Multiplication and division take precedence over addition and subtraction.
  • Vector negation is a special case of scalar multiplication, where the scalar value is always -1.
  • The geometric interpretation of scalar – vector multiplication is the scaling of the vector by a magnitude of |k|, the scalar value.

Vector Normalization

In many situations, it is not the magnitude of the vector that is important, but the direction. In these cases, it is convenient to work with unit vectors, which have the same direction of the original vectors, but their magnitude is 1. The process of taking a vector, and converting it into a vector of magnitude 1 while maintaining the direction, is known as vector normalization. The unit vector is known as the normal.

A vector is normalized by dividing the vector by its magnitude (scalar division, as the magnitude value is a scalar). The result is a vector which is the normal to the given original vector:

Vnorm = V/||V||, Where V is not zero.

The below image (courtesy of the book 3D math primer for graphics and games development, by F.Dunn and I.Parberry) shows unit vectors in 2D space, which touches the surface of a circle of unit radius. In 3D space, unit vectors would touch the surface of a sphere of unit radius:

unitVectors

Vector Addition and Subtraction

Vectors can be added or subtracted only when their dimensions are equal. The individual components of the vectors are added or subtracted to obtain the resultant vector. Though vector addition is commutative, vector subtraction is not. Examples of vector addition and subtraction are given below:

[2, 5, -1] + [3, 1, 0] = [5, 6, -1]

[3, 0, -3] – [5, -2, 0] = [-2, 2, -3]

The geometrical concept of vector addition and subtraction is the basic triangle rule. Given the vector addition of two vectors A and B as A+B, we need to find the resultant vector which has the starting position of A, but the ending position of B. This can be applied to many vectors. Vector addition may seem a simple enough concept, but we will later see a similar mechanism to transform vectors from one coordinate space to another.

In the next post, we will continue the rest of the vector operations, and look at two very important operations: Vector dot product and the vector cross product.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead Exilesoft
Sri Lanka Sri Lanka
Mark is a Technical Lead at Exilesoft, whose passion lies in coding, mentoring, and fueling technical innovation. In the past, he has worked as a developer for a product engineering company, an ERP/Technical Consultant in a Fortune 500 conglomerate, and also as a senior engineer for a startup in the manufacturing and design space. His current areas of research revolve around Enterprise Architecture, Big Data, NoSQL Technology, and Machine Learning.

In his spare time, Mark experiments with (computer) game design/development, operating system internals, and compiler design. He also discusses and blogs about various topics surrounding software development, computer science, game programming, and mathematics, which can be read at markfaction.wordpress.com. Feel free to email or message him anytime and strike up a conversation.

Comments and Discussions

 
-- There are no messages in this forum --