First of all, thanks to Carl and Niklas for thier hints and help.
I ended up with the following approach. (Which seems to work in all the
test data I can find.)

The folloing code/math/hidden errors is/are GPL.  Use/abuse as you feel the need.
This started out as the "Arrow" class I found on j3d.org.  If the author of
that class wants to use it and post an uppdate on j3d.org, feel free. :)

Also, I realize that it should throw an illegal argument exception instead of
returning identity, but it works.

   /**
     *  creates a rotation/translation suitable for placing an object between
     *  two points with its "top" and "bottom" oriented twards the first and
     *  second points respectively.
    **/
    public static void getTwoPointTransform(Tuple3d stPt,
                                            Tuple3d endPt,
                                            Transform3D transform) {
        if(stPt.equals(endPt)) {
            transform.setIdentity();
            return;
        }
        Vector3d unitVect = new Vector3d((endPt.x - stPt.x),
                                        (endPt.y - stPt.y),
                                        (endPt.z - stPt.z));
        unitVect.normalize();
        // find rotation about X axis
        double angle_x = Math.acos( unitVect.y );

        // find rotation about Y axis
        double angle_y = Math.PI/2.0;
        if(unitVect.z != 0.0) {
            angle_y = Math.atan( unitVect.x/unitVect.z );
        }
        if(unitVect.z <= 0) {
            angle_y += Math.PI;
        }

        // determine the transform matrix for X.Y axis rotation
        // and translation to the midpoint of the line segment.
        Matrix4d rotMat = new Matrix4d();
        double A = Math.cos(angle_x);
        double B = Math.sin(angle_x);
        double C = Math.cos(angle_y);
        double D = Math.sin(angle_y);
        rotMat.m00 = C;
        rotMat.m01 = B*D;
        rotMat.m02 = A*D;
        rotMat.m03 = stPt.x + (endPt.x - stPt.x)/2.0f;
        rotMat.m10 = 0.;
        rotMat.m11 = A;
        rotMat.m12 = -B;
        rotMat.m13 = stPt.y + (endPt.y - stPt.y)/2.0f;
        rotMat.m20 = -D;
        rotMat.m21 = B*C;
        rotMat.m22 = A*C;
        rotMat.m23 = stPt.z + (endPt.z - stPt.z)/2.0f;
        rotMat.m30 = 0.;
        rotMat.m31 = 0.;
        rotMat.m32 = 0.;
        rotMat.m33 = 1.;

        transform.set(rotMat);

    }

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to