Reputation: 389
Using Qt Quick 3D QML and without having additional C++, how could I multiply two quaternions?
I have a fixed rotation value given in quaternion (Qt.quaternion(a,b,c)
), to which I would like to add a variable part.
Documentation is very scarce about that (I only found quaternion and Transform) and apparently there is no "times()" property similar to the one from vector. On the C++ side, I can multiply and normalize quaternions (QQuaternion)
Edit: This was a problem in Qt6.0. For Qt6.4 and further there are many quaternion manipulation methods, see Stephen Quan answer.
Upvotes: 2
Views: 448
Reputation: 25966
In Qt6, there are many useful methods on quaternion for multiplying with another quaternion, vector, or scalar values.
var a = Qt.quaternion(1 / Math.sqrt(2), 1 / Math.sqrt(2), 0, 0);
var b = Qt.quaternion(1 / Math.sqrt(2), 0, 1 / Math.sqrt(2), 0);
var c = b.times(a);
console.log(c.toString()); // QQuaternion(0.5, 0.5, 0.5, -0.5)
var a = Qt.quaternion(0.5,0.5,0.5,-0.5);
var b = Qt.vector3d(4,5,6);
var c = a.times(b);
console.log(c.toString()); // QVector3D(5, -6, -4)
var a = Qt.quaternion(1,2,3,4);
var b = 4.48;
var c = a.times(b);
console.log(c.toString()); // QQuaternion(4.48, 8.96, 13.44, 17.92)
See https://doc.qt.io/qt-6/qml-quaternion.html
Upvotes: 0
Reputation: 5472
I would recommend writing your own JavaScript function doing the multiplication. One example implementation (multiplyQuaternion()
) can be seen in this answer to another question.
You can also take a look into implementation of inline const QQuaternion operator*(const QQuaternion &q1, const QQuaternion& q2)
in QQuaternion class for reference when writing your own JS function.
Another possibility might be to utilize some ready-made JS implementation (if found) by importing a JS file in question into your QML.
You could also write QObject-based C++ wrapper which utilizes QQuaternion
class and then expose it to QML. But you would have to link with Qt Gui module and write quite a lot of boilerplate code because of that one function which probably doesn't make too much sense.
Upvotes: 1