MathGem:Vector Operations

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Contents

Symbols

A brief overview of mathematical symbols relevant to this page.


\vec u, \vec v, \vec w - common generic vectors

\begin{bmatrix} x \\ y \\ z \end{bmatrix} - visual representation of a vector

\hat u, \hat v, \hat w - generic unit vectors (length = 1)

\left | \vec v \right | - length of a vector

θ - angle between two vectors

\vec v \cdot \vec w - dot product (of \vec v and \vec w)

\vec v \times \vec w - cross product (of \vec v and \vec w)


Length Of A Vector

A simple yet powerful operation, lengths of a vector are used in a variety of the more complex vector matrix operations. Also, they can be used to determine the distance between two objects, by creating a vector from the subtraction of the two objects' coordinates and then taking the length of that vector:

\left | \vec v \right | = \sqrt{{\vec v}_x^{~2} + {\vec v}_y^{~2} + {\vec v}_z^{~2}}

C

#include <math.h>
 
typedef struct Vector3_
{
  double x;
  double y;
  double z;
} Vector3;
 
double length(Vector3 *v)
{
  return (sqrt(v->x*v->x + v->y*v->y + v->z*v->z));
}

C++

#include <cmath>
 
struct Vector3
{
  double x;
  double y;
  double z;
};
 
double length(Vector3 const &v)
{
  return (std::sqrt(v.x*v.x + v.y*v.y + v.z*v.z));
}

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

C#

public class Vector3
{
  public double X;
  public double Y;
  public double Z;
 
  public double Length
  {
    get{return System.Math.Sqrt(X*X + Y*Y + Z*Z);}
  }
}

Python

# v is a tuple representing a 3d vector
def length(v):
    return (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) ** 0.5

Visual Basic

' In Public Object Module:
Public Type Vector3
  X As Double
  Y As Double
  Z As Double
End Type
 
Public Function LENGTH(v as Vector3) As Double
  LENGTH = (v.x^2 + v.y^2 + v.z^2) ^ .5
End Function

Normalize

Normalizing a vector forms the basis of many more advanced vector operations. The process takes a vector of any length and preserves only its direction information (gives it a length of 1). This is useful when projecting one matrix into the axis of another with a dot product. Mathematically, a normalized vector is called a unit vector and can be obtained easily by dividing the vector by it's length: \hat v = \frac{\vec v}{\left | \vec v \right |}. In practice, however, each component (X, Y, and Z) of the vector is divided by it's length seperately.


C

typedef struct Vector3_
{
  double x;
  double y;
  double z;
} Vector3;
 
void Normalize(Vector3 *v)
{
  double len = length(v);
  v->x /= len;
  v->y /= len;
  v->z /= len;
}

C++

struct Vector3
{
  double x;
  double y;
  double z;
};
 
void Normalize(Vector3 &v)
{
  double len = length(v);
  v.x /= len;
  v.y /= len;
  v.z /= len;
}

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

C#

public class Vector3
{
  public double X;
  public double Y;
  public double Z;
 
  public void Normalize()
  {
    // Vector3.Length property is under length section
    double length = this.Length;
 
    X /= length;
    Y /= length;
    Z /= length;
  }
}

Python

# v is a tuple representing a 3d vector
def normalize(v):
    len = length(v);
    return (v[0] / len, v[1] / len, v[2] / len)

Visual Basic

' In Public Object Module:
Public Type Vector3
  X As Double
  Y As Double
  Z As Double
End Type
 
Public Function NORMALIZE(ByRef v as Vector3)
  Dim VectorLen As Double
  VectorLen = LENGTH(v)
  v.X = v.X / VectorLen
  v.Y = v.Y / VectorLen
  v.Z = v.Z / VectorLen
End Function

The Dot Product

