[Rotation of an object using a QList of QQuaternion doesn't work]
-
Hello again, I hope you're all doing well.
So I have a mainwidget class in which I developped a paintGL() function.
The goal of this function is to draw an object and then rotates it using quaternion values.
I have a QList of QQuaternion that I want to iterate through it and each time see the orientation of the object.
So here is what I did:
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]); }
So logically when I run my code, I should see the object changing orientation till it reaches the last one corresponding to last value of the quaternion. However that doesn't happen.
The object rotates only once and stays at that orientation instead of keeping to rotate.
It seems that the for loop isn't being executed and I don't have any reason why.
Please help me.
Any help would be so appreciated and thank you.
Also sorry for posting a lot of questions lately, I'm trying to work as hard as I can to develop my skills in c++ programming and Qt.
-
@appdev said in [Rotation of an object using a QList of QQuaternion doesn't work]:
It seems that the for loop isn't being executed and I don't have any reason why.
Please help me.You must know by now that if you think a loop is not being executed you need to put
qDebug()
statements in? What else do you expect people to say?Put one in as the first statement above the
for
, and one into the body of thefor
.If it's calling the code but not going through the loop then what reason other than
quaternion_w.size() == 0
could there possibly be? Equally if it's only going through once then presumablyquaternion_w.size() == 1
.... -
@appdev
Show me theqDebug()
statements (and any relevant output so I know they are being hit) you have put in....
You said "It seems that the for loop isn't being executed and I don't have any reason why.", let's see whether that is really the case. -
Okay;
Here is the code with qDebug() statement:
for (int i=0; i<quaternion_w.size();++i) { quaternioncsv.append(QQuaternion(quaternion_w[i],quaternion_x[i], quaternion_y[i], quaternion_z[i])); qDebug()<<"quaternioncsv"<<quaternioncsv; matrix.rotate(quaternioncsv[i]); }
and here is the output of quaternioncsv which is the QList that I want to use to rotate the object.
quaternioncsv QList(QQuaternion(scalar:-0.169757, vector:(-0.00949828, -0.550083, 0.817619)), QQuaternion(scalar:0.542742, vector:(-0.0784193, 0.663872, -0.508483)), QQuaternion(scalar:0.605322, vector:(-0.242393, 0.722369, -0.230249)), QQuaternion(scalar:0.615956, vector:(-0.269809, 0.694952, -0.254643)), QQuaternion(scalar:0.647567, vector:(-0.199047, 0.701155, -0.222303)), QQuaternion(scalar:0.680871, vector:(-0.102179, 0.707951, -0.157415)), QQuaternion(scalar:0.691001, vector:(-0.0221707, 0.71639, -0.0938709)), QQuaternion(scalar:0.705128, vector:(0.000843344, 0.706753, -0.05739)), QQuaternion(scalar:0.709317, vector:(0.0126352, 0.70401, -0.0328456)), QQuaternion(scalar:0.707146, vector:(0.0292804, 0.706193, -0.0194616)), QQuaternion(scalar:0.706209, vector:(0.0210576, 0.707072, -0.0295872)), QQuaternion(scalar:0.709335, vector:(0.00930126, 0.702975, -0.0508208)), QQuaternion(scalar:0.711758, vector:(0.000600887, 0.699882, -0.0597074)), QQuaternion(scalar:0.716376, vector:(-0.00306779, 0.69464, -0.065364)), QQuaternion(scalar:0.723806, vector:(-0.0100708, 0.686258, -0.0710871)), QQuaternion(scalar:0.718329, vector:(-0.0157465, 0.69134, -0.0761881)), QQuaternion(scalar:0.704027, vector:(-0.0112261, 0.705654, -0.0791981)), QQuaternion(scalar:0.702827, vector:(0.0169507, 0.708798, -0.057904)), QQuaternion(scalar:0.712661, vector:(0.02157, 0.699067, -0.054351)), QQuaternion(scalar:0.720286, vector:(0.0286278, 0.691334, -0.0492495)), QQuaternion(scalar:0.727202, vector:(0.0236371, 0.684334, -0.0480126)), QQuaternion(scalar:0.727093, vector:(0.0409809, 0.683973, -0.0428709)), QQuaternion(scalar:0.728412, vector:(0.0425436, 0.681845, -0.0518988)), QQuaternion(scalar:0.723284, vector:(0.042028, 0.689264, 0.00299574))
Its a QList of QQuaternion.
-
Yes exactly, the QList isn't empty.
When I run the application, I 'm supposed to see an object that changes its orientation 24 times [24 orientations that correspond to every quaternion in the QList] however it only changes its orientation one time and then remains at that position.
-
@appdev
The question was: how many times do you go through the loop?
Did you get yourqDebug()
line once or 24 times --- you didn't say.
You could at least have printedqDebug() << i<quaternion_w.size()
above thefor
line....
If that is 1 you only executematrix.rotate(quaternioncsv[i]);
once, withi == 0
.
How will that do 24 operations?Assuming this is the problem, I really don't see why you couldn't figure it with
qDebug()
statements or expect a one-time loop to do 24 operations.... -
@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.