Vectors
- Cartesian coordinates are 2-D vectors.
- You represent 3-D locations by 3-D vectors. They're just 3-tuples or
3 scalar (real or floating point) numbers.
- That's the way most games represent positions.
- You can have much higher dimensional vectors.
- You add two vectors by adding the components. (5,5,5) + (3,2,-1) =
(8,7,4)
- The length of a vector is derived from the pythagorean theorem;
squareRoot(x^2+y^2+z^2) (other dimensions just add in). So the
length of (8,7,4) = squareRoot(8*8+7*7+4*4) = squareRoot(64+49+16) =
squareRoot(129) ~= 11.36 (I used the calculator for that.)
- A vector times a scalar is just the consituents multiplied
so (8,7,4)*3 = (24,21,12).
- You can normalize the vector (to get the direction on the unit
circle which is surprisingly useful) by dividing the components
by the magnitude. (8/11.36,7/11.36, 4/11.36)
- The dot product of two vectors is the sum of the product of the
consituents.
- (8,7,4).(3,2,1) = 8*3+7*2+4*1 = 24+14+4 =42
- The angle a between them is u.v=|u||v|cos(a)
- cos(a) = (u.v)/|u||v|
- |u|~=11.36; |v|= squareRoot(9+4+1) = squareRoot(14) ~= 3.74
- cos(a) ~= .98 -> a ~= .2 radians