You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Qt Quick3D中如何使用NumberAnimation实现物体绕原点Z轴旋转

问题描述

目前通过NumberAnimation已可实现3D物体绕自身x、y、z任意坐标轴旋转,但需要实现物体绕场景原点旋转的效果,本次需求为绕Z轴旋转,预期效果如下:
绕Z轴旋转预期效果

最小可复现示例代码
import QtQuick 2.15
import QtQuick.Window 2.14
import QtQuick3D 1.15
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.12

Window {
    id: window
    width: 1280
    height: 720
    visible: true
    property int i: 0
    property double distance: 1000

    Node {
        id: standAloneScene

        DirectionalLight {
            ambientColor: Qt.rgba(1.0, 1.0, 1.0, 1.0)
        }

        ParallelAnimation{
            id: rotation_y
            running: false
            NumberAnimation { target: cube; property: "eulerRotation.y"; to: 180*i;
                duration: 500; easing.type: Easing.InOutQuad}
        }

        Model {
            id: cube
            source: "#Cube"
            y: 18
            scale: Qt.vector3d(1, 1, 1)
            materials: [
                DefaultMaterial {
                    diffuseColor: Qt.rgba(0.228, 0.185, 0.142, 0.5)
                }
            ]
        }

        OrthographicCamera {
            id: cameraPerspectiveIsometry
            position: Qt.vector3d(distance, distance*1.5, distance)
            eulerRotation.x: -45
            eulerRotation.y: 45
        }
    }
    Rectangle {
        id: isometry
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        height: parent.height
        color: "#848895"
        border.color: "black"

        Label {
            text: "Isometry"
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.margins: 10
            color: "#222840"
            font.pointSize: 14
        }

        View3D {
            id: topLeftView
            anchors.fill: parent
            importScene: standAloneScene
            camera: cameraPerspectiveIsometry

        }
    }


    Button {
        anchors.bottom: parent.bottom
        anchors.right: right.right
        text: "Start"
        font.pixelSize: height / 2
        onClicked:{
            i++;
            rotation_y.restart();
        }
    }
}
实现方法

Qt Quick 3D的节点变换遵循先旋转、后平移的渲染顺序,直接对偏离原点的模型做旋转,旋转中心永远是模型自身的锚点,也就是绕自身轴旋转;要实现绕场景原点旋转,只需要调整节点层级,把旋转操作作用在原点位置的空父节点上即可,修改步骤如下:

  • 在场景中新增一个位置固定在原点(0,0,0)的空Node作为旋转承载节点
  • 将原cube模型作为该空节点的子元素,保留原有的y:18位置偏移
  • 将旋转动画的target从原cube修改为新增的空旋转节点,如果要实现本次需求的绕Z轴旋转,把动画属性改为eulerRotation.z即可,其余动画参数保持不变

修改后的核心代码片段:

// 场景内新增原点位置的旋转父节点
Node {
    id: rotationRoot
    position: Qt.vector3d(0, 0, 0)

    ParallelAnimation{
        id: rotation_z
        running: false
        // 动画目标改为旋转父节点,属性改为绕Z轴旋转
        NumberAnimation { target: rotationRoot; property: "eulerRotation.z"; to: 180*i;
            duration: 500; easing.type: Easing.InOutQuad}
    }

    Model {
        id: cube
        source: "#Cube"
        y: 18 // 模型自身偏移保留,父节点旋转时会带着模型绕原点做圆周运动
        scale: Qt.vector3d(1, 1, 1)
        materials: [
            DefaultMaterial {
                diffuseColor: Qt.rgba(0.228, 0.185, 0.142, 0.5)
            }
        ]
    }
}

如果需要实现绕任意指定坐标点旋转,只需要把空父节点的position设置为目标旋转点坐标,再旋转该父节点即可。


内容的提问来源于stack exchange,提问作者Vladimir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 20:27:27