MathGem:Vector Operations
From GPWiki
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.
[edit] SymbolsA brief overview of mathematical symbols relevant to this page.
θ - angle between two vectors
[edit] Length Of A VectorA 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:
[edit] 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)); } [edit] 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. [edit] 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);} } } [edit] 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 [edit] 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 [edit] NormalizeNormalizing 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:
[edit] Ctypedef 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; } [edit] 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. [edit] 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; } } [edit] 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) [edit] 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 [edit] The Dot ProductThe dot product is very useful in game programming as it gives the angle between two vectors: [edit] Ctypedef 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); } [edit] 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. [edit] 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); } } [edit] 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] [edit] 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 [edit] The Cross ProductThe 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: or (in terms of dot product):
Visually, the orientation of a cross product resultant can be seen here:
[edit] Ctypedef 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; } [edit] 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. [edit] 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 ); } } [edit] 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) [edit] 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 Categories: MathGem | C | Python | VB | Programming Techniques |
- common generic vectors
- visual representation of a vector
- generic unit vectors (length = 1)
- length of a vector
- dot product (of
and
)
- cross product (of
. In practice, however, each component (X, Y, and Z) of the vector is divided by it's length seperately.
where
when the two vectors are placed so that their tails coincide.



