#### Archived

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

# rotation of a vector in 3d space to another vector in 3d space

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

## Recommended Posts

I am trying orient a model of a head in the direction of a vector. I have a model of a head which is facing in the direction of the vector (1,0,0) I want to rotate the vector to a new vector in 3d space. I first take the cross product to determine an axis of rotation. Then I find the angle between the two vectors, by taking the dot product of the two vectors and then taking the acos of the dot product. Using that angle I rotate about the axis to have the head look in the direction of the new vector. glRotatef(rot_ang,rot_axis[0],rot_axis[1],rot_axis[2]); This seems to work if the original vector and the new vector point in the same hemisphere, but if the new vector is looking back the other way it behaves very odd. Please help. [edited by - calico210 on August 13, 2002 1:11:50 PM] [edited by - calico210 on August 13, 2002 1:21:47 PM]

##### Share on other sites
quote:

This seems to work if the original vector and the new vector point in the same hemisphere, but if the new vector is looking back the other way it behaves very odd.

Hmm. Define ''very odd''. What does your head do, but shouldn''t ?

The dotproduct will always find the smallest angle between the given vectors. So, if the two vectors are pointing in different hemispheres (the real angle between them is larger than 180°), then the dotproduct will give you back a wrong rotation angle.

/ Yann

##### Share on other sites
I''m sort of having the same problem.

I want to place a model in the 3d world by rotating/moving it so the origin and 2 axes coincide with two vectors that I give.

For instance, I have a heading and an up-vector (perpendicular to each other) and I want to orient the model with the x-axis along the heading, and the z-vector (or y-vector) along the up-vector.

I already had the code above to ''join'' the heading and the x-axis, but now I''m a bit stumped on how to rotate the z-axis to the up-vector.

Obviously the rotation axis is the heading vector, but how can I easily compute the angle, without needing to calculate the resultant of the z-axis rotated with the heading by hand?

Any hints or tips are greatly appreciated.

##### Share on other sites
using 4x4 matrizes the task is trival and best accomplished by setting the matrix directly without eplicitly rotating the objects.

4x4 matrices have nice property: they transform your base coordinate system, thereby moving objects.

Your initital base coordinate system is
1  0  0  00  1  0  00  0  1  00  0  0  1

You now want to align one of the vectors
(1 0 0)T first column (nose?)
(0 1 0)T second column (top of head?)
(0 0 1)T third column (ears?)

to a heading ''h'' and a up ''u''. First of all you calc the third vector by aplying the crossproduct to h and u, giving you ''e'', for ears. The head itself is centered on the origin and should be moved to ''p''.

Each of h, u, e and p are vectors with three components x,y,z.

select the base vector you want to turn to certain direction and replace it by h,u,e respectively. the base vectors are the first 3 colums, padded with a 0 of the base matrix. So a face looking at (1 0 0)T when no transformation is applied to it has its heading in the first column.

to make the head turn the right way, set the matrix to

hx ux ex pxhy uy ey pyhz uz ez pz 0  0  0  1

##### Share on other sites
thanks, that works like a dream, and I even understand the math behind it.

thanks again.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

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

• Total Topics
633736
• Total Posts
3013598
×