Vector

From Valve Developer Community
Revision as of 09:39, 29 August 2005 by InvaderZim (talk | contribs) (Could use a look over by someone else, i'm a little confusing)
Jump to navigation Jump to search


A Scalar is a value that has magnitude (a value)

A Vector is a value with both magnitude and direction, and as such has mulitple values

the most commonly seen format for a vector is {x,y,z}, the value show represent displacement values for the direction of the vector. a vector defined as {6,0,0} is pointing down the positive x-axis, and has a mganitude (or 'length') of 6

--Points vs Vectors and Homogenous Coordinates-- Vectors are not very much different from points, which are also comprised of an x,y, and z value. in many mathematics systems that use both vectors and points, and extra value is added, and the vector/point is said to be defined in homogenous coordinates, {x,y,z,w}. Points have a homogenous coordinate of 1, vectors have one of 0.

This would at first seem to be just a simple way to tell vectors and points apart, but it still serves a function.

consider two points: {1,1,1,1} and {0,0,1,1}. If we wanted to find the displacement between the two points we would use subtraction:

     {1,1,1,1} - {0,0,1,1}  =    {1,1,0,0}

hmm... that's a vector! This is the result that should be expected, as displacement is a vector value. The new vector now points from the first point to the second.

Attempting to add two points together is geometrically illogical, and the result here would be:

     {1,1,1,1} + {0,0,1,1}  =    {1,1,2,2}

the homogenous 'w' coordinate is now 2, neither a point nor a vector. However, adding a displacement to a point is still reasonable:

     {1,1,1,1} + {0,0,1,0}  =    {1,1,2,1}

the result is still a point

also, vectors can still be safely added and subtracted, since their homogenous coordinate is always zero.

--Vector Mathematics--

A normalized vector is one with a magnitude of 1. To achieve this divide a vector by its length |V| (see below)

Consider two vectors here called 'V' and 'W', they are not defined in homogenous coordinates There are several math operations that apply only to vectors such as: (these symbols are not built into c++, they are merely those that would normally appear in books on vector math)

 Symbol       Purpose                  C++ code
  |V|  ->  the length of vector V    = sqrt(v.x*v.x + v.y*v.y + v.z*v.z)  //Scalar
  V*W  ->  vector dot product        = v.x*w.x + v.y*w.y + v.z*w.z        //Scalar
  VxW  ->  vector cross product      = {v.y*w.z - w.y*v.z , v.z*w.x - w.z*v.x , v.x*w.y - w.x*v.y}  //Vector

These two apply only to normalized vectors:

  V*W  ->  dot product, its result will be the cosine of the angle between the two vectors
  VxW  ->  cross product, it will result in the sine of the angle between the two vectors

Also useful to know is that the cross product of any two vectors will always be perpendicular to both vectors (unless they both point in the same or opposite directions!)

example: the cross product of a vector pointing down the x-axis and a vector pointing down the y-axis is a vector pointing down the z-axis

this function is commonly used to generate surface normals based upon geometry. These are later used in lighting systems.

Consider a triangle made of the points {0,0,0},{1,0,0},{0,1,0} we compute the displacements from one point to the two others. (in this case we have {0,0,0} as a point and so the points themselves are also the displacements) then we take the cross product:

  {1,0,0}x{0,1,0} = {0,0,1} this also happens to be a vector pointing straight up perpendicular to the triangle

See Also

Vector