Advertisement

Calculating angles of rotation

Started by October 31, 2012 10:45 PM
20 comments, last by Alessandro 12 years, 3 months ago
Given a XYZ coordinate system, and a vector AB as represented in the attached image (where A is matching the world center 0,0,0 ), how could I calculate the angles of rotation of AB along the 3 axis?
Meaning that if you start with AB at 0,1,0 I'd like to know what angle rotations have been applied to achieve its new position (which is known) in space.
To put it into numbers, if AB is at (0,1,0) and later you set it at (1,0.4,0.3), there is a way to calculate the angles that if forms within the x,y,z axis?

I don't know if the image are really explanatory, let me know if you need further info. Thanks for any help!

axistest.jpg
First calculate the cross-product between the original vector and the vector AB: that will give you an axis of rotation. Then calculate the dot-product between the original vector and the vector AB: that will give you the arccosine of the angle between them and consequently the angle to rotate.

That will give you the smallest possible rotation in order to rotate a vector from some original direction to the direction AB.
Advertisement
You only need 2 angles to specify a direction in 3 dimensions, and your question really depends on what axis those rotations should be about, and in what order the rotations are supposed to occur.

Even specified properly, there's no total solution, since you can end up with gimbal lock, eg:

Say you define the rotations to be: a rotation about the y-axis, then a rotation about the x-axis; if AB starts at (0, 1, 0) then it is only possible to define the rotations if the new AB happens to be in the yz plane. No matter how you specify the two rotations you will end up with this problem. I 'guess' you could add a third rotation, that is only used in this corner case; but really you can probably do much better with a different representation of rotation, whether it be a quaternion or axis-angle

[Edit: Bob's suggestion is to use axis-angle]

First calculate the cross-product between the original vector and the vector AB: that will give you an axis of rotation. Then calculate the dot-product between the original vector and the vector AB: that will give you the arccosine of the angle between them and consequently the angle to rotate.

That will give you the smallest possible rotation in order to rotate a vector from some original direction to the direction AB.


Thanks Bob, but in a opengl context, if I gather "the arccosine of the angle between them and consequently the angle to rotate.", how would I use such angle on individual axis?
I mean, what if I need to use some commands like:

glRotatef(bankDegrees, 0, 0, 1);
glRotatef(pitchDegrees,1, 0, 0);
glRotatef(headingDegrees, 0, 1, 0);
It is not a well defined problem in that situation. As Luca said, you only need two of the three angles in that case. The third one is arbitrary. The problem is that a vector or a direction is not an orientation, and rotations deal with orientations.
Reading back what Bob wrote, probably I should to something like this:

vector CP = cross product between the original vector and the vector AB
rotationAngle = arccosine(dot-product between the original vector and the vector AB)

Now I could do something like this:

glRotatef(rotationAngle, CP.x, CP.y, CP.z);

Am I on the right path?
Advertisement
That's spot on.
Just to give more details, here is what i'm trying to do. In the first image, I have a guide line, made of two points (whose coordinates are known), and I need to replace that guide with a 3D object (see second image).

Placing the 3D object it's simple, since I already know the guide base point coordinates, but I'd like to orient the 3D object so that it matches the guide orientation as well. And the issue I have is to calculate those angles so that I can apply those to the 3D object as well.

img1.jpg

img2.jpg
Looks like your guide is a direction and not an orientation, and, as I said, rotations deal with orientations and not with directions. In order to orient the object in the direction of the guide, you need additional information or constraints to disambiguate how the direction maps to an orientation. For example, the constraint in my first reply was that it was the minimum possible rotation, and that solution is unique in most cases. Just saying that you want the object to be rotated in a specific direction just isn't enough information to construct a rotation.
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.

This topic is closed to new replies.

Advertisement