# quaternions and matrix translation

This topic is 4828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have the following code for convering a rotation matrix to a quaternion. <source lang="c++"> void matToQuat(float *m, float *quat) { float tr, s, q[4]; int i, j, k; int nxt[3] = {1, 2, 0}; tr = m[0*4+0] + m[1*4+1] + m[2*4+2]; // check the diagonal if (tr > 0.0) { s = sqrt (tr + 1.0); quat[3] = s / 2.0; s = 0.5 / s; quat[0] = (m[1*4+2] - m[2*4+1]) * s; quat[1] = (m[2*4+0] - m[0*4+2]) * s; quat[2] = (m[0*4+1] - m[1*4+0]) * s; } else { // diagonal is negative i = 0; if (m[1*4+1] > m[0*4+0]) i = 1; if (m[2*4+2] > m[i*4+i]) i = 2; j = nxt; k = nxt[j]; s = sqrt ((m[i*4+i] - (m[j*4+j] + m[k*4+k])) + 1.0); q = s * 0.5; if (s != 0.0) s = 0.5 / s; q[3] = (m[j*4+k] - m[k*4+j]) * s; q[j] = (m[i*4+j] + m[j*4+i]) * s; q[k] = (m[i*4+k] + m[k*4+i]) * s; quat[0] = q[0]; quat[1] = q[1]; quat[2] = q[2]; quat[3] = q[3]; } } </source> But how do I preserve translations and scaling that I put into a matrix. Is there anyway to convert this data into the quaternion as well? Does a quaternion even store translation or scaling ?

##### Share on other sites
Quaternions are just another way to represent rotation (an axis, and the angle rotated around that axis). They have nice blending properties which is why they are used in animation systems. They are also pretty compact and can be quickly normalized (faster than a 3x3 matrix).

I've never used (or heard of) them containing translation. As for scale - I'm not sure what a non-unit quaternion represents - any math boffins out there care to answer?

-Steve.

##### Share on other sites
No, quaternions cannot represent scaling or translation. Typically an animation system that uses quaternions will store transforms as an 'SRT' - scale, rotate, translate. A single scalar represents uniform scaling (you could store a vector instead to represent non-uniform scaling), a single vector represents the translation and a single quaternion represents the rotation.

##### Share on other sites
Quaternions represents rotation only...

Since im a java programmer I dont have those problems. I simply use this line:

Matrix4f.get(Quat4f q);

It simply extracts the quaternion of a matrix and place it in the Quat4f argument.

I love java=)

##### Share on other sites
In my opinion quaternions are usefull only as "matrix interpolator"; I've my own quat class as everyone but never used it directly.

To oconnellseanm: as you know a quaternion contains 4 values; intuitively you can think that 3 of these values represent the "forward" vector that is the vector describing the look direction of your frame (object,camera,...).
You can think that the 4th value is the angle that fixes the second versor of your frame (alias the 'up' vector). The third vector can be computed as the cross-product so it's defined implicitly.
As you can see there is no place for the position that requires a new (3d) vector.

I bet that storing a generic rotation+scaling+translation requires a matrix4x4!

Quaternions are not the fastest or most efficient way to store frame data; for example if you have a polar frame (ie quake camera style) you can perform camera interpolation and description with only 2 + 3 floats (angles+position).
Also a trackball can be described with only 5 floats.
And the interpolation is simply the algebric interpolation between these 5 floats!

However quaternion conversions can be usefull, at least used implicitly, if you have to interpolate generic matrices/frames for which you know "nothing".

##### Share on other sites
Quote:
 intuitively you can think that 3 of these values represent the "forward" vector that is the vector describing the look direction of your frame...
Can you clarify this? I may be misunderstanding you, but it sounds like you're saying that the vector component of the quaternion is aligned with the forward vector of the corresponding orthonormal basis. I don't believe that is correct; the quaternion vector component relates to the axis of rotation, which is not necessarily coincident with any of the corresponding basis vectors.

Correct me if I'm mininterpreting...

##### Share on other sites
This is not true...this is only a way to understand why a quaternion (4 floats) can describe a 3x3 matrix (9 floats).
The quaternion itself is an abstraction (like the matrix); it's only one of the methods.

