Tutorials » 3D Math » How to use Matrices

The Untold Engine represents a 3x3 Matrix as U4DEngine::U4DMatrix3n.

Declaration

There are two ways to declare a 3x3 matrix, you can declare a 3x3 matrix using the default constructor, which creates an identity matrix

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m;

You can also declare a 3x3 matrix providing its nine elements as shown below:

// 3x3 matrix - column major. X vector is 0, 1, 2, etc.
//  0   3   6
//  1   4   7
//  2   5   8

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m(1.0,0.0,0.0,
                         0.0,1.0,0.0,
                         0.0,0.0,1.0);

Vector Transformation

To transform a 3D vector with a given matrix, you should use the following method: U4DEngine::U4DMatrix3n::transform

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m(1.0,0.0,0.0,
                         0.0,1.0,0.0,
                         0.0,0.0,1.0);

//declare vector
U4DEngine::U4DVector3n v(1.0,1.0,1.0);

//transform the vector with the given matrix
U4DEngine::U4DVector3n n=m.transform(v);

Another way to transform a 3D vector is to use the following method:

U4DEngine::U4DMatrix3n::operator*()

The snippet below shows an example:

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m(1.0,0.0,0.0,
                         0.0,1.0,0.0,
                         0.0,0.0,1.0);

//declare vector
U4DEngine::U4DVector3n v(1.0,1.0,1.0);

//transform the vector with the given matrix
U4DEngine::U4DVector3n n=m*v;

Identity

A matrix can be set as an Identity Matrix using U4DEngine::U4DMatrix3n::setIdentity, as shown below

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m;

//Set the matrix as an identity matrix
m.setIdentity();

Inverse

The inverse of a 3x3 matrix is computed using U4DEngine::U4DMatrix3n::inverse.

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m(1.0,0.0,0.0,
                         0.0,1.0,0.0,
                         0.0,0.0,1.0);

//compute the inverse
U4DEngine::U4DMatrix3n n=m.inverse();

Transpose

To transpose a 3x3 matrix, you can use the method U4DEngine::U4DMatrix3n::transpose.

//declare a 3x3 matrix
U4DEngine::U4DMatrix3n m(1.0,0.0,0.0,
                         0.0,1.0,0.0,
                         0.0,0.0,1.0);

//compute the transpose
U4DEngine::U4DMatrix3n n=m.transpose();