c++ - 带有 C++ 和旋转的 QT3D
问题描述
我希望有人可以帮助我解决这个问题。我是使用 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 的位置旋转。
编辑代码(未测试):
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++));
}
推荐阅读
- google-apps-script - 如何将添加到 Google 表格单元格中的图像保存到 Google 云端硬盘?
- node.js - 如何在 Typescript 中下载文件(从 URL)
- c++ - C++ 重载/匹配 N 个嵌套模板
- shortest-path - 对于无向边加权图,如何找到从顶点 v 到顶点 w 的最短路径?
- spring-webflux - 使用反应堆通量和 openpdf 创建 pdf
- database-design - 多对多关系中的重复主键
- c# - AspNet.Security.Oauth.Spotify HttpContext.GetTokenAsync("spotify", "access_token") 第二次登录后返回空令牌+
- symfony - 向 Doctrine 添加额外的外键约束
- angular - 我想用角度将数据保存到 json 数据库
- javascript - 创建一个函数 middle(),它返回一个单词的中间字符。如果一个单词有偶数个字符,则返回中间两个字符