2

I've been searching a lot on this problem, but I couldn't really find an answer that would fit.
I need to rotate a cylinder around a given point (eg, 0,0,0), but the pivot of the cylinder is given by default. How do i change that?

I found this topic, and it's quite what I would want to do, but I don't know how to do it with java.

To explain better what I would like to do, I'll show 3 images.(v)

imageshack.us/photo/my-images/259/aintgood.jpg

imageshack.us/photo/my-images/840/whatineed.jpg

imageshack.us/photo/my-images/705/nogoodn.jpg

So, the first image shows my basic problem, the cylinder should be positioned with the end at the center of the sphere, let's say (0,0,0). The user gives two angles. The first one is for a rotX command, the second one for a rotZ one. The pivot of the cylinder is at its center, so, as image 3 shows, even if i translate the cylinder so its end is at the center of the sphere, when it rotates, the whole thing ruins.

Image 2 shows what the cylinder-sphere group should look like, regardless the given angles. The image is not obtained based on an algorithm, but based on calculus, and mouserotated.

Community
  • 1
  • 1
Mihai Bujanca
  • 4,089
  • 10
  • 43
  • 84

1 Answers1

3

The general procedure for rotation about an arbitrary point P is:

  1. Translate by -P (so P is at (0, 0, 0))
  2. Rotate around the origin
  3. Translate by P (to bring the origin back to the original location of P)

The easiest way to do this is to represent everything in homogeneous coordinates and represent translations and rotations by matrices. Composing the above three transformations (translate-rotate-translate) is done by matrix multiplication. If the rotation is composed of two or more simpler rotations, then the rotation matrix itself is a product of the matrices for the simpler rotations.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thank you, this was really helpful. Indeed, what I needed in my case, was only to translate first, an then rotate, not the reversed order. – Mihai Bujanca Oct 30 '12 at 19:39
  • I now hope that representing in homogeneous coordinates will help me pass over my next problem, I need to find a formula to determine the opposite point (the center of the circle at the other end), so I can bound two spheres. Thanks a lot for your help, and if you have any idea for this, I'd be thankful for a new piece of advice. – Mihai Bujanca Oct 30 '12 at 19:47
  • 1
    @BujancaMihai - You can apply the same idea: to find the opposite point of a point on a sphere, transform everything so the center of the sphere is at the origin, negate the point, and then transform everything back. Hope this helps. P.S. Remember to upvote and/or accept answers that are helpful and/or answer your question. :) – Ted Hopp Oct 30 '12 at 21:54
  • The same idea works indeed if i only want to have a new sphere on the direction of the cylinder. But, given a simple example shows the problem. If I have the original sphere at (0,0,0), my cylinder is 2f length, i can just translate the cylinder to (1,0,0) an then rotate it and it's fine, one end of the cylinder is at the center of the sphere. But, if I have a new Sphere that should bound at the other end of the cylinder, i need to determine a formula for that. For example, for rotX 45 degrees, i would have my new sphere at (sin 45,cos 45, 0). But for other angles, the point changes. – Mihai Bujanca Oct 31 '12 at 14:19