Warning!  I do this quite a bit in one of my Java 3D apps.  I found that the
Quat4f get normalized regardless of the initial coordinates I use.  So I end
up setting the length of the quaternion after I instantiated it.

Note that *all* vectors can be thought of as having one "end" at the origin
and the other end at some other point is space.

So let's say I have a vector v and I want to rotate it about the axis
defined by the vector p by the angular amount alpha.

First construct the unit rotation quaternion, q,  from the vector p.  (I
think that dAlpha is double the angle you want to rotate v.)  And define its
inverse quaternion q^-1.

Then convert the vector v into a quaternion.  Do the quaternion
multiplication
  r = q^(-1)(v)(q)
The components of r is the rotated vector.

Here's the code.

---------------------------

   Quat4d q = new Quat4d(p.x * Math.sin(dAlpha),
                         p.y * Math.sin(dAlpha),
                         p.z * Math.sin(dAlpha),
                         Math.cos(dAlpha));
    q.normalize();

    // Will need the inverse quaternion to q.
    Quat4d q_1 = new Quat4d(q);
    q_1.inverse();

    // We'll need the length of the vector v to deal with a J3D bug.
    double vqScale = v.length();

    // Make the vector v into a quaternion.
    Quat4d vq = new Quat4d(v.x, v.y, v.z, 0.0);

    // There is a bug in using Quat4d and Quat4f.
    // The above constructor will normalize the quaternion.
    // It shouldn't, but it does.  I explicitly code a normalization
    // just to remind myself that the result of Quat4d is normalized.
    vq.normalize();     // Quat4d seems to do this normalization. BUG?! yuk!
    vq.scale(vqScale);  // Set the length back to the length of the vector
v.

    // Do the multiplication.
    Quat4d r = new Quat4d();
    r.mul(q_1, vq);
    r.mul(q);

    // Set vector v to the resulting components.
    v = new Vector3d(r.x, r.y, r.z);

---------------------------

Hope this helps

Bob Gray

-----Original Message-----
From: Siau Tan Long [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, September 19, 2000 11:14 AM
To: [EMAIL PROTECTED]
Subject: [JAVA3D] How to use quaternion in J3D?


How to use quaternion to do rotation about an arbitrary axis in Java3D?
e.g: Quat4f





__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com

===========================================================================
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".

===========================================================================
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