[Rotation of an object using a QList of QQuaternion doesn't work]
-
@jsulm I can do one rotation like this :
quaternion = QQuaternion(w, x, y, z); matrix.rotate(quaternion)
where w, x, y and z are the quaternion values that I specify there.
and yes exactly I want to do an animation.
@JonB Okay, I did that :
qDebug() << i<quaternion_w.size()
I get 24 which is the size of quaternion_w. and also 24 lines and not just one line.
Well I thought that a loop will fill the QList of QQuaternion and at the same time executes the rotation of the object.
For example it will fill in the first field of the QList and then executes the first rotation, then fill in the second field and executes the second rotation ..etc.. so that I can have an animation by the end. -
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
I get 24 which is the size of quaternion_w. and also 24 lines and not just one line.
OK, fair enough, that is really what I wanted to know. Sorry if I suspected that was not the case in your situation.
For example it will fill in the first field of the QList and then executes the first rotation, then fill in the second field and executes the second rotation ..etc.. so that I can have an animation by the end.
That I have no idea about --- wouldn't know what a "quaternion` was if it slapped me in the face!
But you have to admit that your original
It seems that the for loop isn't being executed and I don't have any reason why.
was not the case, given the
qDebug()
statements. -
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
and yes exactly I want to do an animation.
But how does this correlate to 'matrix' at all? Where do you use it? Currently you simply add all quaternions and then do nothing...
-
@Christian-Ehrlicher matrix is the modelview matrix.
When I use rotate function, it multiplies the modelview matrix with the matrix that corresponds to the quaternion.What's confusing is that when I run the application, the object does change its orientation but only one time.
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
its orientation but only one time.
When you don't change quaternion_w - what should change?
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
quaternioncsv is the QList that contains values of QQuaternion and that I will be using to rotate my object.
But where? I only see you iterate over a list and modify a variable, nothing more...
-
Okay, here is the code:
for (int i=0; i<quaternion_w.size();++i) { quaternioncsv.append(QQuaternion(quaternion_w[i],quaternion_x[i], quaternion_y[i], quaternion_z[i])); matrix.rotate(quaternioncsv[i]); // the object should rotate }
So first thing first I fill the QList "quaternioncsv" with values from other QLists then I use the rotate function that multiplies the modelview matrix by the rotation matrix that corresponds to every quaternion of the QList " quaternioncsv".
The rotate function executes the rotation.
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
matrix.rotate(quaternioncsv[i]); // the object should rotate
there is no object, just a matrix and you don't paint anything after you set the matrix to a new value...
-
The object is represented by that matrix which is the modelview matrix.
this works perfectly:
quaternion = QQuaternion(0.8536, 0.3536, -0.1464, 0.3536); matrix.rotate(quaternion)
I tried this with different quaternion values and the object does change its orientation.
If I'm not painting anything after setting the matrix to a new value, how come the code lines I wrote above, make the object rotate. I'm confused.
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
make the object rotate. I'm confused.
Because then you enter the event loop again, a paintEvent is triggered and your stuff is painted...
Use a QTimer, set the desired timeout and advance your rotation to the next state in the connected slot.
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
shouldn't I use something like a loop to make it advance ?
Remember the current value, add one to get to the next and use this during the next execution of your slot. How else should it work?
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
but advancing the rotation to the next state isn't really clear ?
In each timeout slot call you take next quaternion from your list and do the rotation.
-
I tried something but it didn't work:
I added a new function to my code: it's actually a SLOTvoid MainWidget::rotate_csv(QMatrix4x4 m) { for (int i=0; i<quaternioncsv.size();++i) { m.rotate(quaternioncsv[i]); } }
It iterates through the QList of quaternions and multiplies the matrix "m" with the rotation matrix of quaternioncsv[i].
Then in my paintGL() function I did this:
this->m_timer=new QTimer(this); m_timer->setInterval(10000); QTimer::connect(m_timer,SIGNAL(timeout()),this,SLOT(rotate_csv(matrix))); m_timer->start();
So according to Qt documentation, the rotate_csv will be called every 10 seconds.
When I run my application, the object remains at its initial orientation. It doesn't start rotating. -
@appdev
First,SLOT(rotate_csv(matrix))
isn't even legal. Why you cannot do yourself a favor and change over to New Signal Slot Syntax (unless you are on Qt4, which you do not say), which would catch this, is beyond me.... If you really won't change over, why don't you test the result returned fromconnect()
?When I run my application, the object remains at its initial orientation. It doesn't start rotating.
Did you put a
qDebug()
statement insideMainWidget::rotate_csv()
to see how many times it was called? I have advised you to do this....@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
But an iteration needs a loop to iterate through the whole QList so I need to use a for loop right ?
No, this is not what the others are asking you to do. They are asking you to perform one quaternion in the list per each timeout. No
for
loop!They are asking you to write something like:
connect(m_timer, &QTimer::timeout, this, &MainWidget::rotate_csv); m_iterations = 0; m_timer->start(); void MainWidget::rotate_csv() { if (m_iterations >= quaternioncsv.count()) return; matrix.rotate(quaternioncsv[m_iterations]); m_iterations++; }
-
You have several wrong expectations. Let's take a step back and look why your original code does not work.
-
The main thing (and most likely also why it still does not work) is that you assume that changing the model view matrix will also change what you see on the screen. However, you never instruct the computer to redraw what you see on the screen. You should call
update()
on the widget that draws the rotated object. -
While you are inside the loop nothing can be drawn (at least as long as you are inside the same thread). This is why others have suggested to use a timer instead. You should create one timer that is started in the constructor already. You wrote that you create a new timer in
paintGL()
. This would mean that whenever you repaint your widget a new timer is added. The timers are set to repeat every 10 seconds (that is whatsetInterval()
is for). You will end up with too many timers. Just create one repeating timer in the constructor. -
Let's suppose your original code magically also updated what you see on screen. Computers are fast! Really fast! The loop will take only a bunch of milliseconds to run. At 60 Hz one single frame will take about 16 ms. Your loop would have finished once the next frame can be displayed. Anyway your brain would only be fast enough to perceive a single rotation even if the hardware would run much much faster than at 60 fps.
-
Successively applying rotations: Your code snippet is too short to see the declaration of your model view matrix. The easiest way would be to have it as a member variable. This would mean that the next time you paint you already know the last rotation. Then, you only need to apply the next rotation, i.e. a single rotation from your list. This goes along with @JonB's suggestion.
-