logo back up home forward   further reading more topics »

Maths - Transforms using matrices

Prerequisites

You may want to review vectors:

Multiply Matrix by Vector

A matrix can convert a vector into another vector by multiplying it by a matrix as follows:

v_out0
v_out1
v_out2
=
m00 m01 m02
m10 m11 m12
m20 m21 m22
v_in0
v_in1
v_in2

We can think of this as transforming (v_in0,v_in1,v_in2) into (v_out0,v_out1,v_out2).

If we apply this to every point in the 3D space we can think of the matrix as transforming the whole vector field.

This can represent any linear transform, including scaling and rotation (translation can be done by adding vectors).

Applying the Transform

The result of this multiplication can be calculated by treating the vector as a n x 1 matrix, so in this case we multiply a 3x3 matrix by a 3x1 matrix we get:

v_out0
v_out1
v_out2
=
m00*v_in0 + m01*v_in1 + m02*v_in2
m10*v_in0 + m11*v_in1 + m12*v_in2
m20*v_in0 + m21*v_in1 + m22*v_in2

So any vector v_in can be converted into another vector v_out.

We can show all vectors as a vector field as shown here.

Scaling

We can scale using the following matrix:

m00 0 0
0 m11 0
0 0 m22

If we want to scale equally in all dimensions then m00 =m11=m22

Translation

We can translate a vector by adding an offset vector, not a linear transform as shown above. Sometimes we want to combine translation with rotation so that we can do both in one operation, but we cant do 3D translation by multiplication with a 3x3 matrix . but we can with a 4x4 matrix as defined here.

Rotation

We can use a 3x3 matrix to represent rotation in 3 dimensions as defined here. Rotations in any arbitrary number of dimensions are discussed in group theory.

Sample 3D Rotations

In order to try to explain things I thought it might help to work out a simple case where rotations are only allowed in multiples of 90 degrees. This should make it easier to illustrate the orientation with a simple aeroplane figure, we can rotate this either about the x,y or z axis as shown here:

heading applied first giving 4 possible orientations:

reference orientation

heading = 0
attitude = 0
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=1
ca=1
cb=1
sh=0
sa=0
sb=0

 

1 0 0
0 1 0
0 0 1

rotate by 90 degrees about y axis

heading = 90 degrees
attitude = 0
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=1
sh=1
sa=0
sb=0

 

0 0 1
0 1 0
-1 0 0

rotate by 180 degrees about y axis

heading = 180 degrees
attitude = 0
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=-1
ca=1
cb=1
sh=0
sa=0
sb=0

 

-1 0 0
0 1 0
0 0 -1

rotate by 270 degrees about y axis

heading = -90 degrees
attitude = 0
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=1
sh=-1
sa=0
sb=0

 

0 0 -1
0 1 0
1 0 0

Then apply attitude +90 degrees for each of the above: (note: that if we went on to apply bank to these it would just rotate between these values, the straight up and streight down orientations are known as singularities because they can be fully defined without using the bank value)

heading = 0
attitude = 90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=1
ca=0
cb=1
sh=0
sa=1
sb=0

 

0 -1 0
1 0 0
0 0 1

heading = 90 degrees
attitude = 90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=0
cb=1
sh=1
sa=1
sb=0

 

0 0 1
1 0 0
0 1 0

heading = 180 degrees
attitude = 90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=-1
ca=0
cb=1
sh=0
sa=1
sb=0

0 1 0
1 0 0
0 0 -1

heading = -90 degrees
attitude = 90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=0
cb=1
sh=-1
sa=1
sb=0

 

0 0 -1
1 0 0
0 -1 0

Or instead apply attitude -90 degrees (also a singularity):

heading = 0
attitude = -90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=1
ca=0
cb=1
sh=0
sa=-1
sb=0

 

0 1 0
-1 0 0
0 0 1

heading = 90 degrees
attitude = -90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=0
cb=1
sh=1
sa=-1
sb=0

 

0 0 1
-1 0 0
0 -1 0

heading = 180 degrees
attitude = -90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=-1
ca=0
cb=1
sh=0
sa=-1
sb=0

 

0 -1 0
-1 0 0
0 0 -1

heading = -90 degrees
attitude = -90 degrees
bank = 0

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=0
cb=1
sh=-1
sa=-1
sb=0

 

0 0 -1
-1 0 0
0 1 0

Normally we dont go beond attitude + or - 90 degrees because thes are singularities, instead apply bank +90 degrees:


heading = 0
attitude = 0
bank = 90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=1
ca=1
cb=0
sh=0
sa=0
sb=1

 

1 0 0
0 0 -1
0 1 0

heading = 90 degrees
attitude = 0
bank = 90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=0
sh=1
sa=0
sb=1

 

0 1 0
0 0 -1
-1 0 0

heading = 180 degrees
attitude = 0
bank = 90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=-1
ca=1
cb=0
sh=0
sa=0
sb=1

-1 0 0
0 0 -1
0 -1 0

 

