The Matrix and Quaternions FAQ

Published February 11, 2002 by Andreas, posted by Myopic Rhino
Do you see issues with this article? Let us know.
Advertisement
[font="Courier New"](Editor's Note - This article requires additional formatting work and is incomplete)

Version 1.21[nbsp][nbsp]30th November 2003
-------------------------------
Please mail feedback to [email="matrix_faq@j3d.org"]matrix_faq@j3d.org[/email]
with a subject starting with[nbsp][nbsp]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:

http://www.j3d.org/matrix_faq/matrfaq_latest.html
Feel free to distribute or copy this FAQ as you please.

Contributions
-------------

[nbsp][nbsp][alink='I1']Introduction I1[/alink]: [email="steve@%28no-spam%29mred.bgm.link.com"]Steve ?????[/email]
[nbsp][nbsp]Correction to [alink='Q55']Q55[/alink] until [alink='Q59']Q59[/alink]:
[nbsp][nbsp][email="andreas.junghanns@%28no-spam%29dcx.com"]Andreas Junghanns[/email]

[nbsp][nbsp]Correction to [alink='Q50']Q50[/alink]: [email="morten@%28no-spam%29innerloop.no"]Morten Ofstad[/email]
[nbsp][nbsp]Note to [alink='Q39']Q39[/alink]: [email="t.nuydens@%28no-spam%29vrcontext.com"]Tom Nuydens[/email]
[nbsp][nbsp]Corrections to [alink='Q29']Q29[/alink] and [alink='Q37']Q37[/alink]: [/font][email="eric@%28no-spam%29ee.pitt.edu"][font="Courier New"]Eric Reiss[/font] [/email][font="Courier New"]
[nbsp][nbsp]Clarification to [alink='Q56']Q56[/alink]: [email="murdoch@%28no-spam%29fisher.stats.uwo.ca"]Duncan Murdoch[/email]
[nbsp][nbsp]Clarification to [alink='Q37']Q37[/alink]: [email="avitzur@%28no-spam%29PacificT.com"]Ron Avitzur[/email]
[nbsp][nbsp]Correction to [alink='Q1']Q1[/alink]: [email="mona@%28no-spam%29ncmir.ucsd.edu"]Mona Wong[/email]

[nbsp][nbsp]Corrections to [alink='Q36']Q36[/alink] and [alink='Q37']Q37[/alink]: [email="eric@%28no-spam%29ee.pitt.edu"]Eric Reiss[/email]
[nbsp][nbsp]Improvement to [alink='Q34']Q34[/alink] and [alink='Q38']Q38[/alink]: [email="hplus-mail@%28no-spam%29mindcontrol.org"]Jon Watte[/email]

[nbsp][nbsp]Warning and alternative to [alink='Q58']Q58[/alink] and [alink='Q59']Q59[/alink]: [email="PPedriana@%28no-spam%29maxis.com"]Paul Pedriana[/email]
[nbsp][nbsp]Correction (and optimization [Lee]) to [alink='Q53']Q53[/alink]: [email="ellieg@%28no-spam%29cableinet.co.uk"]Eleanor Groundwater[/email] and [email="LeeMorgan@%28no-spam%29lee-morgan.net"]Lee Morgan[/email]

[nbsp][nbsp]Improvement to [alink='Q39']Q39[/alink]: [email="jhunpingco@%28no-spam%29yahoo.com"]jhunpingco[/email]
[nbsp][nbsp]Corrections to [alink='Q11']Q11[/alink] and optimization to [alink='Q12']Q12[/alink]: [email="GDW33@%28no-spam%29student.canterbury.ac.nz"]Gordon[/email]
[nbsp][nbsp]Corrections to [alink='Q54']Q54[/alink] to [alink='Q60']Q60[/alink]: [email="eleanorg@%28no-spam%29owl.co.uk"]Eleanor Groundwater[/email]

[nbsp][nbsp]Corrections and improvements to [alink='Q23']Q23[/alink] and [alink='Q24']Q24[/alink]: [email="ben@%28no-spam%29exocortex.org"]Ben Houston[/email]
[nbsp][nbsp]Addition to [alink='Q39']Q39[/alink]: [email="hplus@%28no-spam%29mindcontrol.org"]Jon Watte[/email]
[nbsp][nbsp]Correction to [alink='Q61']Q61[/alink]: [email="adam@%28no-spam%29gimp.org"]Adam D. Moss[/email]

[nbsp][nbsp]Addition of [alink='Q63']Q63[/alink]: [email="cline@%28no-spam%29cs.ubc.ca"]Mike Cline[/email]
[nbsp][nbsp]Addition of [alink='I2']I2[/alink]: [email="jacob@%28no-spam%29marner.dk"]Jacob Marner[/email]
[nbsp][nbsp]Correction to [alink='Q38']Q38[/alink] and inception of [alink='I2']I2[/alink]: [email="armuller@%28no-spam%29ira.uka.de"]Armin M?ller[/email]

[nbsp][nbsp]Addition of [alink='Q60']Q60[/alink]: [email="pfiguero@%28no-spam%29cs.ualberta.ca"]Pablo Figueroa[/email]
[nbsp][nbsp]Correntions and additions to [alink='Q14']Q14[/alink], [alink='Q16']Q16[/alink], [alink='Q21']Q21[/alink] and [alink='Q34']Q34[/alink]: [email="tronster321@%28no-spam%29hotmail.com"]Tronster Hartley[/email]

[nbsp][nbsp]Correction to [alink='Q12']Q12[/alink] and [alink='Q54']Q54[/alink]: [email="frankdj@%28no-spam%29mailhost.ph.tn.tudelft.nl"]Frank DJ[/email]
[nbsp][nbsp]Correction to [alink='Q34']Q34[/alink]: [email="robert.funnell@i%28no-spam%29mcgill.ca"]Robert Funnell[/email]


History
-------
I (Andreas) tried to find "[email="hexapod@%28no-spam%29netcom.com"]hexapod@(no-spam)netcom.com[/email]" who seemed to have maintained
this for a while, but the site at netcom.com doesn't exist anymore,
emails bounce.[nbsp][nbsp]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:
[nbsp][nbsp]ftp://ftp.netcom.com/pub/he/hexapod/index.html
[nbsp][nbsp]http://www.glue.umd.edu/~rsrodger
[nbsp][nbsp]Versions, dates and links to local copies (so you can compare):
matrfaq_1.02.html: Version 1.2[nbsp][nbsp]2nd September 1997
matrfaq_1.04.html: Version 1.4[nbsp][nbsp]26th December 1998
matrfaq_1.06.html: Version 1.6[nbsp][nbsp]30th September 2000
matrfaq_1.07.html: Version 1.7[nbsp][nbsp]20th December 2000
matrfaq_1.08.html: Version 1.8[nbsp][nbsp]21th December 2000
matrfaq_1.09.html: Version 1.9[nbsp][nbsp]16th January 2001
matrfaq_1.10.html: Version 1.10[nbsp][nbsp]30th January 2001

matrfaq_1.11.html: Version 1.11[nbsp][nbsp]9th February 2001
matrfaq_1.12.html: Version 1.12[nbsp][nbsp]26th March 2001
matrfaq_1.13.html: Version 1.13[nbsp][nbsp]20th July 2001
matrfaq_1.14.html: Version 1.14[nbsp][nbsp]17th August 2001
matrfaq_1.15.html: Version 1.15[nbsp][nbsp]20th August 2001
matrfaq_1.16.html: Version 1.16[nbsp][nbsp]2nd October 2001
matrfaq_1.17.html: Version 1.17[nbsp][nbsp]30th November 2001
matrfaq_1.18.html: Version 1.18[nbsp][nbsp]27th January 2002
matrfaq_1.19.html: Version 1.19[nbsp][nbsp]20th March 2002

matrfaq_1.20.html: Version 1.20[nbsp][nbsp]31st January 2002
matrfaq_1.21.html: Version 1.21[nbsp][nbsp]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.

Introduction
------------
[alink='I1']I1.[nbsp][nbsp]Important note relating to OpenGL and this document[/alink]
[alink='I2']I2.[nbsp][nbsp]Important note with respect to normalized inputs[/alink]

Questions
---------

BASICS
======
[nbsp][alink='Q1']Q1. What is a matrix?[/alink]
[nbsp][alink='Q2']Q2. What is the order of a matrix?[/alink]

[nbsp][alink='Q3']Q3. How do I represent a matrix using the C/C++ programming languages?[/alink]
[nbsp][alink='Q4']Q4. What are the advantages of using matrices?[/alink]
[nbsp][alink='Q5']Q5. How do matrices relate to coordinate systems?[/alink]

ARITHMETIC
==========
[nbsp][alink='Q6']Q6. What is the identity matrix?[/alink]
[nbsp][alink='Q7']Q7. What is the major diagonal matrix of a matrix?[/alink]

[nbsp][alink='Q8']Q8. What is the transpose of a matrix?[/alink]
[nbsp][alink='Q9']Q9. How do I add two matrices together?[/alink]
[alink='Q10']Q10. How do I subtract two matrices?[/alink][/font][font="Courier New"]
[alink='Q11']Q11. How do I multiply two matrices together?[/alink]
[alink='Q12']Q12. How do I square or raise a matrix to a power?[/alink]
[alink='Q13']Q13. How do I multiply one or more vectors by a matrix?[/alink]

DETERMINANTS AND INVERSES
=========================
[alink='Q14']Q14. What is the determinant of a matrix?[/alink]

[alink='Q15']Q15. How do I calculate the determinant of a matrix?[/alink]
[alink='Q16']Q16. What are Isotropic and Anisotropic matrices?[/alink]
[alink='Q17']Q17. What is the inverse of a matrix?[/alink]
[alink='Q18']Q18. How do I calculate the inverse of an arbitrary matrix?[/alink]
[alink='Q19']Q19. How do I calculate the inverse of an identity matrix?[/alink]
[alink='Q20']Q20. How do I calculate the inverse of a rotation matrix?[/alink]
[alink='Q21']Q21. How do I calculate the inverse of a matrix using Kramer's rule?[/alink]
[alink='Q22']Q22. How do I calculate the inverse of a 2x2 matrix?[/alink]
[alink='Q23']Q23. How do I calculate the inverse of a 3x3 matrix?[/alink]

[alink='Q24']Q24. How do I calculate the inverse of a 4x4 matrix?[/alink]
[alink='Q25']Q25. How do I calculate the inverse of a matrix using linear equations?[/alink]

TRANSFORMS
==========
[alink='Q26']Q26. What is a rotation matrix?[/alink]
[alink='Q27']Q27. How do rotation matrices relate to coordinate systems?[/alink]
[alink='Q28']Q28. How do I generate a rotation matrix in the X-axis?[/alink]
[alink='Q29']Q29. How do I generate a rotation matrix in the Y-axis?[/alink]
[alink='Q30']Q30. How do I generate a rotation matrix in the Z-axis?[/alink]
[alink='Q31']Q31. What are Euler angles?[/alink]

[alink='Q32']Q32. What are yaw, roll and pitch?[/alink]
[alink='Q33']Q33. How do I combine rotation matrices?[/alink]
[alink='Q34']Q34. What is Gimbal Lock?[/alink]
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.[nbsp][nbsp]What are quaternions?
Q48.[nbsp][nbsp]How do quaternions relate to 3D animation?

Q49.[nbsp][nbsp]How do I calculate the conjugate of a quaternion?
Q50.[nbsp][nbsp]How do I calculate the inverse of a quaternion?
Q51.[nbsp][nbsp]How do I calculate the magnitude of a quaternion?
Q52.[nbsp][nbsp]How do I normalise a quaternion?
Q53.[nbsp][nbsp]How do I multiply two quaternions together?
Q54.[nbsp][nbsp]How do I convert a quaternion to a rotation matrix?
Q55.[nbsp][nbsp]How do I convert a rotation matrix to a quaternion?
Q56.[nbsp][nbsp]How do I convert a rotation axis and angle to a quaternion?
Q57.[nbsp][nbsp]How do I convert a quaternion to a rotation axis and angle?

Q58.[nbsp][nbsp]How do I convert spherical rotation angles to a quaternion?
Q59.[nbsp][nbsp]How do I convert a quaternion to spherical rotation angles?
Q60.[nbsp][nbsp]How do I convert Euler rotation angles to a quaternion?
Q61.[nbsp][nbsp]How do I use quaternions to perform linear interpolation between matrices?
Q62.[nbsp][nbsp]How do I use quaternions to perform cubic interpolation between matrices?
Q63.[nbsp][nbsp]How do I use quaternions to rotate a vector?
[/font]
[font="Courier New"]Introduction
------------

[aname='I1']I1. Important note relating to OpenGl and this document
-------------------------------------------------------
[nbsp][nbsp]In this document (as in most math textbooks), all matrices are drawn
[nbsp][nbsp]in the standard mathematical manner.[nbsp][nbsp]Unfortunately graphics libraries
[nbsp][nbsp]like IrisGL, OpenGL and SGI's Performer all represent them with the
[nbsp][nbsp]rows and columns swapped.
[nbsp][nbsp]Hence, in this document you will see (for example) a 4x4 Translation
[nbsp][nbsp]matrix represented as follows:

[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 1[nbsp][nbsp]0[nbsp][nbsp]0[nbsp][nbsp]X |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0[nbsp][nbsp]1[nbsp][nbsp]0[nbsp][nbsp]Y |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]M = |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0[nbsp][nbsp]0[nbsp][nbsp]1[nbsp][nbsp]Z |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0[nbsp][nbsp]0[nbsp][nbsp]0[nbsp][nbsp]1 |

[nbsp][nbsp]In Performer (for example) this would be populated as follows:

[nbsp][nbsp][nbsp][nbsp]M[0][1] = M[0][2] = M[0][3] =
[nbsp][nbsp][nbsp][nbsp]M[1][0] = M[1][2] = M[1][3] =
[nbsp][nbsp][nbsp][nbsp]M[2][0] = M[2][1] = M[2][3] = 0 ;
[nbsp][nbsp][nbsp][nbsp]M[0][0] = M[1][1] = M[2][2] = m[3][3] = 1 ;
[nbsp][nbsp][nbsp][nbsp]M[3][0] = X ;
[nbsp][nbsp][nbsp][nbsp]M[3][1] = Y ;
[nbsp][nbsp][nbsp][nbsp]M[3][2] = Z ;

[nbsp][nbsp]ie, the matrix is stored like this:

[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| M[0][0][nbsp][nbsp]M[1][0][nbsp][nbsp]M[2][0][nbsp][nbsp]M[3][0] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| M[0][1][nbsp][nbsp]M[1][1][nbsp][nbsp]M[2][1][nbsp][nbsp]M[3][1] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]M = |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| M[0][2][nbsp][nbsp]M[1][2][nbsp][nbsp]M[2][2][nbsp][nbsp]M[3][2] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| M[0][3][nbsp][nbsp]M[1][3][nbsp][nbsp]M[2][3][nbsp][nbsp]M[3][3] |

[nbsp][nbsp]OpenGL uses a one-dimensional array to store matrices - but fortunately,
[nbsp][nbsp]the packing order results in the same layout of bytes in memory - so
[nbsp][nbsp]taking the address of a pfMatrix and casting it to a float* will allow
[nbsp][nbsp]you to pass it directly into routines like glLoadMatrixf.
[nbsp][nbsp]In the code snippets scattered throughout this document, a one-dimensional
[nbsp][nbsp]array is used to store a matrix. The ordering of the array elements is
[nbsp][nbsp]transposed with respect to OpenGL.
[nbsp][nbsp]
[nbsp][nbsp]This Document[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]OpenGL
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0[nbsp][nbsp]1[nbsp][nbsp]2[nbsp][nbsp]3[nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0[nbsp][nbsp]4[nbsp][nbsp]8[nbsp][nbsp]12 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 4[nbsp][nbsp]5[nbsp][nbsp]6[nbsp][nbsp]7[nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 1[nbsp][nbsp]5[nbsp][nbsp]9[nbsp][nbsp]13 |
[nbsp][nbsp][nbsp][nbsp]M = |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]M = |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 8[nbsp][nbsp]9[nbsp][nbsp]10 11 |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 2[nbsp][nbsp]6[nbsp][nbsp]10 14 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 12 13 14 15 |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 3[nbsp][nbsp]7[nbsp][nbsp]11 15 |

[aname='I2']I2. Important note with respect to normalized inputs
----------------------------------------------------
[nbsp][nbsp]Note that most algorithms assume normalized inputs, such as vectors of
[nbsp][nbsp]union length, or matrices with normalized main diagonal etc. It is possible,
[nbsp][nbsp]and often enough the case, that algorithms (and the code snippets provided here)
[nbsp][nbsp]work correctly with arbitrary inputs, but it is usually considered bad practise
[nbsp][nbsp](and you will pay in debugging time if you fail to observe this suggestion) to
[nbsp][nbsp]rely on this property.
[nbsp][nbsp]

Answers
-------

BASICS
======

[aname='Q1']Q1.[nbsp][nbsp]What is a matrix?
----------------------
[nbsp][nbsp]A matrix is a two dimensional array of numeric data, where each
[nbsp][nbsp]row or column consists of one or more numeric values.
[nbsp][nbsp]Arithmetic operations which can be performed with matrices include
[nbsp][nbsp]addition, subtraction, multiplication and division.
[nbsp][nbsp]The size of a matrix is defined in terms of the number of rows
[nbsp][nbsp]and columns.
[nbsp][nbsp]A matrix with M rows and N columns is defined as a MxN matrix.
[nbsp][nbsp]Individual elements of the matrix are referenced using two index
[nbsp][nbsp]values. Using mathematical notation these are usually assigned the
[nbsp][nbsp]variables 'i' and 'j'. The order is row first, column second
[nbsp][nbsp]For example, if a matrix M with order 4x4 exists, then the elements
[nbsp][nbsp]of the matrix are indexed by the following row:column pairs:

[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 00 01 02 03 |
[nbsp][nbsp][nbsp][nbsp]M = | 10 11 12 13 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 20 21 22 23 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 30 31 32 33 |

[nbsp][nbsp]The element at the top right of the matrix has i=0 and j=3
[nbsp][nbsp]This is referenced as follows:

[nbsp][nbsp][nbsp][nbsp]M[nbsp][nbsp][nbsp][nbsp]= M
[nbsp][nbsp][nbsp][nbsp] i,j[nbsp][nbsp][nbsp][nbsp]0,3

[nbsp][nbsp]In computer animation, the most commonly used matrices have either
[nbsp][nbsp]2, 3 or 4 rows and columns. These are referred to as 2x2, 3x3 and 4x4
[nbsp][nbsp]matrices respectively.
[nbsp][nbsp]2x2 matrices are used to perform rotations, shears and other types
[nbsp][nbsp]of image processing. General purpose NxN matrices can be used to
[nbsp][nbsp]perform image processing functions such as convolution.
[nbsp][nbsp]3x3 matrices are used to perform low-budget 3D animation. Operations
[nbsp][nbsp]such as rotation and multiplication can be performed using matrix
[nbsp][nbsp]operations, but perspective depth projection is performed using
[nbsp][nbsp]standard optimised into pure divide operations.
[nbsp][nbsp]4x4 matrices are used to perform high-end 3D animation. Operations
[nbsp][nbsp]such as multiplication and perspective depth projection can be
[nbsp][nbsp]performed using matrix mathematics.
[nbsp][nbsp]
[aname='Q2']Q2.[nbsp][nbsp]What is the "order" of a matrix?
-------------------------------------
[nbsp][nbsp]The "order" of a matrix is another name for the size of the matrix.
[nbsp][nbsp]A matrix with M rows and N columns is said to have order MxN.
[nbsp][nbsp]
[aname='Q3']Q3.[nbsp][nbsp]How do I represent a matrix using the C/C++ programming languages?
-----------------------------------------------------------------------
[nbsp][nbsp]The simplest way of defining a matrix using the C/C++ programming
[nbsp][nbsp]languages is to make use of the "typedef" keyword. Both 3x3 and 4x4
[nbsp][nbsp]matrices may be defined in this way ie:

[nbsp][nbsp][nbsp][nbsp]typedef float MATRIX3[9];
[nbsp][nbsp][nbsp][nbsp]typedef float MATRIX4[16];

[nbsp][nbsp][nbsp][nbsp]Since each type of matrix has dimensions 3x3 and 4x4, this requires
[nbsp][nbsp]9 and 16 data elements respectively.
[nbsp][nbsp]At first glance, the use of a single linear array of data values may
[nbsp][nbsp]seem counter-intuitive. The use of two dimensional arrays may seem
[nbsp][nbsp]more convenient ie.

[nbsp][nbsp][nbsp][nbsp]typedef float MATRIX3[3][3];
[nbsp][nbsp][nbsp][nbsp]typedef float MATRIX4[4][4];

[nbsp][nbsp]However, the use of two reference systems for each matrix element
[nbsp][nbsp]very often leads to confusion. With mathemetics, the order is row
[nbsp][nbsp]first (i), column second (j) ie.

[nbsp][nbsp][nbsp][nbsp] Mij

[nbsp][nbsp]Using C/C++, this becomes

[nbsp][nbsp][nbsp][nbsp] matrix[j]

[nbsp][nbsp][nbsp][nbsp] Using two dimensional arrays also incurs a CPU performance penalty in
[nbsp][nbsp]that C compilers will often make use of multiplication operations to
[nbsp][nbsp]resolve array index operations.
[nbsp][nbsp]So, it is more efficient to stick with linear arrays. However, one issue
[nbsp][nbsp]still remains to be resolved. How is an two dimensional matrix mapped
[nbsp][nbsp]onto a linear array? Since there are only two methods (row first/column
[nbsp][nbsp]second or column first/row column).
[nbsp][nbsp]The performance differences between the two are subtle. If all for-next
[nbsp][nbsp]loops are unravelled, then there is very little difference in the
[nbsp][nbsp]performance for operations such as matrix-matrix multiplication.
[nbsp][nbsp]Using the C/C++ programming languages the linear ordering of each
[nbsp][nbsp]matrix is as follows:

[nbsp][nbsp]mat[0][nbsp][nbsp]= M[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]mat[3][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[12] = M[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]mat[15] = 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]0[nbsp][nbsp]1[nbsp][nbsp]2[nbsp][nbsp]3 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0 1 2 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp]4[nbsp][nbsp]5[nbsp][nbsp]6[nbsp][nbsp]7 |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |
[nbsp][nbsp][nbsp][nbsp]M = |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]M = | 3 4 5 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp]8[nbsp][nbsp]9 10 11 |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]|[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] |[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 6 7 8 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 12 13 14 15 |

[aname='Q4']Q4.[nbsp][nbsp]What are the advantages of using matrices?
-----------------------------------------------
[nbsp][nbsp]One of the first questions asked about the use of matrices in computer
[nbsp][nbsp]animation is why they should be used at all in the first place.
[nbsp][nbsp]Intuitively, it would appear that the overhead of for-next loops and
[nbsp][nbsp]matrix multiplication would slow down an application.
[nbsp][nbsp]Arguments that resolve these objections can be pointed out. These include
[nbsp][nbsp]the use of CPU registers to handle loop counters on-board data caches
[nbsp][nbsp]to optimise memory accesses.
[nbsp][nbsp]Advantages can also be pointed out. By following a mathematical approach
[nbsp][nbsp]to defining 3D algorithms, it is possible to predict and plan the
[nbsp][nbsp]design of a 3D animation system. Such mathematical approaches allow
[nbsp][nbsp]for the implementation of character animation, spline curves and inverse
[nbsp][nbsp]kinematics.
[nbsp][nbsp]However, one objection that frequently comes up is that it would be
[nbsp][nbsp]quicker to just multiply each pair of coordinates by the rotation
[nbsp][nbsp]coefficients for that axis, rather than perform a full vector-matrix
[nbsp][nbsp]multiplication.
[nbsp][nbsp]ie. Rotation in X transforms Y and Z
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]Rotation in Y transforms X and Z
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]Rotation in Z transforms X and Y
[nbsp][nbsp]The argument to this goes as follows:
[nbsp][nbsp]Given a vertex V = (x,y,z), rotation angles (A,B and C) and translation
[nbsp][nbsp](D,E,F). A[nbsp][nbsp]the algorithm
[nbsp][nbsp]is defined as follows:
[nbsp][nbsp][nbsp][nbsp]---------------------------
[nbsp][nbsp][nbsp][nbsp]sx = sin(A)[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] // Setup - only done once
[nbsp][nbsp][nbsp][nbsp]cx = cos(A)
[nbsp][nbsp][nbsp][nbsp]sy = sin(B)
[nbsp][nbsp][nbsp][nbsp]cy = cos(B)
[nbsp][nbsp][nbsp][nbsp]sz = sin(C)
[nbsp][nbsp][nbsp][nbsp]cz = cos(C)
[nbsp][nbsp][nbsp][nbsp]x1 =[nbsp][nbsp]x * cz +[nbsp][nbsp]y * sz[nbsp][nbsp]// Rotation of each vertex
[nbsp][nbsp][nbsp][nbsp]y1 =[nbsp][nbsp]y * cz -[nbsp][nbsp]x * sz
[nbsp][nbsp][nbsp][nbsp]z1 =[nbsp][nbsp]z
[nbsp][nbsp][nbsp][nbsp]x2 = x1 * cy + z1 * sy
[nbsp][nbsp][nbsp][nbsp]y2 = z1
[nbsp][nbsp][nbsp][nbsp]z2 = z1 * cy - x1 * sy
[nbsp][nbsp][nbsp][nbsp]x3 = x2
[nbsp][nbsp][nbsp][nbsp]y3 = y2 * cx + z1 * sx
[nbsp][nbsp][nbsp][nbsp]z3 = z2 * cx - x1 * sx
[nbsp][nbsp][nbsp][nbsp]xr = x3 + D[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] // Translation of each vertex
[nbsp][nbsp][nbsp][nbsp]yr = y3 + E
[nbsp][nbsp][nbsp][nbsp]zr = z3 + F
[nbsp][nbsp][nbsp][nbsp]---------------------------
[nbsp][nbsp]Altogether, this algorithm will use the following amounts of processing
[nbsp][nbsp]time:
[nbsp][nbsp][nbsp][nbsp]Set-up[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] Per-vertex
[nbsp][nbsp][nbsp][nbsp]-------------------------[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]------------------------
[nbsp][nbsp][nbsp][nbsp]6 trigonometric functions
[nbsp][nbsp][nbsp][nbsp]6 assignment operations.[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 12 assignment
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 12 multiplication
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]9 addition
[nbsp][nbsp][nbsp][nbsp]-------------------------[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]------------------------
[nbsp][nbsp]Assume that the same operations is being performed using matrix
[nbsp][nbsp]multiplication.
[nbsp][nbsp]With a 4x4 matrix, the procesing time is used as follows:
[nbsp][nbsp][nbsp][nbsp]Set-up[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] Change[nbsp][nbsp][nbsp][nbsp]Per-vertex[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] Change
[nbsp][nbsp][nbsp][nbsp]--------------------------[nbsp][nbsp] ------[nbsp][nbsp][nbsp][nbsp]------------------------ ------
[nbsp][nbsp][nbsp][nbsp]6[nbsp][nbsp]trigonometric functions[nbsp][nbsp][nbsp][nbsp]0[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]0
[nbsp][nbsp][nbsp][nbsp]18 assignment operation[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]-12[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]3[nbsp][nbsp]assignment[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] -9
[nbsp][nbsp][nbsp][nbsp]12 multiplication[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]+12[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]9[nbsp][nbsp]multiplication[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] -3
[nbsp][nbsp][nbsp][nbsp]6[nbsp][nbsp]subtraction[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] +6[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] 6[nbsp][nbsp]addition[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp] -3
[nbsp][nbsp][nbsp][nbsp]--------------------------[nbsp][nbsp] ------[nbsp][nbsp][nbsp][nbsp]------------------------ ------
[nbsp][nbsp]Comparing the two tables, it can be seen that setting up a rotation
[nbsp][nbsp]matrix costs at least 12 multiplication calculations and an extra
[nbsp][nbsp]18 assignment calls.
[nbsp][nbsp]However, while this may seem extravagant, the savings come from
[nbsp][nbsp]processing each vertex. Using matrix multiplication, the savings made
[nbsp][nbsp]from processing just 4 vertices, will outweigh the additional set-up
[nbsp][nbsp]cost.
[nbsp][nbsp]
[aname='Q5']Q5.[nbsp][nbsp]How do matrices relate to coordinate systems?
--------------------------------------------------
[nbsp][nbsp]With either 3x3 or 4x4 rotation, translation or shearing matrices, there
[nbsp][nbsp]is a simple relationship between each matrix and the resulting coordinate
[nbsp][nbsp]system.
[nbsp][nbsp]The first three columns of the matrix define the direction vector of the
[nbsp][nbsp]X, Y and Z axii respectively.
[nbsp][nbsp]If a 4x4 matrix is defined as:

[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
==========

[aname='Q6']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:

[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 1 0 0 0 |
[nbsp][nbsp][nbsp][nbsp]M = | 0 1 0 0 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0 0 1 0 |
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]| 0 0 0 1 |

[aname='Q7']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]
[/font]
Cancel Save
0 Likes 1 Comments

Comments

NewGirlsGames

WHAT A RESOURCE FOR GAMEDEV bnat games

November 11, 2016 02:02 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement