此代码示例在这里:
import QtQuick 2.0
Item {
width: 200; height: 200
Rectangle {
width: 100; height: 100
anchors.centerIn: parent
color: "#00FF00"
Rectangle {
color: "#FF0000"
width: 10; height: 10
anchors.top: parent.top
anchors.right: parent.right
}
}
}
将产生以下输出:
现在我想从这个绿色矩形的中心应用 3D 旋转。首先,我想在 X 轴上旋转 -45 度(向下鞠躬),然后在 Y 轴上旋转 -60 度(向左转)。
我使用下面的 C++ 代码片段,在侧面使用 GLM 来帮助我计算轴和角度:
// generate rotation matrix from euler in X-Y-Z order
// please note that GLM uses radians, not degrees
glm::mat4 rotationMatrix = glm::eulerAngleXY(glm::radians(-45.0f), glm::radians(-60.0f));
// convert the rotation matrix into a quaternion
glm::quat quaternion = glm::toQuat(rotationMatrix);
// extract the rotation axis from the quaternion
glm::vec3 axis = glm::axis(quaternion);
// extract the rotation angle from the quaternion
// and also convert it back to degrees for QML
double angle = glm::degrees(glm::angle(quaternion));
这个 C++ 小程序的输出给了我一个轴{-0.552483, -0.770076, 0.318976}
和一个角度73.7201
。所以我将示例代码更新为:
import QtQuick 2.0
Item {
width: 200; height: 200
Rectangle {
width: 100; height: 100
anchors.centerIn: parent
color: "#00FF00"
Rectangle {
color: "#FF0000"
width: 10; height: 10
anchors.top: parent.top
anchors.right: parent.right
}
transform: Rotation {
id: rot
origin.x: 50; origin.y: 50
axis: Qt.vector3d(-0.552483, -0.770076, 0.318976)
angle: 73.7201
}
}
}
这正是我想看到的:
到目前为止,一切都很好。现在是最困难的部分。我如何制作这个动画?例如,如果我想从 {45.0, 60.0, 0} 转到 {45.0, 60.0, 90.0}。换句话说,我想从这里开始制作动画
to here
我将目标旋转插入此处
// generate rotation matrix from euler in X-Y-Z order
// please note that GLM uses radians, not degrees
glm::mat4 rotationMatrix = glm::eulerAngleXYZ(glm::radians(-45.0f), glm::radians(-60.0f), glm::radians(90.0f);
// convert the rotation matrix into a quaternion
glm::quat quaternion = glm::toQuat(rotationMatrix);
// extract the rotation axis from the quaternion
glm::vec3 axis = glm::axis(quaternion);
// extract the rotation angle from the quaternion
// and also convert it back to degrees for QML
double angle = glm::degrees(glm::angle(quaternion));
这给了我一个轴{-0.621515, -0.102255, 0.7767}
和一个角度129.007
所以我将此动画添加到我的示例中
ParallelAnimation {
running: true
Vector3dAnimation {
target: rot
property: "axis"
from: Qt.vector3d(-0.552483, -0.770076, 0.318976)
to: Qt.vector3d(-0.621515, -0.102255, 0.7767)
duration: 4000
}
NumberAnimation {
target: rot;
property: "angle";
from: 73.7201; to: 129.007;
duration: 4000;
}
}
这“几乎”有效。问题是,如果您尝试一下,您会发现动画前半部分的旋转完全偏离了所需的旋转轴,但动画后半部分的旋转自行修复。起始轮换很好,目标轮换也很好,但无论中间发生什么都不够好。如果我使用较小的角度(如 45 度而不是 90 度)会更好,如果我使用较大的角度(如 180 度而不是 45 度)则会最糟糕,因为它只会以随机方向旋转,直到达到最终目标。
如何让该动画在开始旋转和目标旋转之间看起来正确?
- - - - - - - - - - 编辑 - - - - - - - - - -
我添加了一个标准:我正在寻找的答案必须绝对提供相同的输出正如我上面提供的屏幕截图。
例如,将 3 个旋转轴拆分为 3 个单独的旋转变换不会给出正确的结果
transform: [
Rotation {
id: zRot
origin.x: 50; origin.y: 50;
angle: 0
},
Rotation {
id: xRot
origin.x: 50; origin.y: 50;
angle: -45
axis { x: 1; y: 0; z: 0 }
},
Rotation {
id: yRot
origin.x: 50; origin.y: 50;
angle: -60
axis { x: 0; y: 1; z: 0 }
}
]
会给我这个:
这是不正确的。