I am trying to animate an object in Qt3D to rotate around a specific axis (not the origin) while performing other transformations (e.g. scaling and translating).
The following code rotates the object as I want it but without animation yet.
QMatrix4x4 mat = QMatrix4x4();
mat.scale(10);
mat.translate(QVector3D(-1.023, 0.836, -0.651));
mat.rotate(QQuaternion::fromAxisAndAngle(QVector3D(0,1,0), -20));
mat.translate(-QVector3D(-1.023, 0.836, -0.651));
//scaling here after rotating/translating shifts the rotation back to be around the origin (??)
Qt3DCore::QTransform *transform = new Qt3DCore::QTransform(root);
transform->setMatrix(mat);
//...
entity->addComponent(transform); //the entity of the object i am animating
I did not manage to incorporate a QPropertyAnimation as I desire with this code. Only animating the rotationY property does not let me include the rotation origin, so it rotates around the wrong axis. And animating the matrix property produces the end result but rotates in a way that is not desired/realistic in my scenario. So how can I animate this rotation to rotate around a given axis?
EDIT: There is a QML equivalent to what I want. There, you can specify the origin of the rotation and just animate the angle values:
Rotation3D{
id: doorRotation
angle: 0
axis: Qt.vector3d(0,1,0)
origin: Qt.vector3d(-1.023, 0.836, -0.651)
}
NumberAnimation {target: doorRotation; property: "angle"; from: 0; to: -20; duration: 500}
How can I do this in C++?
rotateAround()
is not a pure rotation (as rotations are always about origin). It does translations as well (to move rotation center). Involving translations, it's again not commutative anymore. First uniform scale and then translate is different than first translate and then uniform scale... ;-) – Scheff's CatupdateMatrix()
method inorbittransformcontroller.cp
:QVector3D translationVector3D(20, 0, 0); m_matrix.translate(translationVector3D); m_matrix.rotate(m_angle, QVector3D(0.0f, 1.0f, 0.0f)); m_matrix.translate(-translationVector3D);
– Julien Déramond