ware repair - fargo computer repair

The mathematics involved in 3D game programming - Vector Algebra Part 2

The mathematics involved in 3D game programming - Vector Algebra Part 2

Written by Diane Diane Ware - August 23, 2018 in Programming

This is the third blog in a series on the mathematics involved in 3D game programming. 

Note: in the previous blog, I mentioned Paul's Online Notes, a website by Professor Paul Dawkins of Lamar University as a source for certain math images and math information, and I will again often use parts of his tutorial notes in this blog. 

Vector Algebra - A Review

Again, as in the previous blog, I would like to do a review of vector algebra, and do a brief review of the previous blog as well. Recall that: 

  • •Vectors have both magnitude and direction.
  • •Vectors are usually represented as a line segment with a starting point, the tail end, and an ending point, an arrowhead.
  • •Vectors play an important role in computer graphics, collision detection, and physical simulation in 3D programming.
  • •Vectors are used to represent positions, displacements, directions, velocities, and forces in 3D game programming.
  • •Vectors can be manipulated mathematically: 

            1. Vector addition. 

            2. Vector subtraction. 

            3. Vector dot products, which produce a scalar quantity, can be used to:

                  a. Determine whether the angle between two vectors is acute (less than 90 degrees) or obtuse (greater than 90 degrees).

                  b. Determine if the angle between two vectors is 90 degrees (the two vectors are perpendicular to each other, or, in other words, orthogonal to each other). That is, the dot product of vector a and vector b should give this result: 

                  

                  c. Also, something I didn't really discuss in the last blog - the vector dot product can be used to find out whether or not two vectors are parallel to each other. AND, if two vectors are parallel, then the angle between them is either 0 degrees (both vectors are pointing in the same direction) or 180 degrees (both vectors are pointing in the opposite direction). This means one of the following would need to be true: 

                     

                         Two examples of whether two vectors are parallel, orthogonal (perpendicular), or neither, from Paul's website: 

                         

                         

Vector Projections

Okay, now some details on vector projections, which I mentioned I would discuss in the last blog. 

From Paul's site vector on dot products and vector projections, here is an illustration of two vector projections: 

 

As can be seen in the images above, a projection basically involves dropping a line (the dotted line) straight down from the end of the vector b, so that it hits vector a at a right angle. This then creates the projection of vector b onto vector a. Notice that both vector a and vector b arise from the same initial point; this is important. Here is the formula for the above vector projection: 

                                                                                   

 

Here is an example of the math involved in doing a vector projection: 

                                                                                   

Now, you may be wondering, as I did initially, why are vector projections needed for 3D programming? Here are some reasons (but keep in mind there could be quite a few more, though these are the ones I have discovered so far): 

1. One of the main reasons is to simplify the process of finding the closest point on a line or a plane from a given point. Visit this link, JCCC Math/Phys 191, to see some examples and to learn more. 

2. Suppose you are dealing with one vector, but you need to decompose it into two or three separate vectors, in the x and y direction, or in the x, y, and z directions - vector projections can help with this. At first, you would need to find the projection of the vector on to the x-axis, which would then give two vectors. And then the third vector could be determined by using the vector cross product (discussed below) on the other two vectors. Here are some further details about this, from Stack Exchange

3. This 3rd reason is actually quite interesting. Apparently, during the rendering process for 3D games, the renderer has access to every vertex, and these vertices need to be mapped onto a 2D rectangle - that is, the shape of most viewing screens. More can be read about this on a Stackoverflow question and answer session

4. Surprisingly, to cast shadows in 3D games! See this link for more

5. In physics, forces applied to objects, to accomplish work, often have more than one force vector, though both have the same initial point of force, hence why it is important to keep in mind vector projections are accomplished by using vectors that both have the same starting point. And thus, to keep a 3D gaming world linked to reality, it is important that 3D designers apply such concepts. 

 

Vector Cross Product

Something quite important to understand at the onset of a discussion on vector cross products is this: a vector dot product produces a scalar, or rather a number answer, whereas a vector cross product produces another vector. 

As was mentioned above, in reason #2 for the use of vector projections in 3D gaming, having two vectors, and needing a third, which will normally be perpendicular (orthogonal) to the plane the first two vectors reside within, is the main result of calculating a vector cross product on the two initial vectors. 

There are several formulas for determing a vector cross product, as shown on Paul's page for vector cross products:

                                                                               

                                                                               

                                                                               

The first formula is easiest to use, although in general it takes some patience and a careful eye to enter the correct vector points in these equations. 

Here are two examples from Paul's site: 

The reason Paul interchanged the a vector and the b vector in each of the examples above is to show that the two cross products will point in exactly the opposite directions, since they each differ only by a sign. That is, 5i is positive in the first, and 5i is negative in the second. 

These details below, from Paul's vectro cross product page will help to give further reasoning for the use and the results of cross products: 

So, as with vector dot products, why are vector cross products needed in 3D game programming? 

Here are some reasons: 

1. The vector cross product can help determine whether a game engine's coordinate system is left-handed or right-handed. This is discussed in more detail in this Wikipedia article section

2. A game object's forward, up, or right vector direction can be found using the vector cross product. More can be found on this Game Programming and Development blog site

3. The axis of rotation for certain objects in a 3D game program can be found with the vector cross product. 

And I am sure there are more uses for the vector cross product, but these are what I have discovered so far. 

In the next blog article on the mathematics involved in 3D game programming, I will attempt to find more uses for the vector cross product, and I will also venture further into the overall mathematics involved in 3D game programming. 

Diane

Diane Ware

Manager - Tech Support

I am co-owner of Ware Repair and enjoy working with technology, web design and development, and embedded programming, especially that which applies to robotics. I also enjoy writing blogs and sci-fi novels on my free time.