heading = -90 degrees
attitude = 0
bank = 90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=0
sh=-1
sa=0
sb=1

 

0 -1 0
0 0 -1
1 0 0

Apply bank +180 degrees:


heading = 0
attitude = 0
bank = 180 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=1
ca=1
cb=-1
sh=0
sa=0
sb=0

 

1 0 0
0 -1 0
0 0 -1

heading = 90 degrees
attitude = 0
bank = 180 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=-1
sh=1
sa=0
sb=0

 

0 0 -1
0 -1 0
-1 0 0

heading = 180 degrees
attitude = 0
bank = 180 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=-1
ca=1
cb=-1
sh=0
sa=0
sb=0

 

-1 0 0
0 -1 0
0 0 1

heading = -90 degrees
attitude = 0
bank = 180 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=-1
sh=-1
sa=0
sb=0

 

0 0 1
0 -1 0
1 0 0

Apply bank -90 degrees:


heading = 0
attitude = 0
bank = -90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=1
ca=1
cb=0
sh=0
sa=0
sb=-1

 

1 0 0
0 0 1
0 -1 0

heading = 90 degrees
attitude = 0
bank = -90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=0
sh=1
sa=0
sb=-1

 

0 -1 0
0 0 1
-1 0 0

heading = 180 degrees
attitude = 0
bank = -90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=-1
ca=1
cb=0
sh=0
sa=0
sb=-1

-1 0 0
0 0 1
0 1 0

 

heading = -90 degrees
attitude = 0
bank = -90 degrees

ch*ca -ch*sa*cb + sh*sb ch*sa*sb + sh*cb
sa ca*cb -ca*sb
-sh*ca sh*sa*cb + ch*sb - sh*sa*sb + ch*cb

ch=0
ca=1
cb=0
sh=-1
sa=0
sb=-1

 

0 1 0
0 0 1
1 0 0

 

 

     

reference orientation

1 0 0
0 1 0
0 0 1

 

rotate by 90 degrees about x axis

1 0 0
0 0 -1
0 1 0

 

rotate by 180 degrees about x axis

1 0 0
0 -1 0
0 0 -1

 

rotate by 270 degrees about x axis

1 0 0
0 0 1
0 -1 0

 

   

rotate by 90 degrees about z axis

0 1 0
-1 0 0
0 0 1

 

rotate by 90 degrees about y axis

0 0 -1
0 1 0
1 0 0

 

     
 

rotate by 180 degrees about z axis

-1 0 0
0 -1 0
0 0 1

 

 

rotate by 180 degrees about y axis

-1 0 0
0 1 0
0 0 -1

 

     

rotate by 270 degrees about z axis

0 -1 0
1 0 0
0 0 1

 

   

rotate by 270 degrees about y axis

0 0 1
0 1 0
-1 0 0

 

     

When we combine these rotations about the x,y and z axies in 90 degree multiples there are 24 possible orientations as shown here:

1 0 0
0 1 0
0 0 1

 

1 0 0
0 0 -1
0 1 0

 

1 0 0
0 -1 0
0 0 -1

 

1 0 0
0 0 1
0 -1 0

 

0 -1 0
1 0 0
0 0 1

 

0 0 1
1 0 0
0 1 0

 

0 1 0
1 0 0
0 0 -1

0 0 -1
1 0 0
0 -1 0

 

-1 0 0
0 -1 0
0 0 1

 

-1 0 0
0 0 -1
0 -1 0

 

-1 0 0
0 1 0
0 0 -1

 

-1 0 0
0 0 1
0 1 0

 

0 1 0
-1 0 0
0 0 1

 

0 0 1
-1 0 0
0 -1 0

 

0 -1 0
-1 0 0
0 0 -1

 

0 0 -1
-1 0 0
0 1 0

 

0 0 -1
0 1 0
1 0 0

 

0 1 0
0 0 1
1 0 0

 

0 0 1
0 -1 0
1 0 0

 

0 -1 0
0 0 -1
1 0 0

 

0 0 -1
0 -1 0
-1 0 0

 

0 -1 0
0 0 1
-1 0 0

 

0 0 1
0 1 0
-1 0 0

 

 

0 1 0
0 0 -1
-1 0 0

 

encoding of these rotations in quaternions is shown here.
encoding of these rotations in axis-angle is shown here.
encoding of these rotations in euler angles is shown here.


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Mathematics for 3D game Programming - Includes introduction to Vectors, Matrices, Transforms and Trigonometry. (But no euler angles or quaternions). Also includes ray tracing and some linear & rotational physics also collision detection (but not collision response).

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to the software project, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

Matlab.

Can you help?

Please send me any improvements to here. I would appreciate ideas to make the pages more useful including error correction, ideas for new pages, improvements to wording. It helps if you quote the full URL of the page.

 

Terminology and Notation

Specific to this page here:

 

program

I am working on a project which uses these principles, if you would like to help me with this you are welcome to join in, here:

http://sourceforge.net/projects/mjbworld/

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2008 Martin John Baker - All rights reserved.