How do I orientate a cylinder between two points

I need to draw a line (actually a cylinder) between two points. I know that the length of the clinder will just be the distance between the two points, and the center of the cylinder will be at the midpoint of the two points: ((x2-x1)/2, (y2-y1)/2, (z2-z1)/2); but how do I go about figuring out what the transformation matix will be. Sorry, I know this is basic 3d math stuff.

Thanks in advance,
RDL

You might be able to get away using Transform3D.lookAt() to create a transform thats looks from the centre point of the cylinder to one end.

Kev

But what do you chose as Upper Vector? (0, 1, 0)? I use it the API way, lookAt(frompoint, topoint, upvector) and I’m getting bad results ???

Your up vector should be any line thats at a tangent to the line between the two points.

Kev

Any chance of anyone being a little more explicit on how to accomplish this whether using the lookAt function or just basic maths. I have been playing around with this and my Transform3D matrix ends up with NaN in the matrix or there doesn’t seem to be any transformation going on at all when I try it.

I imagine this is a pretty straight forward procedure but my lack of graphics experience and a lack of understanding of the Java API is making this difficult to accomplish.

Thanks
RDL

Its been a while so I’m going to assume that the matrix lookAt takes 3 vectors, forward, up and right (or left, whatever).

Your forward vector is easy, its just the (normalised) vector from point A to point B. ‘Up’ can be arbitrary chosen depending on your world. Something like (0, 1, 0) perhaps. Then you can do a cross product between the ‘forward’ and ‘up’ vectors to get a ‘right’ vector. Then again cross your ‘forward’ with your new ‘right’ vector to get a proper up vector. Then plug those into your matrix. If you start with all vectors normalised beforehand you shouldn’t need to re-normalise them again at the end.

I tried the following (see function below) but when I setRotation on the Transform3D matrix of the object I get absolutly no change.


      public static Matrix3f getRotationMatrixBetweenPoints(Vector3f p1, Vector3f p2){
            Transform3D t3d = new Transform3D();
            Vector3f midpoint = midPoint(p1, p2);
            System.out.println(t3d);
            t3d.lookAt(new Point3d(p1), 
                        new Point3d(p2), 
                        new Vector3d(0,1,0)
                        );
            System.out.println(t3d);
            Matrix3f transformationMatrix = new Matrix3f();
            t3d.get(transformationMatrix);
            return transformationMatrix;
      }

RDL

…the code should work fine. Exept if the ‘new Vector3d(0,1,0)’ is in the same direction as p1 to p2 (or the opposite).

I tried the lookAt function but I get the following translation matrix which has NaN numbers (?):

NaN, NaN, NaN, NaN
NaN, NaN, NaN, NaN
0.0, 1.0, 0.0, -1.0
0.0, 0.0, 0.0, 1.0

Point3d p1 = new Point3d(0,0,0);
Point3d p2 = new Point3d(0,1,0);
Vector3d mag = new Vector3f();
mag.sub(p2, p1);
Transform3D translate = new Transform3D();
Point3d eye = new Point3d(mag);
Point3d center = new Point3d(0,0,0);
translate.lookAt(eye, center, new Vector3d(0,1,0));

I am guesing I am getting this because there is some divide by 0s going on in the lookAt function and so this will be no good for me then?

Thanks
RDL

Here, it’s the case. Your “from-to” vector is the same as your “up” vector: both (0,1,0)
They must not be aligned! …and preferably perpendicular.
Try for example (0,0,1) as “up” vector and it’ll be ok.

misterX,

I don’t know if you tried it out but it doesn’t work for me. I did stop getting the NaNs in the rotation matrix which I changed the up axis, but the rotations still end up being off – looking a lot like how they were off when I tried coding it myself.

For: (0,1,0) it was off by 90 on the x axis
For: (0,1,1) it was off by 90 again, being essentially perpendicular to the two points
For: (1,1,1) it was off by about 45 degrees

I did come accross this website which explains it in openGL but when I tried to implement it in Java it wouldn’t work for me:

http://www.mfcogl.com/OpenGL%20-%20draw%20cylinder%20between%202%20pts.htm

add t3d.invert(); after the t3d.lookAt(…);

masterX,

Thanks but it still doesn’t do the trick. It is still off by 90 degrees for the first two, and for the points: (0,0,0) & (1,1,1) is still off by 45 degrees, but with a 90 degree extra rotation as compared to one without the invert call on the transform

Best,
RDL