{"id":60,"date":"2013-11-08T15:46:23","date_gmt":"2013-11-08T15:46:23","guid":{"rendered":"\/\/3dbym.ru\/2013\/11\/converting-between-matrices-and-quaternions\/"},"modified":"2013-11-08T15:46:23","modified_gmt":"2013-11-08T15:46:23","slug":"converting-between-matrices-and-quaternions","status":"publish","type":"post","link":"https:\/\/3dbym.ru\/2013\/11\/converting-between-matrices-and-quaternions\/","title":{"rendered":"Converting Between Matrices and Quaternions"},"content":{"rendered":"

One of the most important aspects of quaternions is their ability to be converted into rotation matrices. This is useful because graphics APIs such as OpenGL and Direct3D cannot accept quaternions directly rather, they rely on matrices to perform transformations. Because of this, without being able to convert between quaternions, your graphics would become useless because you would not be able to use the infor\u00admation they contain along with OpenGL or other graphics APIs. For example, if you have two sets of rotations for an object, the first set is<\/p>\n

<\/p>\n

the starting rotation and the second is the ending rotation. Without being able to convert these matrices to quaternions, it would be impossible to take advantage of the ease of interpolation between quaternions. Keep in mind that all matrices will be referenced in the manner mij where i is the row and j is the column.<\/p>\n

This section first investigates how to convert a quaternion to a matrix. Because there is much more to cover in the later chapters of the book, there is not room to go through the \u201cwhys\u201d of the conversion. Instead, this section looks at the formula for the conversion. Figure 2.5 shows how to generate a 3×3 rotation matrix from a quaternion. Keep in mind that when writing code to do the conversion, it is helpful to calculate some of the multiplication ahead of time to avoid unnecessary overhead.<\/p>\n

w2<\/a> + x2 \u2014 y2 \u2014 z2 2xy-2wz 2xz+2yn<\/p>\n

2xy+2wz w2 \u2014 x2 + y2 \u2014 z2 2yz-2wx 2xz-2wy 2yz-2wx w2-x2-y2 + z2<\/a><\/p>\n

Figure 2.5 Converting a quaternion to a rotation matrix. W represents the scalar value and X, Y, and Z represent the three parts of the vector component.<\/p>\n

Converting back to a quaternion is a little bit harder, but not much. The first thing you do is calculate the trace of the matrix using the formula (tr = m11+m22+m33). If that value comes out to be greater than zero, you can perform an \u201cinstant calculation\u201d using the follow\u00ading formula:<\/p>\n

temp = 1<\/p>\n

2 \/tr+ 1 0.25<\/p>\n

Qw =<\/p>\n

temp<\/p>\n

Qx = (\u0442\u0433\u044d ~ m\u0437\u0433) x temp qy = (m31 — m13) x temp<\/p>\n

qz = (m12 — m21) x temp<\/p>\n

<\/p>\n\n\n
\n<\/td>\n<\/tr>\n<\/table>\n

<\/p>\n

If it is less than or equal to zero, you have to take a different approach that depends on which element of the major diagonal is the largest. If the upper-left corner is the largest, you use this formula:<\/p>\n

temp =———- 1<\/p>\n

2\/l + — m22 + m33<\/p>\n

0.25<\/p>\n

Qw —<\/p>\n

temp<\/p>\n

qx = (m2, + m12) x temp qy = (m13 + m31) x temp qz = (m32 — m23) x temp<\/p>\n

If the middle element outshines the others, use this formula: 1<\/p>\n

temp =<\/p>\n

ff?22 \u2014 \/\u041f33<\/p>\n

Qw = (\/\u0442\u0442\u0433\u0433 + m12) x temp 0.25<\/p>\n

Qx =<\/p>\n

temp<\/p>\n

Qy = (m32 + m23) x temp qz=(m13-m31)xtemp<\/p>\n

And finally, if the lower-right corner takes the cake, use this one: temp = 1<\/p>\n

+ \u0413\u041f33 \/77 \/ \u0443 \u041b?22<\/p>\n

Qw= (m13+ m31)xtemp<\/p>\n

Qx = (m32 + m23) x temp<\/p>\n

_ 0.25 temp<\/p>\n

Qz = (m2) — m12) x temp<\/p>\n

<\/p>\n\n\n
\n<\/td>\n<\/tr>\n<\/table>\n

<\/p>\n

These formulas convert your rotation matrix into a quaternion so you can take advantage of the many advantages they have, such as easy interpolation, cheaper multiplication, and less storage space.<\/p>\n","protected":false},"excerpt":{"rendered":"

One of the most important aspects of quaternions is their ability to be converted into rotation matrices. This is useful because graphics APIs such as OpenGL and Direct3D cannot accept quaternions directly rather, they rely on matrices to perform transformations. Because of this, without being able to convert between quaternions, your graphics would become useless […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts\/60"}],"collection":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":0,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"wp:attachment":[{"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3dbym.ru\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}