The dot product is very useful in game programming as it gives the angle between two vectors: \cos \theta = \frac{\vec v \cdot \vec w}{ \left | \vec v \right \vert \left | \vec w \right \vert} where θ is the angle between vectors \vec v and \vec w. On its own, the dot product is the length of the projection of \vec v onto the unit vector \hat w when the two vectors are placed so that their tails coincide.

C

typedef struct Vector3_
{
  double x;
  double y;
  double z;
} Vector3;
 
double dot(Vector3 *v, Vector3 *w)
{
  return (v->x*w->x + v->y*w->y + v->z*w->z);
}

C++

struct Vector3
{
  double x;
  double y;
  double z;
};
 
double dot(Vector3 const &v, Vector3 const &w)
{
  return (v.x*w.x + v.y*w.y + v.z*w.z);
}

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

C#

public class Vector3
{
  public double X;
  public double Y;
  public double Z;
 
  public static double Dot(Vector3 v, Vector3 w)
  {
    return (v.X*w.X + v.Y*w.Y + v.Z*w.Z);
  }
}

Python

# v and w are tuples representing 3d vectors
def dot(v, w):
    return v[0]*w[0] + v[1]*w[1] + v[2]*w[2]

Visual Basic

' In Public Object Module:
Public Type Vector3
  X As Double
  Y As Double
  Z As Double
End Type
 
Public Function DOT(v as Vector3, w as Vector3) As Double
  DOT = v.x*w.x + v.y*w.y + v.z*w.z
End Function

The Cross Product

The cross product is a mathematical operation that will return a resultant vector that is orthogonal to the two vectors used to calculate it and has a length relative to the lengths of the vectors and the angles between them: \left | \vec u \times \vec v \right | = \left | \vec u \right | \left | \vec v \right | \sin \theta

or (in terms of dot product):

\left | \vec u \times \vec v \right | = \left | \vec u \right | \left | \vec v \right | \sqrt{1-(\vec u \cdot \vec v)^2}

Visually, the orientation of a cross product resultant can be seen here:

Files:cross.gif


The cross product becomes very useful when calculating normals (vector orthogonal to a surface), which are used extensively in 3D game programming.

C

typedef struct Vector3_
{
  double x;
  double y;
  double z;
} Vector3;
 
Vector3 cross(Vector3 *v, Vector3 *w)
{
  Vector3 c = {
    v.y*w.z - v.z*w.y,
    v.z*w.x - v.x*w.z,
    v.x*w.y - v.y*w.x };
  return c;
}

C++

struct Vector3
{
  double x, y, z;
  
  Vector3(double set_x, double set_y, double set_z): 
  x(set_x), y(set_y), z(set_z) 
  {}
};
 
Vector3 cross(Vector3 const &v, Vector3 const &w)
{
  return Vector3(
    v.y*w.z - v.z*w.y,
    v.z*w.x - v.x*w.z,
    v.x*w.y - v.y*w.x );
}

Note that there's a Complete math::vector Class elsewhere on the wiki that you can use instead of writing your own.

C#

public class Vector3
{
  public double X;
  public double Y;
  public double Z;
 
  public static Vector3 Cross(Vector3 v, Vector3 w)
  {
    return new Vector3(
      v.Y*w.Z - v.Z*w.Y,
      v.Z*w.X - v.X*w.Z,
      v.X*w.Y - v.Y*w.X );
  }
}

Python

# v and w are tuples representing 3d vectors
def cross(v, w):
    x = v[1]*w[2] - v[2]*w[1]
    y = v[2]*w[0] - v[0]*w[2]
    z = v[0]*w[1] - v[1]*w[0]
 
    return (x, y, z)

Visual Basic

' In Public Object Module:
Public Type Vector3
  X As Double
  Y As Double
  Z As Double
End Type
 
Public Function CROSS(v as Vector3, w as Vector3) As Vector3
  CROSS.X = v.Y*w.Z - v.Z*w.Y
  CROSS.Y = v.Z*w.X - v.X*w.Z
  CROSS.Z = v.X*w.Y - v.Y*w.X
End Function

Back To Programming Techniques