##### Share on other sites
The unit quaternion does not contain forward and up vectors. There is a continuous mapping between unit quaternions and 3d rotations, and this mapping does relate to the axis of rotation. In the usual representation, the quaternion has a real part and three imaginary parts, i j and k. Well actually it's an extenion of the complex numbers, i*i = j*j = k*k =-1, i*j=k, j*i=-k, and so on. i j and k can be viewed as corresponding to the three dimensional unit directions, and multiplying them is sort of like a cross product... Anyway in the usual rotation representation, the direction of the vector (i,j,k) represents the axis of rotation, and the magnitude is related to the angle of rotation (with some trigitnomitry involved).

A quaternion can represent any 3 dimensional rotation. 2 angles cannot. Blizzard's description of a quake style camera would not support rolling the camera. To represent any position, rotation, and independent scaling along each axis in 3 dimensions requires 9 numbers, 3 for the rotation 3 for the translation and 3 for the scaling. The rotation can be stored as 3 angles. Quaternions tend to be more convenient though since their interpolation is smoother. Interpolating between polar angles does not result in going through the shortest path of rotation, and it can result in an awkward path. There is a method called spherical linear interpolation (SLERP) for interpolating between quaternions. The spherical part is because you want to find the unit quaternions on the 4 dimensional unit sphere between the starting and ending quaternions. This interpolation results in smooth rotation vectors, rotations that rotate the object from one orientation to another along the shortest path at a fixed speed.

##### Share on other sites
Quote:
 Original post by Anonymous PosterThe unit quaternion does not contain forward and up vectors. There is a continuous mapping between unit quaternions and 3d rotations, and this mapping does relate to the axis of rotation. In the usual representation, the quaternion has a real part and three imaginary parts, i j and k. Well actually it's an extenion of the complex numbers, i*i = j*j = k*k =-1, i*j=k, j*i=-k, and so on. i j and k can be viewed as corresponding to the three dimensional unit directions, and multiplying them is sort of like a cross product... Anyway in the usual rotation representation, the direction of the vector (i,j,k) represents the axis of rotation, and the magnitude is related to the angle of rotation (with some trigitnomitry involved).A quaternion can represent any 3 dimensional rotation. 2 angles cannot. Blizzard's description of a quake style camera would not support rolling the camera. To represent any position, rotation, and independent scaling along each axis in 3 dimensions requires 9 numbers, 3 for the rotation 3 for the translation and 3 for the scaling. The rotation can be stored as 3 angles. Quaternions tend to be more convenient though since their interpolation is smoother. Interpolating between polar angles does not result in going through the shortest path of rotation, and it can result in an awkward path. There is a method called spherical linear interpolation (SLERP) for interpolating between quaternions. The spherical part is because you want to find the unit quaternions on the 4 dimensional unit sphere between the starting and ending quaternions. This interpolation results in smooth rotation vectors, rotations that rotate the object from one orientation to another along the shortest path at a fixed speed.

I agree with your post; my uncorrect and simplified considerations about quaternions were only used to show oconnellseanm that a quaternion has not sufficient 'degrees of freedom' to describe a complete transformation.

If you do not consider the rolling you have no need to use quat in quake style camera; if you need roll you need a more complete model.

A complete trackball can be implemented with 2+3 floats without loose any features.
If you have these angles you can compute the SLERP without quaternions: simply compute the algebric interpolation between angles (because the two angles are related to spherical coordinates you have a SLERP by definition! )

However there are cases in which quaternions give you a bad interpolation because the shortest angular path is not always what you want...think for example about an angular distance > PI; in this case the interpolation follows the shortest path and in some circumstances this means invert the rotation direction!

In the general case you can perform SLERP with or without quaternions; although using quaternions I had a bit more performance (~10%) than extracting rotation from matrices. But I find quat difficult to describe; I prefer describe a frame by its forward, up and origin versor and interpolate different frames without deal (directly) with quaternions.

What is a quaternion? Like complex numbers is an abstract method usefull to perform operations...nothing else.

##### Share on other sites
Quote:
 Original post by blizzard999I bet that storing a generic rotation+scaling+translation requires a matrix4x4!

No it does not. You can do it in a 3x4 or 4x3 matrix (depending upon whether you are pre- or post-multiplying your vectors), and depending on your restrictions you can do it with less. Do you allow shearing? Is the scale always uniform or is non-uniform scaling supported? If size were the most important consideration I could get 3 DOF rotation, translation, and non-uniform scaling all stored in 6 floats. It all depends on the restrictions you're willing to place on your data.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633297
• Total Posts
3011251
• ### Who's Online (See full list)

There are no registered users currently online

×