OpenGL Quaternions and a rotating ball

This topic is 4707 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey, I'm having some problems to get a ball rotating. I have a ball moving along the XZ-plane which I want to get rotating nicely. This leads me to what seems to be one of the most usual problems regarding rotating (glrotatef since I use opengl): it's kinda hard to rotate something around two world-axis (non object-local). I've read about the quaternions on a few sites and finally decided to do as in the NeHe-tutorial (http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=Quaternion_Camera_Class) which seems ideal for this. What I do is that I calculate how far the ball has moved in the Z and X-direction and translates that to how many degrees the ball have rotated. I keep track of these angles of rotation about the absolute X and Z-axis. Since two rotations won't do it I create to quaternions, one for each axis:
GLfloat Matrix[16];
glQuaternion result, xaxis, zaxis;
xaxis.CreateFromAxisAngle(1.0, 0.0, 0.0, player->getRotationX());
zaxis.CreateFromAxisAngle(0.0, 0.0, 1.0, player->getRotationZ());
result = xaxis*zaxis;
result.CreateMatrix(Matrix);
glMultMatrixf(Matrix);
But I still get odd rotations. For example, when the rotation around the X-axis is 90 degrees and I move along the X-axis the ball rotates around the Y-axis, which is exactly what happens when you use glrotatef twice (since the Z-axis points downwards after the rotation around the X-axis). I've been trying to get these silly quaternions to work the last 4 hrs and I'm starting to get kinda irritated on them :) Does anyone have any ideas?

Share on other sites
Quote:
 Original post by noofGLfloat Matrix[16];glQuaternion result, xaxis, zaxis;xaxis.CreateFromAxisAngle(1.0, 0.0, 0.0, player->getRotationX());zaxis.CreateFromAxisAngle(0.0, 0.0, 1.0, player->getRotationZ());result = xaxis*zaxis;result.CreateMatrix(Matrix);glMultMatrixf(Matrix);But I still get odd rotations. For example, when the rotation around the X-axis is 90 degrees and I move along the X-axis the ball rotates around the Y-axis, which is exactly what happens when you use glrotatef twice (since the Z-axis points downwards after the rotation around the X-axis). I've been trying to get these silly quaternions to work the last 4 hrs and I'm starting to get kinda irritated on them :)
Using quaternions is gaining you absolutely nothing here. In short, if one can't make something work with matrices, they probably won't be able to get it to work with quaternions; the latter has some advantages over the former, but the fundamental properties of the two representations are the same.

So I'd drop the quaternions for now. As for getting the ball to roll in a somewhat realistic way, here's what occurs to me (short of an actual physics-based simulation). The axis of the ball's rotation is perpendicular to a) the ball's direction of motion, and b) the normal of the surface on which it is moving. In this case it is always uniquely defined, so you can just take the normalized cross product of the velocity and up vectors. Then, you can find the amount of rotation from the ball's circumference and the distance it's traveled. You can acumulate this angle over time and then feed the axis-angle pair to glRotate().

There might be other issues involved if your ball changes direction. This is a place where your quat class may come in handy (not because it has any special properties with respect to matrices, but rather just because you already have it available).

Share on other sites
Making a quaternion from euler angles, like you're doing there, is sort of like making a fancy decorated bavarian custard from spoiled milk. It's still going to taste bad. Use quaternions (or matrices) to keep track of the orientation of the ball. Don't use the "xaxis, zaxis" thing. Don't use euler angles anywhere.

Share on other sites
Quote:
 Original post by noofGLfloat Matrix[16];glQuaternion result, xaxis, zaxis;xaxis.CreateFromAxisAngle(1.0, 0.0, 0.0, player->getRotationX());zaxis.CreateFromAxisAngle(0.0, 0.0, 1.0, player->getRotationZ());result = xaxis*zaxis;result.CreateMatrix(Matrix);glMultMatrixf(Matrix);

Just to chime in, my first reaction to this code was "wow, this is incredibly... pointless". You probably fell for the silly "quaternions are the magical solution to all your rotation problems" and decided to just throw some in without being explained or looking up what they are, how they work and why they work.

So you create two quaternions, which are doing exactly the same thing as matrices would have and end up with a completely wasteful way to do:
glRotate(a,1,0,0);
glRotate(b,0,0,1);
just that now there is a lot of useless back and forth between all possible representations.

As pointed out above, your problem isn't using quaterions or converting Euler angles to quaternions, but the fact that you are using Euler angles at all. They are extremely useless for just about everything that isn't a typical shooter style first person camera. NEVER try and store an orientation as Euler angles, there is just no useful way to add additional rotations, unless you start converting them (ALL angles) to a (SINGLE) matrix/quaternion, apply the new rotation to that and convert back. And at this point you should realize that storing a matrix or quaternion instead of constantly converting from/to a useless representation is saving you a lot of work and trouble.

Share on other sites
We all have to start somewhere :)

Quote:
 Original post by jykThen, you can find the amount of rotation from the ball's circumference and the distance it's traveled. You can acumulate this angle over time and then feed the axis-angle pair to glRotate().

I actually tried that one before I found out about quaternion which makes it kinda hard to accumulate the rotation (unless you use matrices, which I didn't think of)

Quote:
 Original post by TriencoYou probably fell for the silly "quaternions are the magical solution to all your rotation problems" and decided to just throw some in without being explained or looking up what they are, how they work and why they work.

100% correct, although I started realizing that quaternions aren't as magic as I thought when I thought about them and couldn't grasp why they would solve my problem (just thought that was part of the other silly "quaternions are so hard to understand that you don't wanna waste your time doing it")

So, after reading your thoughts (thanks a lot for them btw!) I'm thinking of the following solution:
1) cross velocity vector with up-vector (plane normal) to get the vector the ball is spinning around
2) get number of degrees the ball has rotatet around that vector
3) convert that spin to a quaternion like:
glQuaternion rot;rot.CreateFromAxisAngle(degrees, spinvector.x, spinvector.y, spinvector.z);

