大佬们帮看下这个旋转中心该怎么设置啊,这旋转后都脱节了

#include "excavator.h"
#include <QScreen>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
#include "animatedentity.h"
//#include "planeentity.h"

#include <QDebug>
#include <QTimer>
#include <qmath.h>
#include <QFile>


Excavator::Excavator(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::ExcavatorClass)
{
    ui->setupUi(this);

    // 设置滑块1的范围
    ui->horizontalSlider->setMinimum(0);
    ui->horizontalSlider->setMaximum(15);

    // 设置滑块2的范围
    ui->horizontalSlider_2->setMinimum(0);
    ui->horizontalSlider_2->setMaximum(1500);

    // 设置滑块3的范围
    ui->horizontalSlider_3->setMinimum(0);
    ui->horizontalSlider_3->setMaximum(1500);

    // 设置滑块4的范围
    ui->horizontalSlider_4->setMinimum(0);
    ui->horizontalSlider_4->setMaximum(180);

    level = -0.15f;
    Qt3DCore::QEntity* sceneRoot = new Qt3DCore::QEntity();
    Qt3DExtras::Qt3DWindow* view = new Qt3DExtras::Qt3DWindow();

    view->defaultFrameGraph()->setClearColor(QRgb(0xffffff));

    view->setRootEntity(sceneRoot);
    initializeLight(sceneRoot);


    basicCamera = view->camera();
    basicCamera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
    basicCamera->setAspectRatio(view->width() / view->height());
    basicCamera->setFieldOfView(45);
    basicCamera->setUpVector(QVector3D(0.0f, 1.0f, 0.0f));
    basicCamera->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
    basicCamera->setPosition(QVector3D(0, 0, 20));

    camController = new TrackballCameraController(sceneRoot);
    camController->setWindowSize(view->size());
    camController->setCamera(basicCamera);
    camController->setRotationSpeed(2.0);


    //    mBase = new AnimatedEntity(sceneRoot);//0.00715,-0.63505,0.86365
    //    mBase->setSource("qrc:/new/obj/model/base.obj");

    m_diPan = new AnimatedEntity(sceneRoot);//0,-0.635688,0.911992
    m_diPan->setSource("qrc:/Excavator/dipan.obj");
    m_diPan->transform()->setScale(0.005f);
    if (m_diPan) {
        qDebug() << "Model entity created successfully!";
    }
    else {
        qDebug() << "Failed to create model entity!";
    }
    cylinderMaterial = new Qt3DExtras::QPhongMaterial();
    cylinderMaterial->setDiffuse(QColor(QRgb(0x928927)));
    m_diPan->addComponent(cylinderMaterial);

    m_1Arm = new AnimatedEntity(sceneRoot);//-0.201742,0.039368,0.890312
    m_1Arm->setSource("qrc:/Excavator/yibijia.obj");
    //mLowerArm->transform()->setTranslation(QVector3D(-1000.0f,1000.0f,-100000.0f));
    cylinderMaterial1 = new Qt3DExtras::QPhongMaterial();
    cylinderMaterial1->setDiffuse(QColor(QRgb(0x928927)));
    m_1Arm->addComponent(cylinderMaterial1);

    m_2Arm = new AnimatedEntity(sceneRoot);//-0.201742,0.039368,0.890312
    m_2Arm->setSource("qrc:/Excavator/ebijia.obj");
    //mLowerArm->transform()->setTranslation(QVector3D(-1000.0f,1000.0f,-100000.0f));
    cylinderMaterial2 = new Qt3DExtras::QPhongMaterial();
    cylinderMaterial2->setDiffuse(QColor(QRgb(0x928927)));
    m_2Arm->addComponent(cylinderMaterial2);


    m_3Arm = new AnimatedEntity(sceneRoot);//-0.2011,1.03828,4.32464
    m_3Arm->setSource("qrc:/Excavator/sanbijia.obj");
    cylinderMaterial3 = new Qt3DExtras::QPhongMaterial();
    cylinderMaterial3->setDiffuse(QColor(QRgb(0x928927)));
    m_3Arm->addComponent(cylinderMaterial3);

    m_4Arm = new AnimatedEntity(sceneRoot);//-0.2011,1.03828,4.32464
    m_4Arm->setSource("qrc:/Excavator/sibijia.obj");
    cylinderMaterial4 = new Qt3DExtras::QPhongMaterial();
    cylinderMaterial4->setDiffuse(QColor(QRgb(0x928927)));
    m_4Arm->addComponent(cylinderMaterial4);

    m_5Arm = new AnimatedEntity(sceneRoot);//-0.2011,1.03828,4.32464  D:\VS2019Project\Excavator
    m_5Arm->setSource("file:///D:/VS2019Project/Excavator/wubijia.obj");
    cylinderMaterial5 = new Qt3DExtras::QPhongMaterial();
    cylinderMaterial5->setDiffuse(QColor(QRgb(0x928927)));
    m_5Arm->addComponent(cylinderMaterial5);

    mWrist = new AnimatedEntity(sceneRoot);//-0.20505,-0.23322,3.55813
    mWrist->setSource("");
    mWrist->transform()->setTranslation(QVector3D(-0.17f, 0.18f, 2.3441f));

    mSteamship = new AnimatedEntity(sceneRoot);
    mSteamship->setSource("");
    //mSteamship->transform()->setScale(1.2f);
    mSteamship->transform()->setRotationY(90);
    //mSteamship->transform()->setTranslation(QVector3D(0.0f,-0.58f,-3.2f));
    mSteamship->transform()->setTranslation(QVector3D(0.0f, -0.38f, -2.7f));

    //mSteamship->setNormalTextureSource("qrc:/new/pictrue/image/steamship.jpg");
    //mSteamship->setDiffuseTextureSource("qrc:/new/pictrue/image/steamship.jpg");
    //mSteamship->setSpecularTextureSource("qrc:/new/pictrue/image/steamship.jpg");

    initializeSphere(sceneRoot);

    //drawLine(QVector3D(1.17f,-4.18f,4.3441f),QVector3D(1.17f,-3.18f,4.3441f),QColor(255,0,0),sceneRoot);

    lText = new Qt3DExtras::QText2DEntity(sceneRoot);
    lText->setFont(QFont("Microsoft YaHei", 3, QFont::Bold));
    lText->setHeight(12);
    lText->setWidth(20);
    lText->setText("12");
    lText->setColor(Qt::red);
    textTransformL = new Qt3DCore::QTransform(lText);
    textTransformL->setTranslation(QVector3D(0.0f, -2.18 - 1.5f, 1.5f));
    textTransformL->setRotationY(90);
    textTransformL->setScale(0.125f);
    lText->addComponent(textTransformL);


    hText = new Qt3DExtras::QText2DEntity(sceneRoot);
    hText->setFont(QFont("Microsoft YaHei", 3, QFont::Bold));
    hText->setHeight(12);
    hText->setWidth(20);
    hText->setText("26");
    hText->setColor(Qt::blue);
    textTransformH = new Qt3DCore::QTransform(lText);
    textTransformH->setTranslation(QVector3D(0.0f, level - 1.5, 2.2f));
    textTransformH->setRotationY(90);
    textTransformH->setScale(0.125f);
    hText->addComponent(textTransformH);

    QWidget* container = QWidget::createWindowContainer(view, this);
    container->setFixedSize(1920, 1080);
    container->setFocusPolicy(Qt::NoFocus);
    ui->horizontalLayout_5->insertWidget(0, container);
    ui->horizontalLayout_5->setStretch(0, 5);
    ui->horizontalLayout_5->setStretch(1, 1);

    setWristTranslation(-1.22f);
    set5ArmTranslation(1.0f);
    set4ArmTranslation(-1.0f);
    set3ArmTranslation(-1.0f);
    set2ArmTranslation(0.15f);
    set1ArmTranslation(0.15f);

    connect(mWrist->transform(), &Qt3DCore::QTransform::matrixChanged, this, &Excavator::wristMatrixChanged);


    turretAnimation = new QPropertyAnimation(ui->horizontalSlider_4, "value");
    turretAnimation->setDuration(10000);
    turretAnimation->setStartValue(0);
    turretAnimation->setEndValue(255);

    lowerArmAnimation = new QPropertyAnimation(ui->horizontalSlider_3, "value");
    lowerArmAnimation->setDuration(10000);
    lowerArmAnimation->setStartValue(15);
    lowerArmAnimation->setEndValue(67);

    upperArmAnimation = new QPropertyAnimation(ui->horizontalSlider_2, "value");
    upperArmAnimation->setDuration(10000);
    upperArmAnimation->setStartValue(-171);
    upperArmAnimation->setEndValue(-129);

    wristAnimation = new QPropertyAnimation(ui->horizontalSlider, "value");
    wristAnimation->setDuration(10000);
    wristAnimation->setStartValue(-122);
    wristAnimation->setEndValue(122);
    wristAnimation->setLoopCount(1);

    ui->horizontalSlider->setValue(-122);
    ui->horizontalSlider_2->setValue(-171);
    ui->horizontalSlider_3->setValue(15);
    ui->horizontalSlider_4->setValue(0);

    //    QTimer::singleShot(5000,this,SLOT(startAnimation()));

    connect(basicCamera, &Qt3DRender::QCamera::positionChanged, this, &Excavator::cameraPositionChanged);
    connect(lowerArmAnimation, &QPropertyAnimation::valueChanged, this, &Excavator::lowerArmReach);
    connect(turretAnimation, &QPropertyAnimation::valueChanged, this, &Excavator::turretReach);
    connect(lowerArmAnimation, &QPropertyAnimation::valueChanged, this, &Excavator::lowerArmReach);
    connect(ui->horizontalSlider, &QSlider::valueChanged, this, &Excavator::on_horizontalSlider_valueChanged);
    connect(ui->horizontalSlider_2, &QSlider::valueChanged, this, &Excavator::on_horizontalSlider_2_valueChanged);
    connect(ui->horizontalSlider_3, &QSlider::valueChanged, this, &Excavator::on_horizontalSlider_3_valueChanged);
    connect(ui->horizontalSlider_4, &QSlider::valueChanged, this, &Excavator::on_horizontalSlider_4_valueChanged);
    connect(ui->horizontalSlider_5, &QSlider::valueChanged, this, &Excavator::on_horizontalSlider_5_valueChanged);
}


