<ul>
<li><p>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).</p></li>
<li><p>How are you applying the rotation? <code>glRotate</code>? Have you tried glRotate with the negative of your estimated angle?</p></li>
<li><p>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?)</p></li>
</ul>

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

CC BY-SA 3.0
2017-04-06T08:39:04.137Z
Hey, I mentioned it in the EDIT that always returning 90 degrees was the mistake I did. But even after that, I got the results as shown in the image. Most of the cylinders went to their correct position but some have deviated a little for some reason. Could be an implementation error again. Thanks for your answer :)
Akshay Jain
CC BY-SA 3.0
2017-04-04T02:43:50.083Z
2017-04-04T02:43:50.083Z
Jacob Panikulam
<p>You also need to scale the cylinder. Take the magnitude (length) of $p_1 - p_2$, and divide it by the length of the cylinder (or don't divide, if your cylinder is already unit length). This gives you the scale factor. You can apply this as a non-uniform scale along the axis that the cylinder points in. (Since you were able to work out how to apply a rotation, I assume you can make the appropriate scaling matrix given just the scale factor and direction.)</p>

<p>Make sure to scale it before rotating or translating the cylinder! If you translate before you scale, you'll translate the start point of the cylinder as well as its length, so the start won't be attached to $p_1$ any more. If you rotate before you scale, you won't be scaling the length of the cylinder, you'll be stretching it in the wrong direction, so it won't be a cylinder any more.</p>

CC BY-SA 3.0
2016-10-05T15:48:17.403Z
Please see the screenshot. The issue is not regarding the scaling. I already scaled cylinders to match the length between the spheres.
Akshay Jain
CC BY-SA 3.0
2016-10-05T15:33:28.897Z
2016-10-05T15:33:28.897Z
Dan Hulme
<p>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.</p>

<p>What I tried so far after searching online:</p>

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

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

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

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

<p>I am still not getting exactly the correct output. Here is the screenshot.</p>

<p><a href="https://i.sstatic.net/GwXFS.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GwXFS.jpg" alt="Atom bond representation of ligands"></a></p>

CC BY-SA 3.0
2016-09-14T12:24:32.390Z
Assuming this is from an exercise question, does the question specify whether the height of the cylinder is the same as the distance between the specified points?
trichoplax is on Codidact now
CC BY-SA 3.0
2016-09-14T12:26:11.993Z
We need to calculate the height based on the distance between the two points.
Akshay Jain
CC BY-SA 3.0
2016-09-14T12:32:31.017Z
And the result is wrong because it doesn't join the two points. I drawn the points(as small spheres) and the cylinder but the cylinder is attached only the point I have translated it to.
Akshay Jain
CC BY-SA 3.0
2016-09-14T21:28:10.253Z
A picture would definitely be helpful, you should also note that the cross product of two parallel vectors leads to the zero vector, which can happen if the vector p points in the same direction as the vector n. Are the 2 points in 3D lying on the xy plane as well?
Fred Garnier
CC BY-SA 3.0
2016-12-05T07:43:39.707Z
How are the cylinders points defined?
joojaa
<opengl><transformations>
Rotate a cylinder from XY plane to given points
Akshay Jain
# Rotate a cylinder from XY plane to given points

(Asked by: username_0 on 2016-09-14)

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:

1. Calculated the vector between the two points p = p1 - p2;
2. Took the normal vector to the plane(0,0,1) v
3. Axis of rotation = v x p (cross)
4. angle of rotation = acos(|v|.|p|) (unit vectors)
5. I applied this rotation.
6. 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.

[![Atom bond representation of ligands](https://i.sstatic.net/GwXFS.jpg)](https://i.sstatic.net/GwXFS.jpg)

**Tags:** opengl, transformations

> **Comments:**
> **username_1**: Assuming this is from an exercise question, does the question specify whether the height of the cylinder is the same as the distance between the specified points?  
> **username_0**: We need to calculate the height based on the distance between the two points.  
> **username_0**: And the result is wrong because it doesn't join the two points. I drawn the points(as small spheres) and the cylinder but the cylinder is attached only the point I have translated it to.  
> **username_2**: A picture would definitely be helpful, you should also note that the cross product of two parallel vectors leads to the zero vector, which can happen if the vector p points in the same direction as the vector n. Are the 2 points in 3D lying on the xy plane as well?  
> **username_3**: How are the cylinders points defined?  

---

## 2 answers

### Answer by username_4 on 2017-04-04. Score: 2

* 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$.

> **Comments:**
> **username_0**: Hey, I mentioned it in the EDIT that always returning 90 degrees was the mistake I did. But even after that, I got the results as shown in the image. Most of the cylinders went to their correct position but some have deviated a little for some reason. Could be an implementation error again. Thanks for your answer :)  

---

### Answer by username_5 on 2016-10-05. Score: 1

You also need to scale the cylinder. Take the magnitude (length) of $p\_1 - p\_2$, and divide it by the length of the cylinder (or don't divide, if your cylinder is already unit length). This gives you the scale factor. You can apply this as a non-uniform scale along the axis that the cylinder points in. (Since you were able to work out how to apply a rotation, I assume you can make the appropriate scaling matrix given just the scale factor and direction.)

Make sure to scale it before rotating or translating the cylinder! If you translate before you scale, you'll translate the start point of the cylinder as well as its length, so the start won't be attached to $p\_1$ any more. If you rotate before you scale, you won't be scaling the length of the cylinder, you'll be stretching it in the wrong direction, so it won't be a cylinder any more.

> **Comments:**
> **username_0**: Please see the screenshot. The issue is not regarding the scaling. I already scaled cylinders to match the length between the spheres.  

---

