Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. How do I make objects in Qt3D move?
QtWS25 Last Chance

How do I make objects in Qt3D move?

Scheduled Pinned Locked Moved Solved Game Development
qt3dqttransformmoving
6 Posts 3 Posters 5.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lias
    wrote on 9 Sept 2016, 08:26 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Neosw
      wrote on 15 Sept 2016, 10:20 last edited by Neosw
      #2

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

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Lias
        wrote on 20 Sept 2016, 06:00 last edited by
        #3

        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?

        K 1 Reply Last reply 21 Sept 2016, 11:44
        0
        • L Lias
          20 Sept 2016, 06:00

          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?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 21 Sept 2016, 11:44 last edited by
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Neosw
            wrote on 22 Sept 2016, 06:42 last edited by
            #5

            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));
               }
            }
            
            L 1 Reply Last reply 22 Sept 2016, 09:25
            0
            • N Neosw
              22 Sept 2016, 06:42

              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));
                 }
              }
              
              L Offline
              L Offline
              Lias
              wrote on 22 Sept 2016, 09:25 last edited by
              #6

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

              1 Reply Last reply
              1

              3/6

              20 Sept 2016, 06:00

              • Login

              • Login or register to search.
              3 out of 6
              • First post
                3/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved