Game Development Reference
In-Depth Information
{
Vector position = cs.Sprite.GetPosition();
position.Y = 0 + Math.Sin((_totalTime + xAdvance) * frequency)*25;
cs.Sprite.SetPosition(position);
xAdvance++;
}
_totalTime += elapsedTime;
}
Vectors
Vectors are a very common game programming tool. The easiest way to get a
good feel for vector math is to use it in a game. Very quickly the possible uses will
become second nature. A simple vector class has already been made in the pre-
vious chapters, but until now none of its properties have been defined.
What Is a Vector?
In game programming, vectors are used to manipulate and describe 3D game
worlds. Mathematically, vectors are described as a direction with a magnitude.
The magnitude is just the length of the vector. See Figure 8.2.
The most common vectors in game programming are 2D, 3D, and 4D. Four-
dimension vectors are used when using projection matrices to translate vertices
from 3D to 2D. On paper, vectors and positions are very similar. A position [0, 3, 0]
and a vector [0, 3, 0] have the same internal values, but they represent different
things. The position uses a coordinate system to define an absolute position in
the world. A vector describes a direction (up in this case) and a magnitude or
length (in this case, 3). See Figure 8.3 for a comparison. For example, ''three
miles in the air'' is not a position; it's a description of a position using a direction
and a length—a vector.
magnitude
origin
Figure 8.2
The anatomy of a vector.
 
 
Search WWH ::




Custom Search