Advertisement

Calculating angles of rotation

Started by October 31, 2012 10:45 PM
20 comments, last by Alessandro 12 years, 3 months ago
I also would like to ask this. Since the application I'm working on is based on a SDK and does not accept direct opengl commands, I need to apply rotation angles separately.

So I can't do something like this:

glRotatef(rotationAngle, CP.x, CP.y, CP.z); // where CP is a vector representing the axis of rotation

but I rather have to:

setRotX(angleX);
setRotY(angleY);
setRotZ(angleZ);

I need, to provide the three angle components (that the right term) for each axis, obviously gathered from the rotationAngle and the CP vector. Also this is something that I can't figure if and how is possible.
Use a better SDK? :P

We need to know what order the final transform gets applied, does it do X followed by Y, followed by Z or some other order?
Advertisement
Well, changing SDK unfortunately is not an option smile.png

The order of rotation is ZXY.

I'm so bad at math that I'm really amazed to realize how something that I figured out a simple operation (place an object at the same base point of a guide, and orient it along it), instead seems to be so complicated.
I hope you guys can come up with a solution to help me with this.

I think I understand what you mean, but then, I'd possibly solve this matter? Would the solution you provided in your first reply do it ? BTW, what means the "constraint was that it was the minimum possible rotation"?
Sorry for all these questions, it's just that I'm very bad at math and all these terms and notions puzzle me.

My solution is one way of doing it given the constraint I assumed. As has been said, you need to fix the problem that your direction vector does not correspond to an orientation. There are infinitely many orientations, and consequently infinitely many rotations that you can apply, that maps your original vector to the vector AB. You need additional information to reduce this set from infinity to one.

I just claim that I want the shortest possible rotation. The solution I proposed gives you the shortest possible rotation. There is no other rotation that has a shorter angle.
The final orientation may not be what you expect, but the object is indeed pointing in the right direction just as you requested.

Well, changing SDK unfortunately is not an option smile.png

The order of rotation is ZXY.

I'm so bad at math that I'm really amazed to realize how something that I figured out a simple operation (place an object at the same base point of a guide, and orient it along it), instead seems to be so complicated.
I hope you guys can come up with a solution to help me with this.

As has been said also, you only need two rotations but an ZXY order has three rotations. One rotation must be removed from the calculation or you have too many rotations to solve the problem. You could, for example, leave one of the rotations at zero degrees and calculate the other two. That will give you a unique solution. But the problem is that you have three angles and thus three ways to zero an angle, leaving you with three different solutions. Three different solutions that indeed give you three different orientations.

I'll see if I can demonstrate the issue with a very simple experiment for you.

  1. Sit in front of your computer as usual, looking towards your screen. Your face is now facing the direction towards the screen.
  2. Tilt your head towards the right but keep looking at the screen. Your face is still facing the direction towards the screen, but the head is tilted 20-ish degrees.
  3. Tilt your head towards the left instead.

In all three points, your head is facing towards your screen. Thus, they all satisfy your wish to face in a certain direction, but their orientations are different, since you tilt your head to the sides.

That is the difference between direction and orientation: you have a direction and you want an orientation.
I think I understand what you mean. Sorry to be so stubborn but, in my case, the guide represent both the direction and the orientation, wouldn't it?
I mean, I just need to orient and direct a 3d object so that it matches that guide.
You're all allowed to insult me if I still can't understand it biggrin.png

I mean, if that guide has origin at (0,0,0), and aims at (0,1,1), there must be a chance to say "Ok, let get another object, sets its origin at (0,0,0) as well, and aim it at (0,1,1)"...
No, your direction is still only a direction. Once the object is facing the guide-vector, you can rotate is arbitrarily around the guide-axis and it will still face the guide-vector. You can face an object towards the X-axis if you like, but you can rotate is as much as you like around the X-axis and it will still face towards X-axis. You need something else to fix the orientation because the direction isn't enough.
Advertisement
I'd do it like this:

1) Project the destination vector onto the XZ axis. Do this simply by setting the y component to zero. The projected vector will no longer be normalised.
2) Get the angle between the object's vector and the vector resulting from 1), and rotate around the y axis by this amount.
3) Now you can rotate your object onto the destination vector using the method outlined earlier in this thread (cross product vectors to get rotation axis, dot product to find angle). This will cause the object to point at the destination vector without any undesired rotation along the object's local x axis.
It's the same problem as when you are placing a camera.

besides having the direction the camera should look, you also need the up-vector to... know which direction is up.

If you don't care, just set the up vector to Vec3(0,1,0) or something. (preferably the same axis as your local up is)
Personally, I would construct the orientation matrix from the direction and up-vector, something like this (use the syntax from our own vector lib, but I think its self explanatory):

Vec3 right = dir.cross(up);
Vec3 newup = right.cross(up);
Mat4 m(dir.normalize(), newup.normalize(), right.normalize()); //Sets columns to the vectors, assuming local x is "forward", and z is "right" and y is "up"
Previous two responses: I think you missed his response that the API he's using permits ONLY setting z-x-y euler angles.
Just to report that thanks to all your suggestions I managed to solve this matter. As you wrote I calculated the rotation axis and the angle between the guide and the object, rotated the object, built up a quaternion from it and extracted the euler angles.

This topic is closed to new replies.

Advertisement