Rotate a cylinder from XY plane to given points I am given geometry of a cylinder which lies on the XY plane with 1 unit radius. And given coordinates of 2 points in 3D I need to move the cylinder so that it connects the two points.

What I tried so far after searching online:


Calculated the vector between the two points p = p1 - p2;
Took the normal vector to the plane(0,0,1) v
Axis of rotation = v x p (cross)
angle of rotation = acos(|v|.|p|) (unit vectors)
I applied this rotation.
Translated one of the end point of cylinder to one of the points.


but it moves the object to a wrong location. Any help what I'm doing wrong?

EDIT: My apologies for not being able to respond.
The main issue was with this line.


angle of rotation = acos(|v|.|p|) (unit vectors)
I normalized the vectors then used this wrong formula. Which will always return 90 degrees.


I am still not getting exactly the correct output. Here is the screenshot.
When you say $acos(\|v\|\|p\|)$ are you saying $acos(\|\hat{v} \cdot \hat{p}\|)$? If not the second, your angle will be some bizarre thing (In fact, you'll always get 90deg).
How are you applying the rotation? glRotate? Have you tried glRotate with the negative of your estimated angle?
Are you doing rotation/scaling/translation in the right order? Could you post the actual code that is executing if it is short? (If not, then a small runnable excerpt?)


Also: I'd be quite careful, as the cross-product-as-axis approach will produce goofy results when $v \parallel u$.