Quaternion Object Rotation

Started by
13 comments, last by GarrickW 12 years, 1 month ago
What part of that code is initialization that you do once and what part is something you do in each iteration of the loop? Or do you do all of that every time?
Advertisement

What part of that code is initialization that you do once and what part is something you do in each iteration of the loop? Or do you do all of that every time?


The input code above is performed every time, assuming a certain flag is true. Rotation is then passed to the entity that is supposed to be rotating. Is that bad?

EDIT: Just to be sure, I changed the code such that it is executed regardless of the flag, and the same problem occurs.
I would remove the input and pretend the user is pressing "Down" constantly. Then either debug the program step by step and see if variables have the values you expect, or add logging to your code so you can see what it's doing. The advantage of doing it with logging is that you can then post the log here if you need help.
All right, I'll do some more intensive debugging and get back with the results.
Found it!

It turns out my implementation of quaternions wasn't correct after all. The code for quaternion multiplication I had was either wrong, or I copied it wrong. Either way, fixing it by referring to this site helped: http://www.arcsynthesis.org/gltut/Positioning/Tut08%20Quaternions.html

For the record, the correct quaternion multiplication code I now use is:

Quaternion Quaternion::operator* (Quaternion OtherQuat)
{
float A = (OtherQuat.a * a) - (OtherQuat.x * x) - (OtherQuat.y * y) - (OtherQuat.z * z);
float X = (OtherQuat.a * x) + (OtherQuat.x * a) + (OtherQuat.y * z) - (OtherQuat.z * y);
float Y = (OtherQuat.a * y) + (OtherQuat.y * a) + (OtherQuat.z * x) - (OtherQuat.x * z);
float Z = (OtherQuat.a * z) + (OtherQuat.z * a) + (OtherQuat.x * y) - (OtherQuat.y * x);
Quaternion NewQuat = Quaternion();
NewQuat.a = A;
NewQuat.x = X;
NewQuat.y = Y;
NewQuat.z = Z;
return NewQuat;
}


Many thanks to you, alvaro! I otherwise would have just assumed this was beyond my capabilities.

The object now rotates easily along all its local axes, no matter what previous rotations took place. Awesome!

This topic is closed to new replies.

Advertisement