Quaternion Class Implementation

Started by
5 comments, last by frob 5 years, 7 months ago

Hii,

short question regarding the implementation of a quaternion class.

I was wondering if quaternions are used for anything other than rotations within the graphics programming context? If this is not the case then one could take several shortcuts implementing specific functions (inverse; to matrix conversion etc.) and boost performance. So, when implementing such a class would you strive for a general purpose class or for one that deals solely with unit quaternions making shortcuts possible?

 

Looking forward to some feedback since I can't decide which route to go down.

Thanks in advance.

Advertisement

I would implement a basic quaternion class which follows the mathematical definition of a quaternion. If I read quaternion this is what I would expect. You still need the basic math operators very often even for unit quaternions. You can derive a Rotation class from your quaternion if you like. Havok does something similar, but with 3x3 matrices. E.g. every rotation can be expressed as a unit quaternion or orthonormal basis (with det == 1), but not every quaternion or matrix is a rotation. This is nice from an API perspective since it makes clear what you are trying to express. In particular when your clients are not super strong at math. I personally don't do this, but it is ok I think. It is somewhat the same as having a separate vector and point class. I am not a friend of this either, but some people seem to like it. Note that there is no right or wrong, but more what is your personal preference. What I don't like is to call a class quaternion, but then just implement a crippled interface for unit quaternions. This is confusing in my opinion.

Non-unit quaternions have actually a use in games quite often. E.g. the quaternion derivative is defined as q' = 0.5 * w * q. Here w is a pure quaternion where the imaginary elements hold the angular velocity and the imaginary part is zero. I use this a lot in physics and IK. 

Hi Dirk,

thanks for your reply. So since quaternions are used in other game systems (e.g. physics, as stated by you) taking the shortcut route seems to be wrong. You have a good point on expecting a quaternion class to do everything that makes a quaternion (not just a unit quaternion).

The only thing that seems to be worth thinking about at this point is whether one is using an existing physics library or writing it from scratch. I would expect the library to come with a quaternion implementation that suffice this specific context. My own quaternion class could be used for the graphics context. But then again mixing two quaternion classes throughout the code base for different intents is also somewhat cumbersome and worsens readability.

Think I will go for a rotator class that uses quaternions under the hood as you suggested.

Thanks again for the input. Very helpful.

I would find it confusing to have a "rotation" class separate from a quaternion.. what purpose does it serve? If you think its confusing for a user to rotate using a quaternion operation then why not just provide a rotate function?

A rotation class makes sense to me. If I am a user of the class, I don't care if it's implemented using a quaternion, a 3x3 matrix or something else.

Actually, I might even have an attitude class and a separate rotation class, similarly to how you can have a point class and a separate vector class. This is so you can compose rotations, you can apply a rotation to an attitude, but you can't compose attitudes and doing so would result in a compilation error.

Or you can just have a quaternion class with a rotate function and be more transparent about how things are implemented. It's a matter of preference.

6 hours ago, alvaro said:

If I am a user of the class, I don't care if it's implemented using a quaternion, a 3x3 matrix or something else.

So much this. I don't care so much how you get the results, only that the library produces the correct results in a timely fashion.

15 hours ago, //Lumia said:

So, when implementing such a class would you strive for a general purpose class or for one that deals solely with unit quaternions making shortcuts possible?

I want it to implement all the operations I use, and I want it to do them quickly and correctly. 

I don't care one bit about operations I don't use, except that if they are operations I am likely to need in the future that they complete quickly and correctly.

Sometimes when writing libraries the hardest bit is finding out what functionally will be used and what is unnecessary.  That's one reason the best libraries tend to be extracted from existing code rather than being generated in isolation. 

This topic is closed to new replies.

Advertisement