calico210 122 Report post Posted August 13, 2002 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] 0 Share this post Link to post Share on other sites

Yann L 1802 Report post Posted August 14, 2002 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 0 Share this post Link to post Share on other sites

rick_appleton 864 Report post Posted May 22, 2003 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. 0 Share this post Link to post Share on other sites

Dreamforger 122 Report post Posted May 22, 2003 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 is1 0 0 00 1 0 00 0 1 00 0 0 1 within this system you created your objects, eg the head.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 tohx ux ex pxhy uy ey pyhz uz ez pz 0 0 0 1 0 Share this post Link to post Share on other sites

rick_appleton 864 Report post Posted May 27, 2003 thanks, that works like a dream, and I even understand the math behind it.thanks again. 0 Share this post Link to post Share on other sites