Excavator::~Excavator()
{
    delete ui;
}

void Excavator::initializeLight(Qt3DCore::QEntity* sceneRoot)
{
    Qt3DCore::QEntity* lightEntity = new Qt3DCore::QEntity(sceneRoot);
    mDirectionalLight = new Qt3DRender::QDirectionalLight(lightEntity);
    mDirectionalLight->setColor("white");
    mDirectionalLight->setIntensity(1.0);

    mDirectionalLight->setWorldDirection(QVector3D(6, -4, -1).normalized());
    lightEntity->addComponent(mDirectionalLight);

    mDirectionalLight2 = new Qt3DRender::QDirectionalLight(lightEntity);
    mDirectionalLight2->setColor("white");
    mDirectionalLight2->setIntensity(0.3f);
    mDirectionalLight2->setWorldDirection(QVector3D(-33, -16, -12).normalized());
    lightEntity->addComponent(mDirectionalLight2);

    mDirectionalLight3 = new Qt3DRender::QDirectionalLight(lightEntity);
    mDirectionalLight3->setColor(QColor(234, 252, 171));
    mDirectionalLight3->setIntensity(0.4f);
    mDirectionalLight3->setWorldDirection(QVector3D(-12, 0, 0).normalized());
    lightEntity->addComponent(mDirectionalLight3);

    mPointLight = new Qt3DRender::QPointLight(lightEntity);
    mPointLight->setColor("white");//设置白光
    mPointLight->setIntensity(1.0f);//设置光的强度
    mPointLight->setLinearAttenuation(1.0f);
    mPointLight->setConstantAttenuation(1.0f);
    mPointLight->setQuadraticAttenuation(0.0025f);
    mPointLight->setColor("white");
    lightEntity->addComponent(mPointLight);

}

