How do I make objects in Qt3D move?



  • I have followed this example http://doc.qt.io/qt-5/qt3d-basicshapes-cpp-example.html in order to create a main window containing a Qt3Dwindow with a sphere entity in it. I use QSphereMesh to render the shape and QTransform to position it.

    Now I wold like to let a user re-position the sphere to a given coordinate by the click of a pushbutton. I tried to use signals and slots with the slot calling the same function I use to position the sphere in the first place:

    this->m_sphereTransform->setTranslation(QVector3D(10.0f, 0.0f, 0.0f));
    

    (where m_sphereTransform is a Qt3DCore::QTransform). The program crashes and I am totally lost on how to actually make my objects move.



  • You approach is good.
    Have you check if the m_sphereTransform is already defines ?



  • As far as I can tell m_sphereTransform is only defined once.
    However, my newest attempt is to initialize the transform with variables in the translation vector:

    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform();
    sphereTransform->setTranslation(QVector3D(m_x, m_y, m_z));
    

    where m_x, m_y and m_z are members of a class defining a sphere entity (with mesh, material and transform components). The class contains functions to set m_x etc

    void Sphere::setX(int x)
    {
      m_x = x;
    }
    

    when printing out m_x through the debugger it shows it has been changed. But the sphere doesn't move.
    I have tried doing the same thing in QML instead of c++ and the sphere moves without problems. I suspect the view isn't refreshed?
    In C++ I use a Qt3Dwindow:

    Qt3DExtras::Qt3DWindow *viewRoom = new Qt3DExtras::Qt3DWindow();
    

    and in QML a 3DScene:

    Scene3D {
                id: scene3d
                anchors.fill: parent
                anchors.margins: 10
                focus: true
                aspects: ["input", "logic"]
                cameraAspectRatioMode: Scene3D.AutomaticAspectRatio
    
                SphereEntity {id: mySphere}
            }
    

    Is there a difference between how the two updates what is shown on the screen?


  • Qt Champions 2016

    @Lias said in How do I make objects in Qt3D move?:

    when printing out m_x through the debugger it shows it has been changed. But the sphere doesn't move.

    And why should it. You're setting a variable that has nothing to do with the sphere's transformation.

    sphereTransform->setTranslation(QVector3D(m_x, m_y, m_z));
    

    This makes a vector from the current values of m_x, m_y, m_z by copying their values. All modifications to said members after that will not cause the sphere's transformation to change.



  • Hi,

    You should add a QEntity as a parent of you QTransform to have a good clean up at the destruction of the QEntity.

    For updating you QTransform, made it member of the class and add a method like this:

    void Sphere::updateTransform(){
        if(_sphereTransform != 0){ //prevent seq fault
         _sphereTransform->setTranslation(QVector3D(m_x, m_y, m_z));
       }
    }
    


  • @Neosw Thanks for your help. I tried this by myself two days ago and it worked!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.