Saturday, February 26, 2011

I shall introduce you to The Swiss Army Knife of our hero's journey… The Math Library!

What's up dudes? I am almost finished explaining the Four Core Systems, so here is the third one: The Math Library a.k.a. The Swiss Army Knife. This one has been by far the easiest one to code, just two (2) full afternoons of coding and testing, and this baby was ready to Rock n' Roll!

Even though this one has been the fastest to get completed, it doesn't make it less important than the rest. Actually, it is probably the one that is going to be use the most for the rest of the class, basically because the Graphics Engine (which is the next assignment after the core systems) relies on this library for all the dirty work.

Well, here goes the explanation. I think I mentioned this previously, the Math Library consists of a Vector class and Matrix class. The former is a four (4) float object (X, Y, Z and W) and the latter is a sixteen (16) float one (4x4 matrix). You might be asking why does the vector has four (4) components and not just three (3)? Well, as you shall see if you continue reading my blog over the next months, in the following class after this one (Game Engine Programming II) I will deal with a very famous problem called 'Gimbal Lock', and one solution to that issue is through the use of Quaternions (4-D vectors). I will expand on this topic further down the road (so continue following my story =D).

I will assume (as the professor did with us) that you readers have a basic understanding of vectors, matrices and all operations involving them (either independently or combined together). Therefore, I will present you with a list of all the functionalities that the library has:
  • Vector: dot and cross product, magnitude, normalization, angle between vectors and epsilon-based comparisons. 
  • Matrix: determinant, transpose, inverse, epsilon-based comparisons and type-based matrix definitions (identity and rotation/scaling/translation matrices).
Apart from this, the library has all the relevant Math Operators overloaded: addition(+, +=), subtraction(-, -=), multiplication (*, *=) by scalar and between types (vector*vector, matrix*matrix and vector*matrix). Both data types also have the Bracket Operators defined, e.g. you can access the vector's X component by doing "vector[X]" directly.

Finally, is important to clarify that the vector is defined as a union of four (4) floats or a _m128 object type. The same applies for the matrix, it is defined as a union of sixteen (16) floats o four (4) vectors. This is done in order to use SIMD (Single Instruction, Multiple Data) instructions from the processor, but this is a topic for another day and other class (Game Code Optimization).

Well my loyal readers, I shall abandon you for now, I will probably be back tomorrow to explain my File System implementation. So, for the time being have a nice Saturday!

Adios!

No comments:

Post a Comment