   Efficiency of quaternions and matrices
  On Wikipedia they say that rotations with quaternions *may* be more efficient than with matrices.Here they give the efficiency comparison:Quote:Storage requirementsMethod StorageRotation matrix 9Quaternion 4Angle/axis 4*Performance comparison of rotation chaining operationsMethod # multiplies # add/subtracts total operationsRotation matrices 27 18 45Quaternions 16 12 28Performance comparison of vector rotating operationsMethod  # multiplies # add/subtracts # sin/cos   total operationsRotation matrix 9 6 0 15Quaternions 21 18 0 39Angle/axis 23 16 2 41Sorry for messed up formatting.For the actual vector rotating operation, a rotation matrix is more efficient than a quaternion, 15 operations versus 39 operations. For chain multiplication, quaternions are more efficient instead. I think they mean multiplying different rotations by this.Do you think the gain from chaining multiple rotations makes up for the slower rotation on a vector? Do you have to "chain multiply" more than doing rotations directly on vectors in games?Doing a rotation matrix twice on one vector is only 30 operations, while first multiplying the two matrices together is 45 operations plus another 15 for then applying it to the vector.So, in practice, which is most efficient?EDIT: Oh, and if your transformation would also do other things than rotations, especially translation (when using 4x4 matrix), and maybe scaling, mirroring, ..., can quaternions still be used to chain such transformations, or do they only work for pure rotations?   
  In game programming, I think quaternions have an advantage, since you can do the vector multiplying on the GPU(you would have to send 4 floats, as opposed to 9 for a matrix), and thus partially solve your problem.   ;  I wouldn't rely on a straight up efficiency comparison to decide which is faster.  Profile, profile, profile, and you'll quickly see whats faster.  The non-linearity when transforming code into actual machine language makes it nearly impossible to guess which is faster.Edit: You CAN use the storage numbers, however the storage for a matrix or quaternion is rarely a limiting factor.   ;Quote:Original post by LodeEDIT: Oh, and if your transformation would also do other things than rotations, especially translation (when using 4x4 matrix), and maybe scaling, mirroring, ..., can quaternions still be used to chain such transformations, or do they only work for pure rotations?Quaternions can only be used for rotations.   ;Quote:Original post by LodeFor the actual vector rotating operation, a rotation matrix is more efficient than a quaternion, 15 operations versus 39 operations.An uncommon operation. By far the most common thing to do with a quaternion (other than using multiplication to effect a rotation) is to convert it to a rotation matrix, then use that rotation matrix to transform thousands of vectors. Under those circumstances, the performance of the quaternion itself is negligible.   ;  Wish i could thumbs up that post..Wait maybe.As long as you're saying that you do the rotation math as a quaternion, then convert it to a matrix once, and use the matrix for all vector transformations.   ;Quote:Original post by SneftelQuote:Original post by LodeFor the actual vector rotating operation, a rotation matrix is more efficient than a quaternion, 15 operations versus 39 operations.An uncommon operation. By far the most common thing to do with a quaternion (other than using multiplication to effect a rotation) is to convert it to a rotation matrix, then use that rotation matrix to transform thousands of vectors. Under those circumstances, the performance of the quaternion itself is negligible.Very interesting, thank you.If you generate the rotation matrix only once, and thus its efficiency is negligable, does it matter, for the efficiency, whether you generate this matrix using quaternions or using 4x4 matrics?What is then the advantage of the quaternions?I can think of numerical stability.But isn't it a nice advantage of the 4x4 matrices that you can mix rotations and translations together?   ;Quote:Original post by LodeQuote:[...]convert it to a rotation matrix, then use that rotation matrix to transform thousands of vectors[...]What is then the advantage of the quaternions?Simply said, the advantage is that it doesn't suck.  :-)A quaternion has 4 degrees of freedom, but since it is a unit quaternion, they reduce to 3 degrees of freedom. This is just what you need, which is cool.Quaternion versus matrix is like a table with 3 legs compared to a table with 4 (or even 5) legs. A table with 3 legs has exactly enough legs, and will always be perfectly stable, on any ground. A table with 4 legs takes a lot of artisanal skill and a very even ground to prevent it from wobbling (or a few beer mats). A table with 5 legs is almost guaranteed to wobble.By the way, rotating a vector/point on the GPU directly with a quaternion or translating/rotating with a dual quaternion is barely slower than doing so with a maxtrix. In fact, I think the difference is not noticeable at all. But then, it always depends on what you do... if you need to rotate 100 million points, then maybe :-)The downside is that quaternions are much harder to grasp, there is little, if any, good literature freely available (emphasis on good), and there are few readily available libraries you can use.   ;  I find quaternions a lot easier to construct than matrices when dealing with angular vectors. So quaternions are always my starting point, and matrices the ending point.   ;  For game play it is usually beneficial to work with quaternions because you are mostly only concatenating rotations or slerping rotations. Quaternions are very effective at both those operations.We have a class that has the rotation, scale and translation decomposed into its different parts making it trivially to do transformations.When all is said and done we take the quaternion, scale and translation and generate a 3x4 transformation matrix that is sent to the engine.-= Dave   