How do I rotate a mesh around specified axis?



  • I have several obj mesh files that I am importing into a Scente3D view and I am trying to animate them. I need to move them into correct position relative to other objects and for example rotate them around its own center (not the 0,0,0 coordinate). For example here is my translate code:

                Transform {
                    id: centerFeedWheelTransform
                    property real userAngle: 0.0
                    matrix: {
                        var m = Qt.matrix4x4();
                        m.rotate(userAngle, Qt.vector3d(0, 0, 1))
                        m.translate(Qt.vector3d(8.66, 14.28, -18.04));
    
                        //m.rotateAround(Qt.vector3d(20,30,40),userAngle, Qt.vector3d(0, 0, 1));  // this did not work
                        return m;
                    }
                }
    

    The problem with the code above is that when object is translated it's origin is not carried with it, so it turns around the scene (0,0,0) coordinate.

    Any suggestions how I could move entities around in the scene and rotate them around their own independent origin? Ideally I like to set the origin for rotation to center of mass, but even if I can manually enter a vector, at least I can get by.


  • Qt Champions 2016

    Hi,
    Translate and scale matrices don't commute. To rotate around the object's own axis you need to switch to the object's local coordinate system. This ultimately means you need to do a translation, make the desired rotation and then reverse the translation. Suppose you have an object that is located at: Qt.vector3d(10, 10, 4), you'd do something like this:

    m.translate(Qt.vector3d(-10, -10, -4)); //< Move the object to the coordinate system's origin
    m.rotate(userAngle, Qt.vector3d(0, 0, 1)); //< Rotate around the z-axis with your angle.
    m.translate(Qt.vector3d(10, 10, 4)); //< Restore the object's original position
    

    Kind regards.



  • Thanks for your help!
    Is there a way to get the current location of my entitiy in code so I do not have to manually figure that out every time? That would make my life much easier since there are lots and lots of moving parts in my scene.

    As an example, here is how I load the mesh file and create and entity with it:

                Mesh {
                    id: centerFeedWheelMesh
                    source: "../../resources/CenterFeedWheel2.obj"
                } 
    
                        Entity {
                             id: centerFeedWheel
                             components: [centerFeedWheelMesh, centerFeedWheelTransform]
                         }
    

    I can not find any way to do this in the docs. Please tell me there is a way to get the current 3D location of entity. Yes?


  • Qt Champions 2016

    @Aras
    The current location of a 3D entity is simply it's applied transformation's translation. There's no special property for this, you can get it from the Transform object (if you've applied any).



  • @kshegunov that is not true! I can load multiple .obj files and they will appear at different locations on the scene without me applying any transformation to them. Their coordinate relative to the origin is taken into account when importing the objects. For example, depending on where your object was in the blender world, it will appear in different location when imported into the scene.

    That behavior is useful because you can arrange parts of a model in blender and then export them to obj. Now when you load them in your 3D scene they will be in correct position relative to each other. My problem now is that I need to know what their position vectors are so than I could use the trick you mentioned to rotate them around a custom rotation axis.


  • Qt Champions 2016

    @Aras
    I don't know, sorry. My early-dev experiment with Qt3D was almost half an year ago and I just bound the meshes to entities, which I could manipulate. I didn't load scenes.



  • @kshegunov no worries, you suggestion help me find a workaround for now. Here is how I did it, in case anyone else runs into the same problem:

    1. Inside blender I place the 3D cursor to where I want to rotate the item around and set that as the origin of my object

    2. Write down the (x,y,z) coordinates of my object in blender properties view

    3. Export selected object as .obj file

    4. Import mesh in qt

                        Mesh {
                            id: centerFeedMountMesh
                            source: "../../resources/CenterFeedMount.obj"
                        }
                        Mesh {
                            id: centerFeedWheelMesh
                            source: "../../resources/CenterFeedWheel.obj"
                        }
                        Entity {
                           id: centerFeedMount
                           components: [ centerFeedMountMesh, darkGreenMaterial]
                           Entity {
                               id: centerFeedWheel
                               components: [centerFeedWheelMesh, orangeMaterial, centerFeedWheelTransform]
                           }
                        }
    
    1. I rotate my object using the following code: (will of course be different for each object)

    Note: for some reason I had to swap Y and Z coordinates to get the correct vector. So if in blender X=10, Y=20, Z=30 then your translate vector will be Qt.vector3d(10, 30, 20)

                Transform {
                    id: centerFeedWheelTransform
                    property real userAngle: 0.0
                    matrix: {
                        var m = Qt.matrix4x4();
                        m.translate(Qt.vector3d(8.06, -18.04, 14.28));
                        m.rotate(userAngle, Qt.vector3d(0, 0, 1))
                        m.translate(Qt.vector3d(-8.06, 18.04, -14.28));
                        return m;
                    }
                }
    

    This works pretty well for me right now, so I will mark item as resolved. I still like to know if I can get the coordinate of entity in my Qt3D program. If you know how to do that, please comment!


Log in to reply
 

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