Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quaternion math for rotation?

I'm drawing a flat disk using gluDisk() in my scene. gluDisk() draws the disk facing the positive Z axis but I want it to be facing some arbitrary normal I have.
Clearly I need to use glRotate() to get the disk facing properly but what should be the rotation? I remember this can be calculated using Quaternions but I can't seem to remember the math.

like image 597
shoosh Avatar asked Apr 18 '09 22:04

shoosh


1 Answers

The solution should be pretty straightforward, and shouldn't require quarternions.

The axis of rotation to get from Normal1 to Normal2 must be orthogonal to both, so just take their vector cross-product.

The amount of rotation is easily derived from their dot-product. This value is |A|.|B|.cos(theta), but as the two normal vectors should be normalised it will give cos(theta), so just take the inverse cosine to get the rotation amount.

The resulting vector and angle are the required parameters for glRotate() - there's no need to calculate the actual rotation matrix yourself.

p.s. don't forget that glRotate() needs the angle in degrees, but the normal C trig functions work in radians.

like image 171
Alnitak Avatar answered Sep 20 '22 16:09

Alnitak