From moore@svax.cs.cornell.edu Wed Feb  1 16:07:38 1989
Path: leah!rpi!batcomputer!cornell!moore
From: moore@svax.cs.cornell.edu (Doug Moore)
Newsgroups: comp.graphics
Subject: Re: 3D Rotations/Instancing
Message-ID: <24681@cornell.UUCP>
Date: 1 Feb 89 21:07:38 GMT
References: <65@sdcc10.ucsd.EDU> <5909@leadsv.UUCP> <25598@sgi.SGI.COM> <5941@leadsv.UUCP>
Sender: nobody@cornell.UUCP
Reply-To: moore@svax.cs.cornell.edu (Doug Moore)
Distribution: usa
Organization: Cornell Univ. CS Dept, Ithaca NY
Lines: 49



Since no one else has mentioned this, I guess I will.  Why not use quaternions,
rather than rotation matrices, to represent your rotations?  Quaternions on
the unit sphere and 3-d rotations are isomorphic, and quaternions don't require
the redundant storage and calculation that 3x3 matrices do.

A quaternion may be thought of as an entity of the form , where s is a
scalar and x is a 3-vector.  Multiplication of quaternions is given by
 *  = .
A unit quaternion is one that satisfies s*s + x dot x = 1.  A unit quaternion
may also be thought of as a rotation of angle 2 arccos s about the axis v.  To
rotate a vector v by a rotation quaternion q to get a vector w, use the
formula <0,w> = inv(q) * <0,v> * q, where inv(q) * q = <1,0>, and inv()
= .  Or, if you prefer, form the equivalent rotation matrix

1 - 2 (x2*x2 + x3*x3)     2 (x1*x2 + s * x3)     2 (x1*x3 - s*x2)
2 (x1*x2 - s*x3)         1 - 2 (x1*x1 + x3*x3)   2 (x2*x3 + s*x1)
2 (x1*x3 + s*x2)          2 (x2*x3 - s*x1)      1 - 2 (x1*x1 + x2*x2)

and use that.

The basic algorithm, then, to display vectors V = 
rotating by q every frame is

rot = <1,0>
do-forever
	R = rotation matrix associated with rot
	DISP = R * V
	display all vectors in DISP
	rot = rot * q
	norm = rot.s * rot.s + rot.x1 * rot.x1 + rot.x2 * rot.x2 + rot.x3 * rot.x3
	if (abs(norm - 1) > tolerance)
		norm = sqrt(norm)
		rot.s = rot.s/norm
		rot.x1 = rot.x1/norm
		rot.x2 = rot.x2/norm
		rot.x3 = rot.x3/norm
	endif
enddo

That's the general idea, anyway.  For a less terse exposition, see 
Ken Shoemake, "Animating Rotation with Quaternion Curves", COMPUTER GRAPHICS
Vol 19 No 3, pp. 245-254.

Incidentally, similar quaternion techniques can be used for 4-d rotations.
I haven't been able to get a handle on higher dimensions, though.

Doug Moore (moore@svax.cs.cornell.edu)

Discuss this article in the forums


Date this article was posted to GameDev.net: 7/16/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
Quaternions

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!