void Excavator::initializeSphere(Qt3DCore::QEntity* sceneRoot)
{
    Qt3DExtras::QSphereMesh* sphereMesh = new Qt3DExtras::QSphereMesh();
    sphereMesh->setRings(8);
    sphereMesh->setSlices(8);
    sphereMesh->setRadius(0.1f);
    sphereTransform = new Qt3DCore::QTransform();
    sphereTransform->setTranslation(QVector3D(-0.17f, 0.98f, 5.4441f));
    Qt3DExtras::QPhongMaterial* sphereMaterial = new Qt3DExtras::QPhongMaterial();
    sphereMaterial->setDiffuse(QColor(QRgb(0xa69929)));

    // Sphere
    m_sphereEntity = new Qt3DCore::QEntity(sceneRoot);
    m_sphereEntity->addComponent(sphereMesh);
    m_sphereEntity->addComponent(sphereMaterial);
    m_sphereEntity->addComponent(sphereTransform);
    m_sphereEntity->setEnabled(false);
}


void Excavator::cameraPositionChanged(const QVector3D& position)
{
    Q_UNUSED(position)
        //t->setTranslation(position);
}

void Excavator::setWristTranslation(float angle)
{
    wristAngle = angle;
    QMatrix4x4 m(1, 0, 0, 0,
        0, qCos(angle), -qSin(angle), -1.3595f,
        0, qSin(angle), qCos(angle), -0.29633f,
        0, 0, 0, 1);
    QMatrix4x4 wristMatrix = m_3Arm->transform()->matrix() * m;

    QMatrix4x4 n(1, 0, 0, 0,
        0, qCos(angle), -qSin(angle), -0.145708f,
        0, qSin(angle), qCos(angle), -0.930605f,
        0, 0, 0, 1);

    QMatrix4x4 mSphere = wristMatrix * n;
    sphereTransform->setMatrix(mSphere);
    mWrist->transform()->setMatrix(wristMatrix);
}

