c++ - 动画围绕特定轴的 qt3d 旋转

标签 c++ qt rotation qml qt3d

我正在尝试为 Qt3D 中的对象设置动画以围绕特定轴(而不是原点)旋转,同时执行其他转换(例如缩放和平移)。

以下代码根据需要旋转对象,但还没有动画。

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

我没有按照我的意愿将 QPropertyAnimation 合并到此代码中。仅对 rotationY 属性进行动画处理并不能包含旋转原点,因此它会围绕错误的轴旋转。对矩阵属性进行动画处理会产生最终结果,但会以我的场景中不希望/不现实的方式旋转。那么,如何为这个旋转设置动画以围绕给定轴旋转?

编辑:有一个 QML 等同于我想要的。在那里,您可以指定旋转的原点,并为角度值设置动画:

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}

我如何在 C++ 中执行此操作?

最佳答案

我认为可以使用 Qt 3D: Simple C++ Example只需修改 orbittransformcontroller.cpp 中的 updateMatrix() 方法即可获得所需内容:

void OrbitTransformController::updateMatrix()
{
    m_matrix.setToIdentity();
    // Move to the origin point of the rotation
    m_matrix.translate(40, 0.0f, -200);
    // Infinite 360° rotation
    m_matrix.rotate(m_angle, QVector3D(0.0f, 1.0f, 0.0f));
    // Radius of the rotation
    m_matrix.translate(m_radius, 0.0f, 0.0f);
    m_target->setMatrix(m_matrix);
} 

注意:将圆环体变成小球体更容易观察旋转。


提问者编辑:这个想法确实是解决问题的好方法!要将它专门应用于我的场景,updateMatrix() 函数必须如下所示:

void OrbitTransformController::updateMatrix()
{
    //take the existing matrix to not lose any previous transformations
    m_matrix = m_target->matrix();
    // Move to the origin point of the rotation, _rotationOrigin would be a member variable
    m_matrix.translate(_rotationOrigin);
    // rotate (around the y axis)
    m_matrix.rotate(m_angle, QVector3D(0.0f, 1.0f, 0.0f));
    // translate back
    m_matrix.translate(-_rotationOrigin);
    m_target->setMatrix(m_matrix);
} 

我已将 _rotationOrigin 也设为 Controller 类中的一个属性,然后可以在外部为每个 Controller 将其设置为不同的值。

关于c++ - 动画围绕特定轴的 qt3d 旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51742892/

相关文章:

C++ - 在 unordered_map 中使用多个值作为键的最佳方式

C++ Java like enum header 编译器错误

c++ - C/C++ 功能/系统测试的测试框架?

Python多重继承中的错误 "TypeError: could not convert X to Y"

c++ - Linux : how to handle fonts when deploying? 上的静态 Qt5 构建

python - OpenCV Python围绕特定点将图像旋转X度

iphone - 旋转时调整 iPhone 上的 View 大小

c++ - C++中自己的异常不能抛出

c++ - 根节点以上内容的 Qt xml 文件问题

rotation - Libgdx 贴花围绕自定义轴旋转