R – Learning game programming (part 2) (math)

3dmathvector

So, it's been a few months since I wrote this question, since then I've toyed with "raw" C++ D3D, The Ogre and Irrlicht graphics engines and lately Microsoft XNA. I've built a few 2D games (mostly replicas of old stuff like tetris, astreoids, etc.) and made some (very) small steps into the 3D world in the above mentioned technologies.

I have little to no trouble creating the actual game logic, abstracting away object interactions to allow me to plug in different forms of control (computer, player, over network. etc.), doing threading or any of the other stuff I'm used to from my day to day work – which feels perfectly natural to me. I messed around very little with HLSL and particle effects (very very basic).

But 3D math involving Matrices and Vectors (and Quaternions(?) in Ogre3D, are these really needed?)… really gets me, I can follow examples (e.g. the Learning XNA 3.0 book I bought from O'Reilly, which is an awesome book btw) and I understand why and how something happens in the example, but when I try to do something myself I feel that I'm lacking the understanding of this type of math to be able to really get it and make it work by myself.

So I'm looking for resources on learning 3D math (mostly) and some Shader/Particle Effects books. I would prefer resources that are pedagogic and take it slow above something like a doctors thesis on vector math which will be way over my head. The ideal resource would be something that demonstrates it all in D3D.

Best Answer

Ok, a quick course in Matrix/Vector calculation:

A matrix is a collection of numbers ordered in a rectangular grid like:

[ 0,  1,  2 ]
[ 2,  3,  5 ]
[ 2,  1,  3 ]
[ 0,  0,  1 ]

The above matrix has 4 rows and 3 columns and as such is a 4 x 3 matrix. A vector is a matrix with 1 row (a row vector) or 1 column (a column vector). Normal numbers are called scalars to contrast with matrices.

It is also common to use capital letters for matrices and lowercase letters for scalars.

We can do basic calculation with matrices but there are some conditions.

Addition

Matrices can be added if they have the same dimensions. So a 2x2 matrix can be added to a 2x2 matrix but not to a 3x5 matrix.

[ 1,  2 ] + [ 2,  5 ] = [ 3,  7 ]
[ 2,  4 ]   [ 0,  3 ]   [ 2,  7 ]

You see that by addition each number at each cell is added to the number on the same position in the other matrix.

Matrix multiplication

Matrices can be multiplied, but this is a bit more complex. In order to multiply matrix A with matrix B, you need to multiply the numbers in each row if matrix A with each column in matrix B. This means that if you multiply an a x b matrix with a c x d matrix, b and c must be equal and the resulting matrix is a x d:

[1,2,3] x [4,6] = [1x4+2x2+3x2, 1x6+2x1+3x3 ] = [4+4+6,  6+2+9  ] = [14, 20]
[1,4,5]   [2,1]   [1x4+4x2+5x2, 1x6+4x1+5x3 ]   [4+8+10, 6+4+15 ]   [22, 25]
          [2,3] 

As you can see, with matrixes, A x B differs from B x A.

Matrix scalar multiplication

You can multiply a matrix with a scalar. In that case, each cell is multiplied with that number:

3 x [1,2] = [ 3, 6]
    [4,7]   [12,21]

Inverting a matrix Matrix division is not possible, but you can create an inversion of a matrix such that A x A-inv is a matrix with all zero's except for that main diagonal:

[ 1, 0, 0 ]
[ 0, 1, 0 ]
[ 0, 0, 1 ]

Inverting a matrix can only be done with square matrices and it is a complex job that does not neccesary have a result.

Start with matrix A:

    [ 1, 2, 3 ]
A = [ 1, 3, 4 ]
    [ 2, 5, 1 ]

We add 3 extra columns and fill them with the unit matrix:

[ 1, 2, 3, 1, 0, 0 ]
[ 1, 3, 4, 0, 1, 0 ]
[ 2, 5, 1, 0, 0, 1 ]

Now we start with the first column. We need to subtract the first row from each other row such that the first column contains only zeroes except for the first row. In order to do that we subtract the first row once from the second and twice from the third:

[ 1, 2, 3, 1, 0, 0 ]
[ 0, 1, 1,-1, 1, 0 ]
[ 0, 1,-5,-2, 0, 1 ]

Now we repeat this with the second column (twice from the first row and once from the third)

[ 1, 0, 1, 3,-2, 0 ]
[ 0, 1, 1,-1, 1, 0 ]
[ 0, 0,-6,-1,-1, 1 ]

For the third column, we have a slight problem. The pivot number is -6 and not 1. But we can solve this by multiplying the entire row with -1/6:

[ 1, 0, 1,   3,  -2,    0 ]
[ 0, 1, 1,  -1,   1,    0 ]
[ 0, 0, 1, 1/6, 1/6, -1/6 ]

And now we can subtract the third row from the first and the second:

[ 1, 0, 0, 17/6,-13/6,  1/6 ]
[ 0, 1, 0, -7/6,  5/6,  1/6 ]
[ 0, 0, 1,  1/6,  1/6, -1/6 ]

Ok now we have the inverse of A:

[ 17/6,-13/6,  1/6 ]
[ -7/6,  5/6,  1/6 ]
[  1/6,  1/6, -1/6 ]

We can write this as:

      [ 17,-13,  1 ]
1/6 * [ -7,  5,  1 ]
      [  1,  1, -1 ]



    [ 1, 2, 3 ]   [ 17,-13,  1 ]                [ 6, 0, 0 ]    [ 1, 0, 0 ]
A = [ 1, 3, 4 ] x [ -7,  5,  1 ] x 1/6  = 1/6 x [ 0, 6, 0 ] =  [ 0, 1, 0 ]
    [ 2, 5, 1 ]   [  1,  1, -1 ]                [ 0, 0, 6 ]    [ 0, 0, 1 ]

Hope this helps a bit.

Related Topic