簡體   English   中英

帶 C ++ 和旋轉的 QT3D

[英]QT3D with C ++ and rotation

我希望有人可以幫助我解決這個問題。 我是使用 QT3D 的新手,我需要使用 QT C++ 來制作 Qt3D 應用程序,我遇到的問題是我需要從特定點旋轉圖形,但它總是從中心點旋轉。 如何指定旋轉是由圖中的樞軸之一進行的? 我需要它能夠模擬鍾擺的運動。 請幫助告訴我如何解決此問題的人,這是我的代碼。

void window::paint(){
    arm1 = new Qt3DExtras::QCylinderMesh();
    arm1->setRadius(0.5);
    arm1->setLength(3);

    arm1Transform = new Qt3DCore::QTransform();
    arm1Transform->setTranslation(QVector3D(-3, 3, 0));
    arm1Transform->setScale(1.5f);

    Qt3DExtras::QPhongMaterial *arm1Material = new Qt3DExtras::QPhongMaterial();
    arm1Material->setDiffuse(Qt::red);

    arm1Entity = new Qt3DCore::QEntity(rootEntity);
    arm1Entity->addComponent(arm1);
    arm1Entity->addComponent(arm1Material);
    arm1Entity->addComponent(arm1Transform);
}

void window::on_pushButton_clicked(){
    arm1Transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), angle++));
}

提前致謝。

您可以使arm1Entity成為新 QEntity 的子級。 這個新的 QEntity 將定位在您要旋轉的位置。 所以如果你旋轉新的 QEntity,他的子arm1Entity將從新的 QEntity 的 position 旋轉。

編輯代碼(未測試):

void window::paint(){
    arm1PivotEntity = new Qt3DCore::QEntity(rootEntity);
    arm1PivotTransform = new Qt3DCore::QTransform(arm1PivotEntity);
    arm1PivotTransform->setTranslation(QVector3D(/*desired pivot position*/));
    arm1PivotEntity->addComponent(arm1PivotTransform);

    arm1 = new Qt3DExtras::QCylinderMesh();
    arm1->setRadius(0.5);
    arm1->setLength(3);

    arm1Transform = new Qt3DCore::QTransform();
    // as arm1 is now child of arm1 pivot, you need to set the relative position
    arm1Transform->setTranslation(QVector3D(/*relative position to arm1pivot position*/)); 
    arm1Transform->setScale(1.5f);

    Qt3DExtras::QPhongMaterial *arm1Material = new Qt3DExtras::QPhongMaterial();
    arm1Material->setDiffuse(Qt::red);

    // made arm1 child of arm1pivot so it will inherits its rotation
    arm1Entity = new Qt3DCore::QEntity(arm1PivotEntity); 
    arm1Entity->addComponent(arm1);
    arm1Entity->addComponent(arm1Material);
    arm1Entity->addComponent(arm1Transform);
}

void window::on_pushButton_clicked(){
    arm1PivotTransform->setRotation(angle++));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM