#### Archived

This topic is now archived and is closed to further replies.

# Rotation from one vector to another, Möller

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

## Recommended Posts

I found this http://www.ce.chalmers.se/staff/tomasm/code/fromtorot.txt. It's Möllers implementation for calculating the rotating matrix between two vectors as descriebed in some papers and in the real-time rendering book. The thing is in the beginning it takes care of a special case, when the two vectors are parallel. I don't understand what the code does. I would just check the crossproduct to see if it's parallel. Then return identity matrix if the vectors are pointing at the same direction, otherwise -1 * identity matrix. Any ideas? [edited by - __fold on December 16, 2003 9:18:20 AM]

##### Share on other sites
A dot product is more efficient than a cross product (there are less operations to do).

Thus, it''s better to do the dot product of both vectors (a and b).

here''s the thing:

When the vectors are perpendicular, the dot product is 0.

When they''re parallel, it''s either 1 or -1, depending on whether they''re pointing in the same direction (1) or opposite directions (-1).

Following examples:

[0, 0, 1] . [0, 1, 0] (perpendicular vectors)

0*0+1*0+0*1 = 0

[0,0, 1] . [0, 0, 1] (parallel -same direction)

0*0+0*0+1*1 = 1

[0, 0, 1] . [0, 0, -1] (parallel - opposite directions)

0*0+0*0+1*-1 = -1

Anything "between" parallel and perpendicular will have a value such that 0 < abs(dotproduct) < 1.

Now, the epsilon in the if statement is there to account for floating-point errors. It would suck if you did a comparison againt 1 and your normalized vectors, because of precision, returned 0.998 or something like that, so the epsilon is there to account for that.

Hope that helps,

Josh

##### Share on other sites
Ah, ok, now I understand. But i think I test this, I'm not sure at all that the solution he presents is faster, especially when he uses coefficients. He's solution is almost accurate, that's a good thing though.

Thanks for the reply!

[edited by - __fold on December 16, 2003 4:49:46 PM]

##### Share on other sites
... assuming your vectors are normalized.

Your problem is basically a quaternion division. There is an equivalance between points and quaternions. Google about quaternions and point rotations to understand it better. Also check some mouse-trackball simulation code.

##### Share on other sites
Charles B: All the vectors are normalized and I have already a fully working Quaternion class.

I want to create the rotation matrix cause I need to rotate, transate and rotate alot of points in software. So I want to calculate a matrix M = R*T*R and then calculate Vi_new = M * Vi for 1 < i < 300000 (it''s used for face/face CD).

The code for caclulating the from-to-matrix is actually using the quaternion q^ = (1/sqrt(2(1+s.t)(s x t), sqrt(2(1+s.t)/2) (where s and t are vectors) which simplifies to the matrix in the last part of the code.

1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633696
• Total Posts
3013390
×