Skip to content
HN On Hacker News ↗

Matrix and Quaternion FAQ

▲ 30 points 8 comments by signa11 3w ago HN discussion ↗

Pangram verdict · v3.3

We believe that this document is fully human-written

0 %

AI likelihood · overall

Human
100% human-written 0% AI-generated
SEGMENTS · HUMAN 7 of 7
SEGMENTS · AI 0 of 7
WORD COUNT 1,973
PEAK AI % 1% · §5
Analyzed
Jun 23
backend: pangram/v3.3
Segments scanned
7 windows
avg 282 words each
Distribution
100 / 0%
human / AI fraction
Verdict
Human
Pangram v3.3

Article text · 1,973 words · 7 segments analyzed

Human AI-generated
§1 Human · 0%

The Matrix and Quaternions FAQ ============================== Version 1.21 30th November 2003 ------------------------------- Please mail feedback to matrix_faq@j3d.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: http://www.j3d.org/matrix_faq/matrfaq_latest.html Feel free to distribute or copy this FAQ as you please. Contributions ------------- Introduction I1: Steve ????? Correction to Q55 until Q59: Andreas Junghanns Correction to Q50: Morten Ofstad Note to Q39: Tom Nuydens Corrections to Q29 and Q37: Eric Reiss Clarification to Q56: Duncan Murdoch Clarification to Q37: Ron Avitzur Correction to Q1: Mona Wong Corrections to Q36 and Q37: Eric Reiss Improvement to Q34 and Q38: Jon Watte Warning and alternative to Q58 and Q59: Paul Pedriana Correction (and optimization [Lee]) to Q53: Eleanor Groundwater and Lee Morgan Improvement to Q39: jhunpingco Corrections to Q11 and optimization to Q12: Gordon Corrections to Q54 to Q60: Eleanor Groundwater Corrections and improvements to Q23 and Q24: Ben Houston Addition to Q39: Jon Watte Correction to Q61: Adam D. Moss Addition of Q63: Mike Cline Addition of I2: Jacob Marner Correction to Q38 and inception of I2: Armin Mller Addition of Q60: Pablo Figueroa Correntions and additions to Q14, Q16, Q21 and Q34: Tronster Hartley Correction to Q12 and Q54: Frank DJ Correction to Q34: Robert Funnell 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.

§2 Human · 0%

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

§3 Human · 0%

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. Introduction ------------ I1. Important note relating to OpenGL and this document I2. Important note with respect to normalized inputs Questions --------- BASICS ====== Q1. What is a matrix? Q2. What is the order of a matrix? Q3. How do I represent a matrix using the C/C++ programming languages? Q4. What are the advantages of using matrices? Q5. How do matrices relate to coordinate systems? ARITHMETIC ========== Q6. What is the identity matrix? Q7. What is the major diagonal matrix of a matrix? Q8. What is the transpose of a matrix? Q9. How do I add two matrices together? Q10. How do I subtract two matrices? Q11. How do I multiply two matrices together? Q12. How do I square or raise a matrix to a power? Q13. How do I multiply one or more vectors by a matrix? DETERMINANTS AND INVERSES ========================= Q14. What is the determinant of a matrix? Q15. How do I calculate the determinant of a matrix? Q16. What are Isotropic and Anisotropic matrices?

§4 Human · 0%

Q17. What is the inverse of a matrix? Q18. How do I calculate the inverse of an arbitary matrix? Q19. How do I calculate the inverse of an identity matrix? Q20. How do I calculate the inverse of a rotation matrix? Q21. How do I calculate the inverse of a matrix using Kramer's rule? Q22. How do I calculate the inverse of a 2x2 matrix? Q23. How do I calculate the inverse of a 3x3 matrix? Q24. How do I calculate the inverse of a 4x4 matrix? Q25. How do I calculate the inverse of a matrix using linear equations? TRANSFORMS ========== Q26. What is a rotation matrix? Q27. How do rotation matrices relate to coordinate systems? Q28. How do I generate a rotation matrix in the X-axis? Q29. How do I generate a rotation matrix in the Y-axis? Q30. How do I generate a rotation matrix in the Z-axis? Q31. What are Euler angles? 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?

§5 Human · 1%

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? Introduction ------------ 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: M[0][1] = M[0][2] = M[0][3] = M[1][0] = M[1][2] = M[1][3] = M[2][0] = M[2][1] = M[2][3] = 0 ; M[0][0] = M[1][1] = M[2][2] = m[3][3] = 1 ; M[3][0] = X ; M[3][1] = Y ; M[3][2] = Z ; ie, the matrix is stored like

§6 Human · 0%

this: | M[0][0] M[1][0] M[2][0] M[3][0] | | | | M[0][1] M[1][1] M[2][1] M[3][1] | M = | | | M[0][2] M[1][2] M[2][2] M[3][2] | | | | M[0][3] M[1][3] M[2][3] M[3][3] | 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. This Document OpenGL | 0 1 2 3 | | 0 4 8 12 | | | | | | 4 5 6 7 | | 1 5 9 13 | M = | | M = | | | 8 9 10 11 | | 2 6 10 14 | | | | | | 12 13 14 15 | | 3 7 11 15 | 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. Answers ------- BASICS ====== 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.

§7 Human · 0%

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: | 00 01 02 03 | M = | 10 11 12 13 | | 20 21 22 23 | | 30 31 32 33 | 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: typedef float MATRIX3[9]; typedef float MATRIX4[16]; Since each type of matrix has dimensions 3x3 and 4x4, this requires 9 and 16 data elements respectively.