• 10
• 12
• 12
• 14
• 15

# Converting coordinates to rotations.

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

## Recommended Posts

I am looking for a formula that takes a normalized vector indicating an up-direction, and uses it to rotate a camera to the specified orientation. I am using Java, but I understand a number of computer languages, so feel free to write in whatever language you are comfortable with. In my initial attempt, I came up with the following formula (I tried to translate this to pseudo code so everyone can follow):
distance = lengthOf( previousUpVector - newUpVector );
angle = acos( (2 - (distance*distance)) / 2 );
cross = crossProduct( previousUpVector, newUpVector );
normalize( cross );

rotateCameraAroundAxis( cross, angle );

The idea here was to use the law of cosines to calculate the angle between the two vectors:
cos(A) = (b*b + c*c - a*a) / (2*b*c)

So:
A = acos( (b*b + c*c - a*a) / (2*b*c) )

Since b and c are normalized (length = 1):
A = acos( (2 - a*a) / 2 )

And then calculate the cross-product to calculate what axis to rotate around. This formula doesn't seem to be working though (I'm getting sporatic rotations when tring to use this formula to match a camera's orientation to an object's). Any ideas what might be wrong with my formula, or does anyone know of one that works?

##### Share on other sites
1. Normalise v1 and v2 (in your case previousUpVector and upVector)
2. Compute the dot product of v1 and v2
3. Get the acos of the result obtained in step 2
4. Compute the cross product of v1 and v2
5. Rotate by angle obtained in step 3 around the vector obtained in step 4 (you might need to normalise the cross product vector as well depending on how your angle around axis rotation method works).

##### Share on other sites
Awsome, thanks for the quick reply! I will try this out right now.

##### Share on other sites
Unfortunately, this formula does not appear to be working (possibly there is a problem elsewhere in my program, so I will check into that as well). In the mean time, can anyone think of another formula to use for this?

Here is an applet demonstrating the problem I am getting:

http://www.paulscode.com/source/CameraOrbit/

Arrow keys orbit the camera around the tree (the tree is stationary, only the camera is moving). The new up-vector I am passing to the formula is printed out to the Java console for every call.

The problem seems to be after rotations around the x-axis. You can rotate around the y-axis all day with no problem.

If you need further information/source, let me know. Thanks in advance!

##### Share on other sites
I think my problem is that I am trying to solve look-direction and up-direction seperately.

Any ideas about how to take both a look-direction vector and an up-direction vector and convert that into rotations?

##### Share on other sites
Quote:
 Original post by paulscodeI think my problem is that I am trying to solve look-direction and up-direction seperately.Any ideas about how to take both a look-direction vector and an up-direction vector and convert that into rotations?
If you have a look direction and an up direction, then their cross-product gives you the "right" direction, which gives you 3 orthonormal basis vectors with which you can build a rotation matrix.

HERE: Scroll down to "Constructing a camera frame". Basically a 4x4 matrix of [RIGHTVEC UPVEC -DIRECTIONVEC Origin] gives the transformation for orientation.

EDIT: Changed the sign on the direction vector in the matrix... sorry about that.

[Edited by - smitty1276 on October 15, 2008 7:55:26 PM]

##### Share on other sites
Ok, I'm tracking the first part of "Constructing Camera Frame". I have a couple of questions before I'll understand what to do with the information, though.

After taking a closer look, I've figured out the U, V, and W direction vectors and the "e" position vector (correct me if I am wrong here):

U = (-up.x, -up.y, -up.z)V = look CROSS upW = (-look.x, -look.y, -look.z)e = position of camera in "world space"

Next, we have two 4X4 matrices:

U.x, U.y, U.z, 0.0V.x, V.y, V.z, 0.0W.x, W.y, W.z, 0.00.0, 0.0, 0.0, 1.0

And the second 4X4 matrix:

1.0, 0.0, 0.0, e.x0.0, 1.0, 0.0, e.y0.0, 0.0, 1.0, e.z0.0, 0.0, 0.0, 1.0

From here, the author loses me. Maybe I am not understanding the terminology, so I don't know which of the various formulas apply to what I am tring to do. I want to create a rotation matrix, but I dont know what to do with those original two matrices to accomplish this?

[Edited by - paulscode on October 16, 2008 7:55:47 PM]

##### Share on other sites
DOH! I am dumb. I didn't pay attention to what you said, that [RIGHTVEC UPVEC DIRECTIONVEC Origin] IS a rotation matrix! Sorry about that. Yes, this works beautifully. Thanks a million!!

Here is the working applet:

http://www.paulscode.com/source/CameraOrbit/Vectors2Matrix/

##### Share on other sites
Ah, good to see its working... did you use the negative direction vector?

##### Share on other sites
Quote:
 Original post by smitty1276Ah, good to see its working... did you use the negative direction vector?

I had to play around a bit to get it to work properly. I ended up reversing the cross product for "right" and not using the negative of the look directon. The following is the result (this is in Java using the jPCT engine):

    public void setOrientation( Camera camera, SimpleVector look, SimpleVector up )    {        SimpleVector right = up.calcCross( look ).normalize();        Matrix m = new Matrix();                m.set( 0, 0, right.x );        m.set( 1, 0, right.y );        m.set( 2, 0, right.z );        m.set( 3, 0, 0.0f );                m.set( 0, 1, up.x );        m.set( 1, 1, up.y );        m.set( 2, 1, up.z );        m.set( 3, 1, 0.0f );                m.set( 0, 2, look.x );        m.set( 1, 2, look.y );        m.set( 2, 2, look.z );        m.set( 3, 2, 0.0f );                m.set( 0, 3, 0.0f );        m.set( 1, 3, 0.0f );        m.set( 2, 3, 0.0f );        m.set( 3, 3, 1.0f );                camera.setBack( m );    }

I could not have solved this problem without understanding what a rotation matrix is, so thanks again!