Created
August 18, 2011 16:54
-
-
Save timknip/1154512 to your computer and use it in GitHub Desktop.
Quaternion setFromRotationMatrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setFromRotationMatrix: function ( m ) { | |
// Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm | |
function copySign(a, b) { | |
return b < 0 ? -Math.abs(a) : Math.abs(a); | |
} | |
var absQ = Math.pow(m.determinant(), 1.0 / 3.0); | |
this.w = Math.sqrt( Math.max( 0, absQ + m.n11 + m.n22 + m.n33 ) ) / 2; | |
this.x = Math.sqrt( Math.max( 0, absQ + m.n11 - m.n22 - m.n33 ) ) / 2; | |
this.y = Math.sqrt( Math.max( 0, absQ - m.n11 + m.n22 - m.n33 ) ) / 2; | |
this.z = Math.sqrt( Math.max( 0, absQ - m.n11 - m.n22 + m.n33 ) ) / 2; | |
this.x = copySign( this.x, ( m.n32 - m.n23 ) ); | |
this.y = copySign( this.y, ( m.n13 - m.n31 ) ); | |
this.z = copySign( this.z, ( m.n21 - m.n12 ) ); | |
this.normalize(); | |
return this; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment