If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
(Editor's Note - This article requires additional formatting work and is incomplete)
Version 1.21 30th November 2003 ------------------------------- Please mail feedback to firstname.lastname@example.org with a subject starting with MATRIX-FAQ
(otherwise my spam filter will simply kill your message). Any additional suggestions or related questions are welcome. Just send E-mail to the above address. The latest copy of this FAQ can be found at the following web page:
History ------- I (Andreas) tried to find "hexapod@(no-spam)netcom.com" who seemed to have maintained this for a while, but the site at netcom.com doesn't exist anymore, emails bounce. Since I (and colleques) wasted quite some time figuring out what was wrong with some of the algorithms given in the earlier versions of this document, I decided to correct it and put it back on the web. The formerly given sites for the location of these documents do not exist anymore: ftp://ftp.netcom.com/pub/he/hexapod/index.html http://www.glue.umd.edu/~rsrodger Versions, dates and links to local copies (so you can compare): matrfaq_1.02.html: Version 1.2 2nd September 1997 matrfaq_1.04.html: Version 1.4 26th December 1998 matrfaq_1.06.html: Version 1.6 30th September 2000 matrfaq_1.07.html: Version 1.7 20th December 2000 matrfaq_1.08.html: Version 1.8 21th December 2000 matrfaq_1.09.html: Version 1.9 16th January 2001 matrfaq_1.10.html: Version 1.10 30th January 2001
matrfaq_1.11.html: Version 1.11 9th February 2001 matrfaq_1.12.html: Version 1.12 26th March 2001 matrfaq_1.13.html: Version 1.13 20th July 2001 matrfaq_1.14.html: Version 1.14 17th August 2001 matrfaq_1.15.html: Version 1.15 20th August 2001 matrfaq_1.16.html: Version 1.16 2nd October 2001 matrfaq_1.17.html: Version 1.17 30th November 2001 matrfaq_1.18.html: Version 1.18 27th January 2002 matrfaq_1.19.html: Version 1.19 20th March 2002
matrfaq_1.20.html: Version 1.20 31st January 2002 matrfaq_1.21.html: Version 1.21 30th November 2003
Please refrain from asking me math questions. I am only maintaining this FAQ and have very little knowledge about the subject. But, if you have a question that is not answered by this FAQ and later happen to find the answer and believe it to be relevant for this FAQ (or its readers), please send all relevant information, hopefully in a pre-digested form, to me to be included here. Thanks! If you prefer to appear as "anonymous" in the contributions list, let me know, otherwise I'll just put you down with whatever name I can gather from your email header.
Q32. What are yaw, roll and pitch? Q33. How do I combine rotation matrices? Q34. What is Gimbal Lock? Q35. What is the correct way to combine rotation matrices? Q36. How do I generate a rotation matrix from Euler angles? Q37. How do I generate Euler angles from a rotation matrix? Q38. How do I generate a rotation matrix for a selected axis and angle? Q39. How do I generate a rotation matrix to map one vector onto another? Q40. How do I use matrices to convert between two coordinate systems?
Q41. What is a translation matrix? Q42. What is a scaling matrix? Q43. What is a shearing matrix? Q44. How do I perform linear interpolation between two matrices? Q45. How do I perform cubic interpolation between four matrices? Q46. How can I render a matrix?
QUATERNIONS =========== Q47. What are quaternions? Q48. How do quaternions relate to 3D animation?
Q49. How do I calculate the conjugate of a quaternion? Q50. How do I calculate the inverse of a quaternion? Q51. How do I calculate the magnitude of a quaternion? Q52. How do I normalise a quaternion? Q53. How do I multiply two quaternions together? Q54. How do I convert a quaternion to a rotation matrix? Q55. How do I convert a rotation matrix to a quaternion? Q56. How do I convert a rotation axis and angle to a quaternion? Q57. How do I convert a quaternion to a rotation axis and angle?
Q58. How do I convert spherical rotation angles to a quaternion? Q59. How do I convert a quaternion to spherical rotation angles? Q60. How do I convert Euler rotation angles to a quaternion? Q61. How do I use quaternions to perform linear interpolation between matrices? Q62. How do I use quaternions to perform cubic interpolation between matrices? Q63. How do I use quaternions to rotate a vector?
I1. Important note relating to OpenGl and this document ------------------------------------------------------- In this document (as in most math textbooks), all matrices are drawn in the standard mathematical manner. Unfortunately graphics libraries like IrisGL, OpenGL and SGI's Performer all represent them with the rows and columns swapped. Hence, in this document you will see (for example) a 4x4 Translation matrix represented as follows:
| 1 0 0 X | | | | 0 1 0 Y | M = | | | 0 0 1 Z | | | | 0 0 0 1 |
In Performer (for example) this would be populated as follows:
OpenGL uses a one-dimensional array to store matrices - but fortunately, the packing order results in the same layout of bytes in memory - so taking the address of a pfMatrix and casting it to a float* will allow you to pass it directly into routines like glLoadMatrixf. In the code snippets scattered throughout this document, a one-dimensional array is used to store a matrix. The ordering of the array elements is transposed with respect to OpenGL.
I2. Important note with respect to normalized inputs ---------------------------------------------------- Note that most algorithms assume normalized inputs, such as vectors of union length, or matrices with normalized main diagonal etc. It is possible, and often enough the case, that algorithms (and the code snippets provided here) work correctly with arbitrary inputs, but it is usually considered bad practise (and you will pay in debugging time if you fail to observe this suggestion) to rely on this property.
Q1. What is a matrix? ---------------------- A matrix is a two dimensional array of numeric data, where each row or column consists of one or more numeric values. Arithmetic operations which can be performed with matrices include addition, subtraction, multiplication and division. The size of a matrix is defined in terms of the number of rows and columns. A matrix with M rows and N columns is defined as a MxN matrix. Individual elements of the matrix are referenced using two index values. Using mathematical notation these are usually assigned the variables 'i' and 'j'. The order is row first, column second For example, if a matrix M with order 4x4 exists, then the elements of the matrix are indexed by the following row:column pairs:
The element at the top right of the matrix has i=0 and j=3 This is referenced as follows:
M = M i,j 0,3
In computer animation, the most commonly used matrices have either 2, 3 or 4 rows and columns. These are referred to as 2x2, 3x3 and 4x4 matrices respectively. 2x2 matrices are used to perform rotations, shears and other types of image processing. General purpose NxN matrices can be used to perform image processing functions such as convolution. 3x3 matrices are used to perform low-budget 3D animation. Operations such as rotation and multiplication can be performed using matrix operations, but perspective depth projection is performed using standard optimised into pure divide operations. 4x4 matrices are used to perform high-end 3D animation. Operations such as multiplication and perspective depth projection can be performed using matrix mathematics.
Q2. What is the "order" of a matrix? ------------------------------------- The "order" of a matrix is another name for the size of the matrix. A matrix with M rows and N columns is said to have order MxN.
Q3. How do I represent a matrix using the C/C++ programming languages? ----------------------------------------------------------------------- The simplest way of defining a matrix using the C/C++ programming languages is to make use of the "typedef" keyword. Both 3x3 and 4x4 matrices may be defined in this way ie:
Since each type of matrix has dimensions 3x3 and 4x4, this requires 9 and 16 data elements respectively. At first glance, the use of a single linear array of data values may seem counter-intuitive. The use of two dimensional arrays may seem more convenient ie.
However, the use of two reference systems for each matrix element very often leads to confusion. With mathemetics, the order is row first (i), column second (j) ie.
Using C/C++, this becomes
Using two dimensional arrays also incurs a CPU performance penalty in that C compilers will often make use of multiplication operations to resolve array index operations. So, it is more efficient to stick with linear arrays. However, one issue still remains to be resolved. How is an two dimensional matrix mapped onto a linear array? Since there are only two methods (row first/column second or column first/row column). The performance differences between the two are subtle. If all for-next loops are unravelled, then there is very little difference in the performance for operations such as matrix-matrix multiplication. Using the C/C++ programming languages the linear ordering of each matrix is as follows:
mat[nbsp][nbsp]= M[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]mat[nbsp][nbsp]= M [nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 00[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 03 [nbsp][nbsp]mat = M[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]mat = M [nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 30[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 33
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| A B C D | [nbsp][nbsp][nbsp][nbsp]M = | E F G H | [nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| I J K L | [nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| M N O P |
[nbsp][nbsp]Then the direction vector for each axis is as follows:
[nbsp][nbsp][nbsp][nbsp] X-axis = [ A E I ] [nbsp][nbsp][nbsp][nbsp] Y-axis = [ B F J ] [nbsp][nbsp][nbsp][nbsp] Z-axis = [ C G K ] [nbsp][nbsp][nbsp][nbsp] ARITHMETIC ==========
Q6.[nbsp][nbsp]What is the identity matrix? --------------------------------- [nbsp][nbsp]The identity matrix is matrix in which has an identical number of rows [nbsp][nbsp]and columns. Also, all the elements in which i=j are set one. All others [nbsp][nbsp]are set to zero. For example a 4x4 identity matrix is as follows:
Q7.[nbsp][nbsp]What is the major diagonal of a matrix? -------------------------------------------- [nbsp][nbsp]The major diagonal of a matrix is the set of elements where the [nbsp][nbsp]row number is equal to the column number ie.
[nbsp][nbsp][nbsp][nbsp]M[nbsp][nbsp] where i=j [nbsp][nbsp][nbsp][nbsp] ij
[nbsp][nbsp]In the case of the identity matrix, only the elements on the major [nbsp][nbsp]diagonal are set to 1, while all others are set to 0. [nbsp][nbsp]