void Excavator::set4ArmTranslation(float angle)
{
    //angle = 45;
    // 构建旋转矩阵
    /*QMatrix4x4 rotationMatrix;
    rotationMatrix.setToIdentity();
    rotationMatrix.rotate(angle, QVector3D(0.0f, 0.0f, 1.0f)); // 绕z轴旋转

    // 构建平移矩阵
    QMatrix4x4 translationMatrix;
    translationMatrix.setToIdentity();
    translationMatrix.translate(40.0f, 235.0f, 100.0f); // 平移到指定位置
    //translationMatrix.translate(1, 1, -1);

    // 构建缩放矩阵
    QMatrix4x4 scaleMatrix;
    scaleMatrix.setToIdentity();
    scaleMatrix.scale(1);

    // 组合所有变换
    QMatrix4x4 upperArmMatrix = m_3Arm->transform()->matrix() * translationMatrix * rotationMatrix * scaleMatrix;

    // 应用变换
    m_4Arm->transform()->setMatrix(upperArmMatrix);
    set5ArmTranslation(0);*/
    angle = angle / 360 * 2 * M_PI;
    QMatrix4x4 m(
        qCos(angle), -qSin(angle), 0, 80.0f,
        qSin(angle), qCos(angle), 0, 285.0f,
        0, 0, 1, 100.0f,
        0, 0, 0, 1
    );


    QMatrix4x4 upperArmMatrix = m_3Arm->transform()->matrix() * m;
    m_4Arm->transform()->setMatrix(upperArmMatrix);
    set5ArmTranslation(0);
}

