Game Physics Cookbook
上QQ阅读APP看书,第一时间看更新

Identity matrix

Multiplying a scalar number by 1 will result in the original scalar number. There is a matrix analogue to this, the identity matrix. The identity matrix is commonly written as I. If a matrix is multiplied by the identity matrix, the result is the original matrix Identity matrix.

In the identity matrix, all non-diagonal elements are 0, while all diagonal elements are one Identity matrix. The identity matrix looks like this:

Identity matrix

Getting ready

Because the identity matrix has no effect on multiplication, by convention it is the default value for all matrices. We're going to add two constructors to every matrix struct. One of the constructors is going to take no arguments; this will create an identity matrix. The other constructor will take one float for every element of the matrix and assign every element inside the matrix. Both constructors are going to be inline.

How to do it…

Follow these steps to add both a default and overloaded constructors to matrices:

  1. Add the default inline constructor to the mat2 struct:
    inline mat2() { 
        _11 = _22 = 1.0f;
        _12 = _21 = 0.0f;
    }
  2. Add the default inline constructor to the mat3 struct:
    inline mat3() {
        _11 = _22 = _33 = 1.0f;
        _12 = _13 = _21 = 0.0f;
        _23 = _31 = _32 = 0.0f;
    }
  3. Add the default inline constructor to the mat4 struct:
    inline mat4() {
        _11 = _22 = _33 = _44 = 1.0f;
        _12 = _13 = _14 = _21 = 0.0f;
        _23 = _24 = _31 = _32 = 0.0f;
        _34 = _41 = _42 = _43 = 0.0f;
    }
  4. Add a constructor to the mat2 struct that takes four floating point numbers:
    inline mat2(float f11, float f12,
                float f21, float f22) {
        _11 = f11; _12 = f12;
        _21 = f21; _22 = f22;
    }
  5. Add a constructor to the mat3 struct that takes nine floating point numbers:
    inline mat3(float f11, float f12, float f13,
                float f21, float f22, float f23,
                float f31, float f32, float f33) {
        _11 = f11; _12 = f12; _13 = f13;
        _21 = f21; _22 = f22; _23 = f23;
        _31 = f31; _32 = f32; _33 = f33;
    }
  6. Add a constructor to the mat4 struct that takes 16 floating point numbers:
    inline mat4(float f11, float f12, float f13, float f14,
                float f21, float f22, float f23, float f24,
                float f31, float f32, float f33, float f34,
                float f41, float f42, float f43, float f44) {
        _11 = f11; _12 = f12; _13 = f13; _14 = f14;
        _21 = f21; _22 = f22; _23 = f23; _24 = f24;
        _31 = f31; _32 = f32; _33 = f33; _34 = f34;
        _41 = f41; _42 = f42; _43 = f43; _44 = f44;
    }

How it works…

Let's explore how the identity matrix works. Suppose we want to multiply the following matrices:

How it works…

Let's find the value of How it works… by taking the dot product of row 3 of the identity matrix (0,0,1) and column 2 of the other matrix(7,2,6):

How it works…

The result is the original value of 6! This happens because any given row or column of the identity matrix is going to have two 0 components and one 1 component. As seen in the preceding snippet, the dot product eliminated components with a value of 0.