In the Beginning There Was the Vector

In the last chapter I told you that vectors shouldn't be mixed up with positions. This is not entirely true, as we can (and will) represent a position in some space via a vector. A vector can actually have many interpretations:

■ Position: We already used this in the previous chapters to encode the coordinates of our entities relative to the origin of the coordinate system.

■ Velocity and acceleration: These are physical quantities we'll talk about in the next section. While we are used to thinking about velocity and acceleration as being a single value, they should actually be represented as 2D or 3D vectors. They encode not only the speed of an entity (e.g., a car driving at 100 kilometers per hour), but also the direction the entity is traveling in. Note that this kind of vector interpretation does not state that the vector is given relative to the origin. This makes sense since the velocity and direction of a car is independent of its position. Think of a car traveling northwest on a straight highway at 100 kilometers per hour. As long as its speed and direction don't change, the velocity vector won't change either.

■ Directions and distances: Directions are similar to velocities but lack physical quantities in general. We can use such a vector interpretation to encode states such as this entity is pointing southeast. Distances just tell us the how far away and in what direction a position is from another position.

Figure 8-1 shows these interpretations in action.

Figure 8-1 shows these interpretations in action.

Figure 8-1. Bob, with position, velocity, direction, and distance expressed as vectors

Figure 8-1 is of course not exhaustive. Vectors can have a lot more interpretations. For our game development needs, however, these four basic interpretations suffice.

Figure 8-1 is of course not exhaustive. Vectors can have a lot more interpretations. For our game development needs, however, these four basic interpretations suffice.

One thing that's left out from Figure 8-1 is what units the vector components have. We always have to make sure that those are sensible (e.g., Bob's velocity could be in meters per second, so he travels 2 meters to the left and 3 meters up in 1 second). The same is true for positions and distances, which we could also express in meters, for example. The direction of Bob is a special case, though — it is unitless. This will come in handy if we want to specify the general direction of an object while keeping the direction's physical features separate. We could do this for the velocity of Bob, storing the direction of his velocity as a direction vector and his speed as a single value. Single values are also known as scalars. For this, the direction vector must be of length 1, as we'll discuss later on.

0 0

Post a comment