void Excavator::set5ArmTranslation(float angle)
{
    //angle = 0;
    // 构建旋转矩阵
    QMatrix4x4 rotationMatrix;
    rotationMatrix.setToIdentity();
    rotationMatrix.rotate(angle, QVector3D(0.0f, 0.0f, 1.0f)); // 绕z轴旋转

    // 构建平移矩阵
    QMatrix4x4 translationMatrix;
    translationMatrix.setToIdentity();
    translationMatrix.translate(1100.0f, -1050, -100); // 平移到指定位置
    //translationMatrix.translate(1, 1, -1);

    // 构建缩放矩阵
    QMatrix4x4 scaleMatrix;
    scaleMatrix.setToIdentity();
    scaleMatrix.scale(1);

    // 组合所有变换
    QMatrix4x4 upperArmMatrix = m_4Arm->transform()->matrix() * translationMatrix * rotationMatrix * scaleMatrix;

    // 应用变换
    m_5Arm->transform()->setMatrix(upperArmMatrix);
}

void Excavator::set3ArmTranslation(float value)
{
    float angle = 0;
    // 构建旋转矩阵
    QMatrix4x4 rotationMatrix;
    rotationMatrix.setToIdentity();
    rotationMatrix.rotate(angle, QVector3D(0.0f, 0.0f, 1.0f)); // 绕X轴旋转
    //rotationMatrix.rotate(angle, QVector3D(0.0f, 1.0f, 0.0f)); // 绕Y轴旋转
    //rotationMatrix.rotate(angle, QVector3D(0.0f, 0.0f, 1.0f)); // 绕Z轴旋转

    // 构建平移矩阵
    QMatrix4x4 translationMatrix;
    translationMatrix.setToIdentity();
    translationMatrix.translate(-2000.0f + value, 30.0f, 0.0f); // 平移到指定位置
    //translationMatrix.translate(1, 1, -1);

    // 构建缩放矩阵
    QMatrix4x4 scaleMatrix;
    scaleMatrix.setToIdentity();
    scaleMatrix.scale(1);

    // 组合所有变换
    QMatrix4x4 upperArmMatrix = m_2Arm->transform()->matrix() * translationMatrix * rotationMatrix * scaleMatrix;

    // 应用变换
    m_3Arm->transform()->setMatrix(upperArmMatrix);
    set4ArmTranslation(angle);
}
void Excavator::set2ArmTranslation(float value)
{
    upperArmAngle = 0;
    float angle = -0;
    // 构建旋转矩阵
    QMatrix4x4 rotationMatrix;
    rotationMatrix.setToIdentity();
    rotationMatrix.rotate(angle, QVector3D(1.0f, 0.0f, 0.0f)); // 绕X轴旋转

    // 构建平移矩阵
    QMatrix4x4 translationMatrix;
    translationMatrix.setToIdentity();
    translationMatrix.translate(-100.0f - value, 15.0f, -1101.0f); // 平移到指定位置
    //translationMatrix.translate(1, 1, -1);

    // 构建缩放矩阵
    QMatrix4x4 scaleMatrix;
    scaleMatrix.setToIdentity();
    scaleMatrix.scale(1);

    // 组合所有变换
    QMatrix4x4 lowArmMatrix = m_1Arm->transform()->matrix() * translationMatrix * rotationMatrix * scaleMatrix;

    // 应用变换
    m_2Arm->transform()->setMatrix(lowArmMatrix);
    set3ArmTranslation(upperArmAngle);
}

void Excavator::set1ArmTranslation(float angle)
{
    angle = -0;
    // 构建旋转矩阵
    QMatrix4x4 rotationMatrix;
    rotationMatrix.setToIdentity();
    rotationMatrix.rotate(angle, QVector3D(1.0f, 0.0f, 0.0f)); // 绕X轴旋转
    //rotationMatrix.rotate(angle, QVector3D(0.0f, 1.0f, 0.0f)); // 绕Y轴旋转
    //rotationMatrix.rotate(angle, QVector3D(0.0f, 0.0f, 1.0f)); // 绕Z轴旋转

    // 构建平移矩阵
    QMatrix4x4 translationMatrix;
    translationMatrix.setToIdentity();
    translationMatrix.translate(10.0f, 490.0, 833.0f); // 平移到指定位置
    //translationMatrix.translate(1, 1, -1);

    // 构建缩放矩阵
    QMatrix4x4 scaleMatrix;
    scaleMatrix.setToIdentity();
    scaleMatrix.scale(1);

    // 组合所有变换
    QMatrix4x4 lowArmMatrix = m_diPan->transform()->matrix() * translationMatrix * rotationMatrix * scaleMatrix;

    // 应用变换
    m_1Arm->transform()->setMatrix(lowArmMatrix);
    set2ArmTranslation(upperArmAngle);
}

