3
$\begingroup$

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

$\endgroup$
5
  • $\begingroup$ 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? $\endgroup$ Sep 14, 2016 at 12:24
  • $\begingroup$ We need to calculate the height based on the distance between the two points. $\endgroup$ Sep 14, 2016 at 12:26
  • $\begingroup$ 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. $\endgroup$ Sep 14, 2016 at 12:32
  • $\begingroup$ 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? $\endgroup$ Sep 14, 2016 at 21:28
  • $\begingroup$ How are the cylinders points defined? $\endgroup$
    – joojaa
    Dec 5, 2016 at 7:43

2 Answers 2

2
$\begingroup$
  • 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$.

$\endgroup$
1
  • $\begingroup$ 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 :) $\endgroup$ Apr 6, 2017 at 8:39
1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Please see the screenshot. The issue is not regarding the scaling. I already scaled cylinders to match the length between the spheres. $\endgroup$ Oct 5, 2016 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.