Vectors


You've seen vectors already within this book, and it's possible you think that they are simply a representation of a point in space, such as a vertex. Although it's true that the vector can store this information, it does so in the DirectX API mainly for a convenience. The mathematical definition of a vector is an object that denotes both direction and velocity (or magnitude). For example, if you fire a bullet out of a tank (for which you will write code in the next game), that bullet has both a direction and a velocity. They are stored in one vector, which incidentally has the same members as a vertex, namely x, y, and z components. You're probably thinking, wait, how can you specify these two things with a single set of three coordinates? Look at Figure 10.7 for an example.

Figure 10.7. Two different vectors.


You'll notice that in this figure, each of the two vectors has similar directions, but the velocity is different. In practice, you can have these vectors measure both direction and velocity because most times a vector is defined as a free vector, which is when the root of the vector is at the origin. Thus, the single set of coordinates are where the arrow of your vector is, and you now know the direction and velocity of that vector by taking those two points. Vectors do not have to be rooted at the origin, but it certainly makes things easier for most calculations, as you saw during the rotation example earlier this chapter.

How do you calculate the velocity of a vector knowing its arrow coordinates? The formula is derived from the Pythagorean Theorem (a2 + b2 = c2). You can find out more information about this formula in many different places, but it goes beyond the scope of what you're trying to learn here, which is the length of a vector. This formula can be defined as the following:


When dealing with the vector classes while you're developing, you'll notice that the vector has a length method that you can use to calculate this for you, but it's important to see the formula to understand that calculating the length can be expensive. Squaring each of the components and adding them together isn't that bad, but taking the square root of that result can be an expensive operation. You'll notice that the vector classes also have a LengthSq method that you can use to return the squared length of the vector to avoid this expensive step. Many times, it is "less expensive" to square the length you're comparing against than for you to take the square root of the length of this vector. This point is something you'll want to remember.

It isn't all that exciting, though: what about adding even more knowledge to your repertoire and doing some addition, subtraction, or multiplication of some vectors? Addition is basic because it is much like translation from earlier; you simply add each new component to the existing component to form the new vector. Subtracting is the same as adding; you just negate the new component before the addition. See Figure 10.8 for a visual representation of these concepts.

Figure 10.8. Adding and subtracting vectors, oh my!


Multiplication with a vector is exceedingly simple as well; you simply multiply each component of the vector with a single constant value.

One of the important aspects of vectors that you will use numerous times throughout this book is something called a normalized vector or unit vector. It is just a fancy way of saying a vector with a length of 1. This vector is extremely important because it can store direction without regard to velocity, which allows you to change direction without changing velocity by first calculating the length of your vector, normalizing it, modifying the direction, and then multiplying it by the calculated length. These calculations cause you to have a new vector pointing in a new direction with the same velocity.

Calculating the normalized version of a vector is really pretty simple: you simply take each component of the vector and divide it by the length of the vector. Imagine you had a free vector with coordinates (23,18,7). The length of this vector is approximately 30.0333 (based on the earlier formula), so dividing each component by this number gives a vector with coordinates of (0.7658,0.5993,0.233). The length of this vector is 0.99994956372809118, which is awfully close to the 1.0 you should have expected. The reason it's not exact is because I didn't use the full precision value when calculating the original length (to save on some math time). When dealing with values with decimal points (especially with computers), close is often as good as exact.

All the functionality you've learned about vectors so far already exists in the classes in the Managed DirectX runtime, but it's important to understand the how and why in some cases rather than just see the black box and not necessarily understand where these weird formulas come from. There are plenty of methods that I did not cover here (such as the dot product, which you can use to calculate the angle between two vectors, or the cross product, which you can use to calculate a vector perpendicular to two other vectors), but read up on them as well so you can understand the math behind these methods.

Construction Cue

It's also interesting to note that in this book, you deal only with 3D vectors. The built-in classes actually have a representative for 2D, 3D, and even 4D vectors. The basic functionality and logic is the same regardless of how many dimensions you are working on.




Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net