void Excavator::setTurretTranslation(float angle)
{

    //    QMatrix4x4 m(qCos(angle),   0,  qSin(angle),    0,
    //                 0,             1,  0,              0,
    //                 -qSin(angle),  0,  qCos(angle),    0,
    //                 0,             0,  0,              1);
    //    QMatrix4x4 turretMatrix = mBase->transform()->matrix()*m;
    //    mTurret->transform()->setMatrix(turretMatrix);
    //    setLowerArmTranslation(lowerArmAngle);

}

void Excavator::startSteamshipAnimation()
{

}


void Excavator::startAnimation()
{
    wristAnimation->setStartValue(-122);
    wristAnimation->setEndValue(122);

    lowerArmAnimation->start();
    upperArmAnimation->start();
    wristAnimation->start();
}


void Excavator::lowerArmReach(const QVariant& value)
{
    //    float lowerArmAngle = value.toFloat();
    //    if(lowerArmAngle >= 27){
    //        turretAnimation->start();
    //        wristAnimation->stop();
    //    }
    //    if(lowerArmAngle <= 15){
    //        turretAnimation->stop();
    //    }
    float turretAngle = value.toFloat();
    if (turretAngle >= 255) {
        wristAnimation->setStartValue(122);
        wristAnimation->setEndValue(-122);
        wristAnimation->start();
    }
}


void Excavator::turretReach(const QVariant& value)
{
    float turretAngle = value.toFloat();
    if (turretAngle >= 255) {
        wristAnimation->setStartValue(122);
        wristAnimation->setEndValue(-122);
        wristAnimation->start();
    }
}

void Excavator::on_horizontalSlider_2_valueChanged(int value)
{
    set2ArmTranslation(value / 100.f * 100.f);
}

void Excavator::on_horizontalSlider_3_valueChanged(int value)
{
    set3ArmTranslation(value / 100.f * 100.f);

    //qDebug()<<"=========="<<mWrist->transform()->translation()<<"--------"<<mTurret->transform()->translation();

}

void Excavator::wristMatrixChanged()
{
    float length = sphereTransform->translation().z();
    QVector3D v(vectL.x(), vectL.y(), vectL.z() - 1.5f + length / 2.0f);//+0.968774f
    //lineL->setLength(length - 0.65f);
    //lineL->setPosition(v);
    lText->setText(QString::number(length, 'f', 2));
    textTransformL->setTranslation(QVector3D(vectL.x(), vectL.y() - 1.5f, vectL.z() - 1.5f + length / 2.0f));


    float y = sphereTransform->translation().y();
    float lineHlength = qAbs(qAbs(level) + qAbs(y) - 0.5);

    //lineH->setLength(lineHlength);

    QVector3D vH(0.0f, level + lineHlength / 2.0f, 2.6f);
    textTransformH->setTranslation(QVector3D(0.0f, level + y / 2.0f - 1.5f, sphereTransform->translation().z()));

    //lineH->setPosition(QVector3D(0.0f, level + y / 2.0f, sphereTransform->translation().z()));
    if (y + level > 0) {
        hText->setText(QString::number(lineHlength, 'f', 2));
    }
    else {
        hText->setText(QString::number(-lineHlength, 'f', 2));
    }


}

void Excavator::on_horizontalSlider_4_valueChanged(int value)
{
    set4ArmTranslation(value / 100.0f * 100.0f);
}

void Excavator::on_horizontalSlider_5_valueChanged(int value)
{
    set5ArmTranslation(value / 100.0f * 100.0f);
}

void Excavator::on_horizontalSlider_valueChanged(int value)
{
    m_diPan->transform()->setTranslation(QVector3D(-value / 100.0f * 100.0f, 0.0f, -0.0f));
    set1ArmTranslation(0);
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值