Question

I'm working with qglwidget and various gestures for an android app and the topic of Quaternions is thoroughly confusing so its been mostly guess and check. I've been able to make a rotation about one axis by some number of degrees using:

rotation=QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),delta.y())*rotation;

This has the desired results as does the same statement in the x direction.

My question is, for one, is the the correct way of doing a rotation? And two, if I want to rotate on two axes do I just do:

rotation=QQuaternion::fromAxisAndAngle(QVector3D(1,0,0),delta.y())*rotation;
rotation=QQuaternion::fromAxisAndAngle(QVector3D(0,1,0),delta.x())*rotation;

Or is there a one line statement that will work just as well?

Was it helpful?

Solution

Yes, you are doing it the right way, there are no one-line statement :-)

It is very common in 3D applications to create a quaternion from a set of Euler angles, and we do this simply by multiplying together the most basic rotations, since it is anyway pretty cheap to compute (unless you are doing a lot of them, and determined by profiling that this part was critical for performance). For instance, if you are using the convention Z-X-Z (as illustrated in the first picture here ), then you would write:

QQuaternion rotation =
    QQuaternion::fromAxisAndAngle(QVector3D(0,0,1), alpha) *
    QQuaternion::fromAxisAndAngle(QVector3D(1,0,0), beta) * 
    QQuaternion::fromAxisAndAngle(QVector3D(0,0,1), gamma);

where alpha, beta and gamma are double values representing the angles in degrees (be careful, not in radians).

Note: you can create the one-liner yourself by wrapping it in your own method:

static QQuaternion fromEuler(double alpha, double beta, double gamma);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top