4) "add" that rotation to the rotation accumulator:
total = total*rot;

5) rotate the ball:
GLfloat Matrix[16];total .CreateMatrix(Matrix);glMultMatrixf(Matrix);

Does this sound any better? :)

[Edited by - noof on January 20, 2006 4:54:11 AM]

Share on other sites
Quote:
 Original post by noof(just thought that was part of the other silly "quaternions are so hard to understand that you don't wanna waste your time doing it")

That depends on how deeply you want to understand them. But usually we only care about unit quaternions, so reducing it to axis and angle can already get you somewhere. Completely without worrying too much about all the implications of having complex numbers with not just i, but also j,k and all their properties.

Quote:
 I'm thinking of the following solution:

If I get you right you plan to store the total rotation/orientation as a quaternion and just add (well, multiply) the rotation for each update. That would be pretty much the way to do it.

Keep in mind that quaternions are smaller, not as problematic as deorthonormalizing matrices and quat-quat multiplication is a bit cheaper. But transforming something by a quaternion is a good bit more expensive. So they are usually only of interest if you really need to save memory or expect to concatenate lots of transformations so the cheaper multiplication is worth the more expensive transformation.

Share on other sites
Quote:
 Original post by TriencoIf I get you right you plan to store the total rotation/orientation as a quaternion and just add (well, multiply) the rotation for each update.
Yeah, that's the solution I'm thinking about trying later today.

Quote:
 Original post by TriencoKeep in mind that quaternions are smaller, not as problematic as deorthonormalizing matrices and quat-quat multiplication is a bit cheaper. But transforming something by a quaternion is a good bit more expensive. So they are usually only of interest if you really need to save memory or expect to concatenate lots of transformations so the cheaper multiplication is worth the more expensive transformation.

Ok, I don't think it'll make a huge difference in my project, but it always good to know :)

Quote:
 Original post by TriencoBut usually we only care about unit quaternions

That makes me think of another question. Do I have to normalize the quaternion after doing the "multiplication" ? like:
total = total*rot;total = total / sqrt(w^2+x^2+y^2+z^2)

because I think only x^2+y^2+z^2=1 and not w^2+x^2+y^2+z^2=1, since I will get a unit vector as the "vector part" of the quaternion, but the angle will be arbitrary, or?

Share on other sites
Quote:
 Original post by noofThat makes me think of another question. Do I have to normalize the quaternion after doing the "multiplication" ?

Mathematically? No, the result should also be a unit quaternion.
Technically? Every once in a while, because discrete representations of real numbers (ie. float/double) will introduce small errors that can add up. As sqrt can be kind of expensive it might be worth to check if a conditional might be better (or just normalize after every 100 or whatever multiplications). And yes, you normalize the whole thing, not just part of it (which means you can drop one -preferably positive- component if you really need every byte you can get).

Share on other sites
Tried the new idea and I still have the same problem. In every frame I do (rotation is the rotation accumulator as a quaternion, velocity is the normalized ball velocity vector):
Vector3D spinAxis = velocity.crossProduct(Vector3D(0,1,0)); // velocity is normalizedfloat rotationAngle = <calculates angle here>;if(!rotation) {  // first frame  rotation = new Quaternion(rotationAngle, spinAxis.getX(), spinAxis.getY(), spinAxis.getZ());} else {  // other frames  *rotation = (*rotation)*Quaternion(rotationAngle, spinAxis.getX(), spinAxis.getY(), spinAxis.getZ());}rotation->normalize(); // just to be sureglMultMatrixf(...);
Which gives me exactly the same results :( But when I think of it I would be kinda surprised if it had worked. Let's say for example that I have a really sucky frame rate and start by rolling 90 degrees of the ball along the Z-axis before frame 1. Then I turn around 90 degrees and roll the same distance along the X-axis before frame 2. In the first frame I will have:
rotation = Quaternion(90, 1, 0, 0);
and in the next frame:
rotation = rotation*Quaternion(90, 0, 0, 1);
which seems to be exactly what I had in my first attempt to use quaternions. I'm obviously missing something here, any more ideas? :)

*edit* It's working now, YEY! :D And it looks damn sweet :) I replaced:
  *rotation = (*rotation)*Quaternion(rotationAngle, spinAxis.getX(), spinAxis.getY(), spinAxis.getZ());
with:
  *rotation = Quaternion(rotationAngle, spinAxis.getX(), spinAxis.getY(), spinAxis.getZ())*(*rotation);
If someone has an explanation for why it should be that way and not the other, feel free to post a reply. (And thanks for all the help, really appreciated!)

[Edited by - noof on January 20, 2006 10:51:21 AM]

Share on other sites
Because, as you noticed, neither matrix nor quaternion multiplication is commutative. Order matters and in this case is making the difference between rotating around the local axis (after all previous rotations have been applied) and the global axis (before previous rotations were applied). As you calculate your rotation axis in world coordinates (as far as I can tell), you also need to multiply the other way round. But which one is "right" pretty much depends on whether you swapped quaternions in your multiplication function or not. I would guess some people do, simply because it allows to use quat*=other_quat instead of having to write quat=otherquat*quat all the time.

1. 1
2. 2
3. 3
Rutin
19
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• Forum Statistics

• Total Topics
633657
• Total Posts
3013197
×