Orientation with Quaternions
This document discusses using quaternions rather than matrices as a means to store an object's orientation information.
A quaternion may be thought of as an entity of the form [s,x], where s is a scalar and x is a 3-vector. Multiplication of quaternions is given by [s1,x1] * [s2,x2] = [s1 * s2 - x1 dot x2, s1*x2 + s2*x1 + x1 cross x2]. 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([s,v]) = [s,-v]. 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)The basic algorithm, then, to display vectors V = [v1 ; v2 ; v3 ; ... ; vn] 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
enddoIncidentally, similar quaternion techniques can be used for 4-d rotations. I haven't been able to get a handle on higher dimensions, though.
Related Tutorials
A Day As A Game Designer
The fabled game designer. The man, the myth, the legend behind the game. An inspiration to young creatives who wish to …
Kai Wüest
So You Want to be a Game Developer?
Battletech tools developer Chris Eck describes his journey and advice for getting a job in the industry.
A Quick Guide to Gamification and Gamification Careers
If you find yourself slightly stumped over the ever-growing buzz around gamification, and unsure whether there is any o…
Sophie Jackson
Raph Koster's Postmortems: Una Carrera
This excerpt from Raph Koster's Postmortems is an adaptation from a speech he delivered at GameDay Peru in early 2015. …
Raph Koster
Negotiating Sign-On Bonuses in the Games Industry
A quick blurb on signing bonuses in the games industry and how to approach asking for one.
A LinkedIn Profile for Job Hunting and Networking
Marc Mencher is founder and CEO of GameRecruiter and author of Get in the Game!, an instructional book on building a ca…
Discussion
More from Doug Moore
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion